@@ -3272,14 +3272,16 @@ posix_link(PyObject *self, PyObject *args, PyObject *kwargs)
32723272
32733273
32743274PyDoc_STRVAR (posix_listdir__doc__ ,
3275- "listdir(path='.') -> list_of_strings\n\n\
3276- Return a list containing the names of the entries in the directory.\n\
3277- \n\
3275+ "listdir(path='.') -> list_of_filenames\n\n\
3276+ Return a list containing the names of the files in the directory.\n\
32783277The list is in arbitrary order. It does not include the special\n\
32793278entries '.' and '..' even if they are present in the directory.\n\
32803279\n\
3281- path can always be specified as a string.\n\
3282- On some platforms, path may also be specified as an open file descriptor.\n\
3280+ path can be specified as either str or bytes. If path is bytes,\n\
3281+ the filenames returned will also be bytes; in all other circumstances\n\
3282+ the filenames returned will be str.\n\
3283+ On some platforms, path may also be specified as an open file descriptor;\n\
3284+ the file descriptor must refer to a directory.\n\
32833285 If this functionality is unavailable, using it raises NotImplementedError." );
32843286
32853287static PyObject *
@@ -3316,7 +3318,7 @@ posix_listdir(PyObject *self, PyObject *args, PyObject *kwargs)
33163318 PyObject * v ;
33173319 DIR * dirp = NULL ;
33183320 struct dirent * ep ;
3319- int arg_is_unicode = 1 ;
3321+ int return_str ; /* if false, return bytes */
33203322#endif
33213323
33223324 memset (& path , 0 , sizeof (path ));
@@ -3538,11 +3540,6 @@ posix_listdir(PyObject *self, PyObject *args, PyObject *kwargs)
35383540#else
35393541
35403542 errno = 0 ;
3541- /* v is never read, so it does not need to be initialized yet. */
3542- if (path .narrow && !PyArg_ParseTuple (args , "U:listdir" , & v )) {
3543- arg_is_unicode = 0 ;
3544- PyErr_Clear ();
3545- }
35463543#ifdef HAVE_FDOPENDIR
35473544 if (path .fd != -1 ) {
35483545 /* closedir() closes the FD, so we duplicate it */
@@ -3555,14 +3552,26 @@ posix_listdir(PyObject *self, PyObject *args, PyObject *kwargs)
35553552 goto exit ;
35563553 }
35573554
3555+ return_str = 1 ;
3556+
35583557 Py_BEGIN_ALLOW_THREADS
35593558 dirp = fdopendir (fd );
35603559 Py_END_ALLOW_THREADS
35613560 }
35623561 else
35633562#endif
35643563 {
3565- char * name = path .narrow ? path .narrow : "." ;
3564+ char * name ;
3565+ if (path .narrow ) {
3566+ name = path .narrow ;
3567+ /* only return bytes if they specified a bytes object */
3568+ return_str = !(PyBytes_Check (path .object ));
3569+ }
3570+ else {
3571+ name = "." ;
3572+ return_str = 1 ;
3573+ }
3574+
35663575 Py_BEGIN_ALLOW_THREADS
35673576 dirp = opendir (name );
35683577 Py_END_ALLOW_THREADS
@@ -3593,7 +3602,7 @@ posix_listdir(PyObject *self, PyObject *args, PyObject *kwargs)
35933602 (NAMLEN (ep ) == 1 ||
35943603 (ep -> d_name [1 ] == '.' && NAMLEN (ep ) == 2 )))
35953604 continue ;
3596- if (arg_is_unicode )
3605+ if (return_str )
35973606 v = PyUnicode_DecodeFSDefaultAndSize (ep -> d_name , NAMLEN (ep ));
35983607 else
35993608 v = PyBytes_FromStringAndSize (ep -> d_name , NAMLEN (ep ));
0 commit comments