### Eclipse Workspace Patch 1.0 #P jython-trunk Index: tests/java/org/python/core/PyUnicodeTest.java =================================================================== --- tests/java/org/python/core/PyUnicodeTest.java (revision 0) +++ tests/java/org/python/core/PyUnicodeTest.java (revision 0) @@ -0,0 +1,38 @@ +package org.python.core; + +import junit.framework.TestCase; + +public class PyUnicodeTest extends TestCase { + + public void testUnicode___str__() { + PyUnicode unicode; + PyString result; + + final String asciiString = "to"; + unicode = new PyUnicode(asciiString); + result = unicode.unicode___str__(); + assertEquals(asciiString, result.toString()); + + final String umlaut = "\u00F6"; // german o umlaut + final String unicodeString = "t".concat(umlaut); + unicode = new PyUnicode(unicodeString); + result = unicode.unicode___str__(); + assertEquals(unicodeString, result.toString()); + } + + + public void testUnicode___repr__() { + PyUnicode unicode; + PyString result; + + unicode = new PyUnicode("to"); + result = unicode.unicode___repr__(); + assertEquals("u'to'", result.toString()); + + final String umlaut = "\u00F6"; // german o umlaut + unicode = new PyUnicode("t".concat(umlaut)); + result = unicode.unicode___repr__(); + assertEquals("u't\\xf6'", result.toString()); + } + +} Index: src/org/python/core/PyUnicode.java =================================================================== --- src/org/python/core/PyUnicode.java (revision 7173) +++ src/org/python/core/PyUnicode.java (working copy) @@ -225,7 +225,8 @@ @ExposedMethod(doc = BuiltinDocs.unicode___str___doc) final PyString unicode___str__() { - return new PyString(encode()); + // latin-1 covers a wider range of strings than the default ascii encoding + return new PyString(encode("latin-1")); } @Override Index: Lib/test/test_sys_jy.py =================================================================== --- Lib/test/test_sys_jy.py (revision 7173) +++ Lib/test/test_sys_jy.py (working copy) @@ -1,7 +1,9 @@ import sys +import os import re import unittest import test.test_support +import tempfile class SysTest(unittest.TestCase): @@ -146,8 +148,42 @@ self.assert_(Main.getResource('Main.txt')) +# bug 1693: importing from a unicode path threw a unicode encoding error +class SyspathUnicodeTest(unittest.TestCase): + + def test_nonexisting_import_from_unicodepath(self): + sys.path.append(u'/home/tr\xf6\xf6t') # \xf6 = german o umlaut + try: + import non_existing_module + self.fail("ImportError expected") + except ImportError, ie: + pass + + def test_import_from_unicodepath(self): + moduleDir = tempfile.mkdtemp(suffix=u'tr\xf6\xf6t') # \xf6 = german o umlaut + try: + self.assertTrue(os.path.exists(moduleDir)) + module = 'unicodetempmodule' + moduleFile = '%s/%s.py' % (moduleDir, module) + try: + f = open(moduleFile, 'w') + f.write('# empty module') + f.close() + self.assertTrue(os.path.exists(moduleFile)) + sys.path.append(moduleDir) + exec('import %s' % module) + moduleClassFile = '%s/%s$py.class' % (moduleDir, module) + self.assertTrue(os.path.exists(moduleClassFile)) + os.remove(moduleClassFile) + finally: + os.remove(moduleFile) + finally: + os.rmdir(moduleDir) + self.assertFalse(os.path.exists(moduleDir)) + + def test_main(): - test.test_support.run_unittest(SysTest, ShadowingTest, SyspathResourceTest) + test.test_support.run_unittest(SysTest, ShadowingTest, SyspathResourceTest, SyspathUnicodeTest) if __name__ == "__main__": test_main()