From 96b1c903f5310202a665e86e624a38ede50429c3 Mon Sep 17 00:00:00 2001
From: Just van Rossum <just@letterror.com>
Date: Mon, 3 Mar 2003 17:32:15 +0000
Subject: Patch #683592 revisited, after discussions with MvL: - Implement the
 behavior as specified in PEP 277, meaning os.listdir()   will only return
 unicode strings if it is _called_ with a unicode   argument. - And then
 return only unicode, don't attempt to convert to ASCII. - Don't switch on
 Py_FileSystemDefaultEncoding, but simply use the   default encoding if
 Py_FileSystemDefaultEncoding is NULL. This means   os.listdir() can now raise
 UnicodeDecodeError if the default encoding   can't represent the directory
 entry. (This seems better than silcencing   the error and fall back to a byte
 string.) - Attempted to decribe the above in Doc/lib/libos.tex. - Reworded
 the Misc/NEWS items to reflect the current situation.

This checkin also fixes bug #696261, which was due to os.listdir() not
using Py_FileSystemDefaultEncoding, like all file system calls are
supposed to.
---
 Doc/lib/libos.tex     |  4 ++--
 Misc/NEWS             | 12 +++++-------
 Modules/posixmodule.c | 18 ++++++++----------
 3 files changed, 15 insertions(+), 19 deletions(-)

diff --git a/Doc/lib/libos.tex b/Doc/lib/libos.tex
index b2c10f7..844704b 100644
--- a/Doc/lib/libos.tex
+++ b/Doc/lib/libos.tex
@@ -711,8 +711,8 @@ entries \code{'.'} and \code{'..'} even if they are present in the
 directory.
 Availability: Macintosh, \UNIX, Windows.
 
-\versionadded[On Windows NT/2k/XP, if \var{path} is a Unicode object,
-the result will be a list of Unicode objects.]{2.3}
+\versionadded[On Windows NT/2k/XP and Unix, if \var{path} is a Unicode
+object, the result will be a list of Unicode objects.]{2.3}
 \end{funcdesc}
 
 \begin{funcdesc}{lstat}{path}
diff --git a/Misc/NEWS b/Misc/NEWS
index bb13310..5bf335a 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -41,11 +41,9 @@ Library
   about the rationale is in the NEWS entry for that.  See also SF bug
   report <http://www.python.org/sf/693121>.
 
-- 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.)
+- On Unix platforms, if os.listdir() is called with a Unicode argument,
+  it now returns Unicode strings.  (This behavior was added earlier
+  to the Windows NT/2k/XP version of os.listdir().)
 
 - Distutils: both 'py_modules' and 'packages' keywords can now be specified
   in core.setup().  Previously you could supply one or the other, but
@@ -88,8 +86,8 @@ TBD
 Mac
 ---
 
-- os.listdir() now may return Unicode strings on MacOS X. See the general
-  news item under "Library".
+- os.listdir() now returns Unicode strings on MacOS X when called with
+  a Unicode argument. 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.
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index 482bba9..d9033f6 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -1775,7 +1775,13 @@ posix_listdir(PyObject *self, PyObject *args)
 	PyObject *d, *v;
 	DIR *dirp;
 	struct dirent *ep;
-	if (!PyArg_ParseTuple(args, "s:listdir", &name))
+	int arg_is_unicode = 1;
+
+	if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
+		arg_is_unicode = 0;
+		PyErr_Clear();
+	}
+	if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name))
 		return NULL;
 	if ((dirp = opendir(name)) == NULL) {
 		return posix_error_with_filename(name);
@@ -1796,7 +1802,7 @@ posix_listdir(PyObject *self, PyObject *args)
 			break;
 		}
 #ifdef Py_USING_UNICODE
-		if (Py_FileSystemDefaultEncoding != NULL) {
+		if (arg_is_unicode) {
 			PyObject *w;
 
 			w = PyUnicode_FromEncodedObject(v,
@@ -1809,14 +1815,6 @@ posix_listdir(PyObject *self, PyObject *args)
 				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) {
-- 
cgit v0.12

