]> granicus.if.org Git - python/commitdiff
Patch #683592: unicode support for os.listdir()
authorJust van Rossum <just@letterror.com>
Tue, 25 Feb 2003 21:42:15 +0000 (21:42 +0000)
committerJust van Rossum <just@letterror.com>
Tue, 25 Feb 2003 21:42:15 +0000 (21:42 +0000)
os.listdir() may now return unicode strings on platforms that set
Py_FileSystemDefaultEncoding.

Misc/NEWS
Modules/posixmodule.c

index fa7ef5ee73dce62e3979053737abccb7cea221e3..750b2c3667ea53fc73399721b08b73f3dfb6d792 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -25,7 +25,11 @@ Extension modules
 Library
 -------
 
-TBD
+- os.listdir() now returns Unicode strings on platforms that set
+  Py_FileSystemDefaultEncoding, for file names that are not representable
+  in ASCII.  (This currently only affects MacOS X; on Windows versions
+  with wide file name support os.listdir() already returned Unicode
+  strings.)
 
 Tools/Demos
 -----------
@@ -61,6 +65,9 @@ TBD
 Mac
 ---
 
+- os.listdir() now may return Unicode strings on MacOS X. See the general
+  news item under "Library".
+
 - A new method MacOS.WMAvailable() returns true if it is safe to access
   the window manager, false otherwise.
 
index d203378d3dfaeae22087226ab072fd803c7a8ef8..482bba95cbd8c2563b04b832990ad23807bbab31 100644 (file)
@@ -1795,6 +1795,30 @@ posix_listdir(PyObject *self, PyObject *args)
                        d = NULL;
                        break;
                }
+#ifdef Py_USING_UNICODE
+               if (Py_FileSystemDefaultEncoding != NULL) {
+                       PyObject *w;
+
+                       w = PyUnicode_FromEncodedObject(v,
+                                       Py_FileSystemDefaultEncoding, 
+                                       "strict");
+                       Py_DECREF(v);
+                       v = w;
+                       if (v == NULL) {
+                               Py_DECREF(d);
+                               d = NULL;
+                               break;
+                       }
+                       /* attempt to convert to ASCII */
+                       w = PyUnicode_AsASCIIString(v);
+                       if (w != NULL) {
+                               Py_DECREF(v);
+                               v = w;
+                       }
+                       else
+                               PyErr_Clear();
+               }
+#endif
                if (PyList_Append(d, v) != 0) {
                        Py_DECREF(v);
                        Py_DECREF(d);