mirror of
https://github.com/python/cpython.git
synced 2025-11-03 19:34:08 +00:00
Refactor the common code out of the posix and windows listdir
implementations from my previous commit into the higher level function.
This commit is contained in:
parent
16ea14a690
commit
40a2160a09
1 changed files with 44 additions and 55 deletions
|
|
@ -3251,12 +3251,9 @@ On some platforms, path may also be specified as an open file descriptor;\n\
|
||||||
|
|
||||||
#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
|
#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
|
||||||
static PyObject *
|
static PyObject *
|
||||||
_listdir_windows_no_opendir(PyObject *self, PyObject *args, PyObject *kwargs)
|
_listdir_windows_no_opendir(path_t *path, PyObject *list)
|
||||||
{
|
{
|
||||||
path_t path;
|
|
||||||
PyObject *list = NULL;
|
|
||||||
static char *keywords[] = {"path", NULL};
|
static char *keywords[] = {"path", NULL};
|
||||||
int fd = -1;
|
|
||||||
PyObject *v;
|
PyObject *v;
|
||||||
HANDLE hFindFile = INVALID_HANDLE_VALUE;
|
HANDLE hFindFile = INVALID_HANDLE_VALUE;
|
||||||
BOOL result;
|
BOOL result;
|
||||||
|
|
@ -3268,25 +3265,16 @@ _listdir_windows_no_opendir(PyObject *self, PyObject *args, PyObject *kwargs)
|
||||||
PyObject *po = NULL;
|
PyObject *po = NULL;
|
||||||
wchar_t *wnamebuf = NULL;
|
wchar_t *wnamebuf = NULL;
|
||||||
|
|
||||||
memset(&path, 0, sizeof(path));
|
if (!path->narrow) {
|
||||||
path.function_name = "listdir";
|
|
||||||
path.nullable = 1;
|
|
||||||
|
|
||||||
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&:listdir", keywords,
|
|
||||||
path_converter, &path)) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!path.narrow) {
|
|
||||||
WIN32_FIND_DATAW wFileData;
|
WIN32_FIND_DATAW wFileData;
|
||||||
wchar_t *po_wchars;
|
wchar_t *po_wchars;
|
||||||
|
|
||||||
if (!path.wide) { /* Default arg: "." */
|
if (!path->wide) { /* Default arg: "." */
|
||||||
po_wchars = L".";
|
po_wchars = L".";
|
||||||
len = 1;
|
len = 1;
|
||||||
} else {
|
} else {
|
||||||
po_wchars = path.wide;
|
po_wchars = path->wide;
|
||||||
len = wcslen(path.wide);
|
len = wcslen(path->wide);
|
||||||
}
|
}
|
||||||
/* The +5 is so we can append "\\*.*\0" */
|
/* The +5 is so we can append "\\*.*\0" */
|
||||||
wnamebuf = malloc((len + 5) * sizeof(wchar_t));
|
wnamebuf = malloc((len + 5) * sizeof(wchar_t));
|
||||||
|
|
@ -3312,7 +3300,7 @@ _listdir_windows_no_opendir(PyObject *self, PyObject *args, PyObject *kwargs)
|
||||||
if (error == ERROR_FILE_NOT_FOUND)
|
if (error == ERROR_FILE_NOT_FOUND)
|
||||||
goto exit;
|
goto exit;
|
||||||
Py_DECREF(list);
|
Py_DECREF(list);
|
||||||
list = path_error(&path);
|
list = path_error(path);
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
do {
|
do {
|
||||||
|
|
@ -3341,15 +3329,15 @@ _listdir_windows_no_opendir(PyObject *self, PyObject *args, PyObject *kwargs)
|
||||||
it got to the end of the directory. */
|
it got to the end of the directory. */
|
||||||
if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
|
if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
|
||||||
Py_DECREF(list);
|
Py_DECREF(list);
|
||||||
list = path_error(&path);
|
list = path_error(path);
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
} while (result == TRUE);
|
} while (result == TRUE);
|
||||||
|
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
strcpy(namebuf, path.narrow);
|
strcpy(namebuf, path->narrow);
|
||||||
len = path.length;
|
len = path->length;
|
||||||
if (len > 0) {
|
if (len > 0) {
|
||||||
char ch = namebuf[len-1];
|
char ch = namebuf[len-1];
|
||||||
if (ch != SEP && ch != ALTSEP && ch != ':')
|
if (ch != SEP && ch != ALTSEP && ch != ':')
|
||||||
|
|
@ -3368,7 +3356,7 @@ _listdir_windows_no_opendir(PyObject *self, PyObject *args, PyObject *kwargs)
|
||||||
if (error == ERROR_FILE_NOT_FOUND)
|
if (error == ERROR_FILE_NOT_FOUND)
|
||||||
goto exit;
|
goto exit;
|
||||||
Py_DECREF(list);
|
Py_DECREF(list);
|
||||||
list = path_error(&path);
|
list = path_error(path);
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
do {
|
do {
|
||||||
|
|
@ -3396,7 +3384,7 @@ _listdir_windows_no_opendir(PyObject *self, PyObject *args, PyObject *kwargs)
|
||||||
it got to the end of the directory. */
|
it got to the end of the directory. */
|
||||||
if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
|
if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
|
||||||
Py_DECREF(list);
|
Py_DECREF(list);
|
||||||
list = path_error(&path);
|
list = path_error(path);
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
} while (result == TRUE);
|
} while (result == TRUE);
|
||||||
|
|
@ -3406,26 +3394,21 @@ exit:
|
||||||
if (FindClose(hFindFile) == FALSE) {
|
if (FindClose(hFindFile) == FALSE) {
|
||||||
if (list != NULL) {
|
if (list != NULL) {
|
||||||
Py_DECREF(list);
|
Py_DECREF(list);
|
||||||
list = path_error(&path);
|
list = path_error(path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (wnamebuf)
|
if (wnamebuf)
|
||||||
free(wnamebuf);
|
free(wnamebuf);
|
||||||
path_cleanup(&path);
|
|
||||||
|
|
||||||
return list;
|
return list;
|
||||||
|
|
||||||
} /* end of _listdir_windows_no_opendir */
|
} /* end of _listdir_windows_no_opendir */
|
||||||
|
|
||||||
#else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */
|
#else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
_posix_listdir(PyObject *self, PyObject *args, PyObject *kwargs)
|
_posix_listdir(path_t *path, PyObject *list)
|
||||||
{
|
{
|
||||||
path_t path;
|
|
||||||
PyObject *list = NULL;
|
|
||||||
static char *keywords[] = {"path", NULL};
|
|
||||||
int fd = -1;
|
int fd = -1;
|
||||||
|
|
||||||
PyObject *v;
|
PyObject *v;
|
||||||
|
|
@ -3433,24 +3416,12 @@ _posix_listdir(PyObject *self, PyObject *args, PyObject *kwargs)
|
||||||
struct dirent *ep;
|
struct dirent *ep;
|
||||||
int return_str; /* if false, return bytes */
|
int return_str; /* if false, return bytes */
|
||||||
|
|
||||||
memset(&path, 0, sizeof(path));
|
|
||||||
path.function_name = "listdir";
|
|
||||||
path.nullable = 1;
|
|
||||||
#ifdef HAVE_FDOPENDIR
|
|
||||||
path.allow_fd = 1;
|
|
||||||
path.fd = -1;
|
|
||||||
#endif
|
|
||||||
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&:listdir", keywords,
|
|
||||||
path_converter, &path)) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
errno = 0;
|
errno = 0;
|
||||||
#ifdef HAVE_FDOPENDIR
|
#ifdef HAVE_FDOPENDIR
|
||||||
if (path.fd != -1) {
|
if (path->fd != -1) {
|
||||||
/* closedir() closes the FD, so we duplicate it */
|
/* closedir() closes the FD, so we duplicate it */
|
||||||
Py_BEGIN_ALLOW_THREADS
|
Py_BEGIN_ALLOW_THREADS
|
||||||
fd = dup(path.fd);
|
fd = dup(path->fd);
|
||||||
Py_END_ALLOW_THREADS
|
Py_END_ALLOW_THREADS
|
||||||
|
|
||||||
if (fd == -1) {
|
if (fd == -1) {
|
||||||
|
|
@ -3468,10 +3439,10 @@ _posix_listdir(PyObject *self, PyObject *args, PyObject *kwargs)
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
char *name;
|
char *name;
|
||||||
if (path.narrow) {
|
if (path->narrow) {
|
||||||
name = path.narrow;
|
name = path->narrow;
|
||||||
/* only return bytes if they specified a bytes object */
|
/* only return bytes if they specified a bytes object */
|
||||||
return_str = !(PyBytes_Check(path.object));
|
return_str = !(PyBytes_Check(path->object));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
name = ".";
|
name = ".";
|
||||||
|
|
@ -3484,7 +3455,7 @@ _posix_listdir(PyObject *self, PyObject *args, PyObject *kwargs)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dirp == NULL) {
|
if (dirp == NULL) {
|
||||||
list = path_error(&path);
|
list = path_error(path);
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
if ((list = PyList_New(0)) == NULL) {
|
if ((list = PyList_New(0)) == NULL) {
|
||||||
|
|
@ -3500,7 +3471,7 @@ _posix_listdir(PyObject *self, PyObject *args, PyObject *kwargs)
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
Py_DECREF(list);
|
Py_DECREF(list);
|
||||||
list = path_error(&path);
|
list = path_error(path);
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -3533,8 +3504,6 @@ exit:
|
||||||
Py_END_ALLOW_THREADS
|
Py_END_ALLOW_THREADS
|
||||||
}
|
}
|
||||||
|
|
||||||
path_cleanup(&path);
|
|
||||||
|
|
||||||
return list;
|
return list;
|
||||||
} /* end of _posix_listdir */
|
} /* end of _posix_listdir */
|
||||||
#endif /* which OS */
|
#endif /* which OS */
|
||||||
|
|
@ -3542,11 +3511,31 @@ exit:
|
||||||
static PyObject *
|
static PyObject *
|
||||||
posix_listdir(PyObject *self, PyObject *args, PyObject *kwargs)
|
posix_listdir(PyObject *self, PyObject *args, PyObject *kwargs)
|
||||||
{
|
{
|
||||||
#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
|
path_t path;
|
||||||
return _listdir_windows_no_opendir(self, args, kwargs);
|
PyObject *list = NULL;
|
||||||
#else
|
static char *keywords[] = {"path", NULL};
|
||||||
return _posix_listdir(self, args, kwargs);
|
PyObject *return_value;
|
||||||
|
|
||||||
|
memset(&path, 0, sizeof(path));
|
||||||
|
path.function_name = "listdir";
|
||||||
|
path.nullable = 1;
|
||||||
|
#ifdef HAVE_FDOPENDIR
|
||||||
|
path.allow_fd = 1;
|
||||||
|
path.fd = -1;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&:listdir", keywords,
|
||||||
|
path_converter, &path)) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
|
||||||
|
return_value = _listdir_windows_no_opendir(&path, list);
|
||||||
|
#else
|
||||||
|
return_value = _posix_listdir(&path, list);
|
||||||
|
#endif
|
||||||
|
path_cleanup(&path);
|
||||||
|
return return_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef MS_WINDOWS
|
#ifdef MS_WINDOWS
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue