|
From: <pj...@us...> - 2011-01-07 01:15:58
|
Revision: 7182
http://jython.svn.sourceforge.net/jython/?rev=7182&view=rev
Author: pjenvey
Date: 2011-01-07 01:15:51 +0000 (Fri, 07 Jan 2011)
Log Message:
-----------
special case handling of unicode sys.path items, basically pass them through
fixes #1693
test case from Oti
Modified Paths:
--------------
trunk/jython/Lib/test/test_sys_jy.py
trunk/jython/NEWS
trunk/jython/src/org/python/core/SyspathJavaLoader.java
trunk/jython/src/org/python/core/imp.java
trunk/jython/src/org/python/core/packagecache/PathPackageManager.java
Modified: trunk/jython/Lib/test/test_sys_jy.py
===================================================================
--- trunk/jython/Lib/test/test_sys_jy.py 2010-12-29 01:59:20 UTC (rev 7181)
+++ trunk/jython/Lib/test/test_sys_jy.py 2011-01-07 01:15:51 UTC (rev 7182)
@@ -1,7 +1,10 @@
+from __future__ import with_statement
+import os
+import re
import sys
-import re
+import tempfile
import unittest
-import test.test_support
+from test import test_support
class SysTest(unittest.TestCase):
@@ -132,7 +135,7 @@
class SyspathResourceTest(unittest.TestCase):
def setUp(self):
self.orig_path = sys.path
- sys.path.insert(0, test.test_support.findfile("bug1373.jar"))
+ sys.path.insert(0, test_support.findfile("bug1373.jar"))
def tearDown(self):
sys.path = self.orig_path
@@ -146,8 +149,43 @@
self.assert_(Main.getResource('Main.txt'))
+class SyspathUnicodeTest(unittest.TestCase):
+ """bug 1693: importing from a unicode path threw a unicode encoding
+ error"""
+
+ def test_nonexisting_import_from_unicodepath(self):
+ # \xf6 = german o umlaut
+ sys.path.append(u'/home/tr\xf6\xf6t')
+ self.assertRaises(ImportError, __import__, 'non_existing_module')
+
+ def test_import_from_unicodepath(self):
+ # \xf6 = german o umlaut
+ moduleDir = tempfile.mkdtemp(suffix=u'tr\xf6\xf6t')
+ try:
+ self.assertTrue(os.path.exists(moduleDir))
+ module = 'unicodetempmodule'
+ moduleFile = '%s/%s.py' % (moduleDir, module)
+ try:
+ with open(moduleFile, 'w') as f:
+ f.write('# empty module')
+ self.assertTrue(os.path.exists(moduleFile))
+ sys.path.append(moduleDir)
+ __import__(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_support.run_unittest(SysTest,
+ ShadowingTest,
+ SyspathResourceTest,
+ SyspathUnicodeTest)
if __name__ == "__main__":
test_main()
Modified: trunk/jython/NEWS
===================================================================
--- trunk/jython/NEWS 2010-12-29 01:59:20 UTC (rev 7181)
+++ trunk/jython/NEWS 2011-01-07 01:15:51 UTC (rev 7182)
@@ -8,6 +8,7 @@
- [ 1675 ] Jython exits prematurely when executing a file, thus killing Swing windows
- [ 1682 ] exit code of 0 on unhandled exception
- [ 1668 ] strptime('','') works on cpython but not on jython
+ - [ 1693 ] Unicode sys.path elements cause UnicodeErrors on import
Jython 2.5.2rc2
Bugs Fixed
Modified: trunk/jython/src/org/python/core/SyspathJavaLoader.java
===================================================================
--- trunk/jython/src/org/python/core/SyspathJavaLoader.java 2010-12-29 01:59:20 UTC (rev 7181)
+++ trunk/jython/src/org/python/core/SyspathJavaLoader.java 2011-01-07 01:15:51 UTC (rev 7182)
@@ -3,7 +3,6 @@
package org.python.core;
-import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
@@ -14,8 +13,6 @@
import java.util.StringTokenizer;
import java.util.zip.ZipEntry;
-import javax.management.RuntimeErrorException;
-
import org.python.core.util.RelativeFile;
public class SyspathJavaLoader extends ClassLoader {
@@ -115,7 +112,10 @@
SyspathArchive archive = (SyspathArchive)entry;
buffer = getBytesFromArchive(archive, name);
} else {
- String dir = entry.__str__().toString();
+ if (!(entry instanceof PyUnicode)) {
+ entry = entry.__str__();
+ }
+ String dir = entry.toString();
buffer = getBytesFromDir(dir, name);
}
if (buffer != null) {
@@ -155,7 +155,10 @@
}
continue;
}
- String dir = sys.getPath(entry.__str__().toString());
+ if (!(entry instanceof PyUnicode)) {
+ entry = entry.__str__();
+ }
+ String dir = sys.getPath(entry.toString());
try {
File resource = new File(dir, res);
if (!resource.exists()) {
Modified: trunk/jython/src/org/python/core/imp.java
===================================================================
--- trunk/jython/src/org/python/core/imp.java 2010-12-29 01:59:20 UTC (rev 7181)
+++ trunk/jython/src/org/python/core/imp.java 2011-01-07 01:15:51 UTC (rev 7182)
@@ -436,7 +436,6 @@
}
static PyObject find_module(String name, String moduleName, PyList path) {
-
PyObject loader = Py.None;
PySystemState sys = Py.getSystemState();
PyObject metaPath = sys.meta_path;
@@ -468,7 +467,10 @@
return loadFromLoader(loader, moduleName);
}
}
- ret = loadFromSource(sys, name, moduleName, p.__str__().toString());
+ if (!(p instanceof PyUnicode)) {
+ p = p.__str__();
+ }
+ ret = loadFromSource(sys, name, moduleName, p.toString());
if (ret != null) {
return ret;
}
Modified: trunk/jython/src/org/python/core/packagecache/PathPackageManager.java
===================================================================
--- trunk/jython/src/org/python/core/packagecache/PathPackageManager.java 2010-12-29 01:59:20 UTC (rev 7181)
+++ trunk/jython/src/org/python/core/packagecache/PathPackageManager.java 2011-01-07 01:15:51 UTC (rev 7182)
@@ -7,7 +7,9 @@
import org.python.core.Py;
import org.python.core.PyJavaPackage;
import org.python.core.PyList;
+import org.python.core.PyObject;
import org.python.core.PyString;
+import org.python.core.PyUnicode;
import org.python.core.util.RelativeFile;
import java.io.BufferedInputStream;
@@ -38,7 +40,11 @@
+ name;
for (int i = 0; i < path.__len__(); i++) {
- String dir = path.pyget(i).__str__().toString();
+ PyObject entry = path.pyget(i);
+ if (!(entry instanceof PyUnicode)) {
+ entry = entry.__str__();
+ }
+ String dir = entry.toString();
File f = new RelativeFile(dir, child);
try {
@@ -96,7 +102,12 @@
String child = jpkg.__name__.replace('.', File.separatorChar);
for (int i = 0; i < path.__len__(); i++) {
- String dir = path.pyget(i).__str__().toString();
+ PyObject entry = path.pyget(i);
+ if (!(entry instanceof PyUnicode)) {
+ entry = entry.__str__();
+ }
+ String dir = entry.toString();
+
if (dir.length() == 0) {
dir = null;
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|