Commits

Philip Jenvey committed 06981af

have os.listdir return unicode when requested
fixes #1520

Comments (0)

Files changed (2)

     - [ 1396 ] Assigning os module funcs as class attributes incompatible with CPython
     - [ 1504 ] Inheriting twice from the same Java interface causes MRO problems
     - [ 1511 ] PySet doesn't support Java serialization
-    - [ 1426 ] JSR 223 Bindings changes not taking effect and leaking between threads; unnecessary synchronization
+    - [ 1426 ] JSR 223 Bindings changes not taking effect and leaking between threads; unnecessary synchronization
     - [ 1548 ] Parentheses in CLASSPATH cause errors in jython.bat
     - [ 1576 ] files opened in 'a+' mode not readable
     - [ 1563 ] unicode() for Java objects working differently in 2.2 and 2.5
     - [ 1534 ] new style object __dict__[name] ignored
     - [ 1479 ] xml parser file lock
     - [ 1582 ] com.ziclix.python.sql.PyConnection leaks memory
+    - [ 1520 ] os.listdir doesn't return unicode when requested
     - Fix runtime issues during exitfuncs triggered via SystemRestart (such as
       during Django or Pylons development mode reloading)
     - Fix pickling of collections.defaultdict objects

src/org/python/modules/posix/PosixModule.java

         "path: path of directory to list\n\n" +
         "The list is in arbitrary order.  It does not include the special\n" +
         "entries '.' and '..' even if they are present in the directory.");
-    public static PyList listdir(String path) {
-        ensurePath(path);
+    public static PyList listdir(PyObject pathObj) {
+        ensurePath(pathObj);
+        String path = pathObj.asString();
         PyList list = new PyList();
         File file = new RelativeFile(path);
         String[] names = file.list();
-
         if (names == null) {
             // Can't read the path for some reason. stat will throw an error if it can't
             // read it either
             }
             throw Py.OSError("listdir(): an unknown error occured: " + path);
         }
+
+        PyString string = (PyString) pathObj;
         for (String name : names) {
-            list.append(new PyString(name));
+            list.append(string.createInstance(name));
         }
         return list;
     }
         return new RelativeFile(path).getPath();
     }
 
+    private static void ensurePath(PyObject path) {
+        if (!(path instanceof PyString)) {
+            throw Py.TypeError(String.format("coercing to Unicode: need string, %s type found",
+                                             path.getType().fastGetName()));
+        }
+    }
+
     private static void ensurePath(String path) {
         if (path == null) {
             throw Py.TypeError("coercing to Unicode: need string or buffer, NoneType found");