#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
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};
- int fd = -1;
PyObject *v;
HANDLE hFindFile = INVALID_HANDLE_VALUE;
BOOL result;
PyObject *po = NULL;
wchar_t *wnamebuf = NULL;
- memset(&path, 0, sizeof(path));
- path.function_name = "listdir";
- path.nullable = 1;
-
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&:listdir", keywords,
- path_converter, &path)) {
- return NULL;
- }
-
- if (!path.narrow) {
+ if (!path->narrow) {
WIN32_FIND_DATAW wFileData;
wchar_t *po_wchars;
- if (!path.wide) { /* Default arg: "." */
+ if (!path->wide) { /* Default arg: "." */
po_wchars = L".";
len = 1;
} else {
- po_wchars = path.wide;
- len = wcslen(path.wide);
+ po_wchars = path->wide;
+ len = wcslen(path->wide);
}
/* The +5 is so we can append "\\*.*\0" */
wnamebuf = malloc((len + 5) * sizeof(wchar_t));
if (error == ERROR_FILE_NOT_FOUND)
goto exit;
Py_DECREF(list);
- list = path_error(&path);
+ list = path_error(path);
goto exit;
}
do {
it got to the end of the directory. */
if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
Py_DECREF(list);
- list = path_error(&path);
+ list = path_error(path);
goto exit;
}
} while (result == TRUE);
goto exit;
}
- strcpy(namebuf, path.narrow);
- len = path.length;
+ strcpy(namebuf, path->narrow);
+ len = path->length;
if (len > 0) {
char ch = namebuf[len-1];
if (ch != SEP && ch != ALTSEP && ch != ':')
if (error == ERROR_FILE_NOT_FOUND)
goto exit;
Py_DECREF(list);
- list = path_error(&path);
+ list = path_error(path);
goto exit;
}
do {
it got to the end of the directory. */
if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
Py_DECREF(list);
- list = path_error(&path);
+ list = path_error(path);
goto exit;
}
} while (result == TRUE);
if (FindClose(hFindFile) == FALSE) {
if (list != NULL) {
Py_DECREF(list);
- list = path_error(&path);
+ list = path_error(path);
}
}
}
if (wnamebuf)
free(wnamebuf);
- path_cleanup(&path);
return list;
-
} /* end of _listdir_windows_no_opendir */
#else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */
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;
PyObject *v;
struct dirent *ep;
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;
#ifdef HAVE_FDOPENDIR
- if (path.fd != -1) {
+ if (path->fd != -1) {
/* closedir() closes the FD, so we duplicate it */
Py_BEGIN_ALLOW_THREADS
- fd = dup(path.fd);
+ fd = dup(path->fd);
Py_END_ALLOW_THREADS
if (fd == -1) {
#endif
{
char *name;
- if (path.narrow) {
- name = path.narrow;
+ if (path->narrow) {
+ name = path->narrow;
/* only return bytes if they specified a bytes object */
- return_str = !(PyBytes_Check(path.object));
+ return_str = !(PyBytes_Check(path->object));
}
else {
name = ".";
}
if (dirp == NULL) {
- list = path_error(&path);
+ list = path_error(path);
goto exit;
}
if ((list = PyList_New(0)) == NULL) {
break;
} else {
Py_DECREF(list);
- list = path_error(&path);
+ list = path_error(path);
goto exit;
}
}
Py_END_ALLOW_THREADS
}
- path_cleanup(&path);
-
return list;
} /* end of _posix_listdir */
#endif /* which OS */
static PyObject *
posix_listdir(PyObject *self, PyObject *args, PyObject *kwargs)
{
+ path_t path;
+ PyObject *list = NULL;
+ static char *keywords[] = {"path", NULL};
+ 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
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&:listdir", keywords,
+ path_converter, &path)) {
+ return NULL;
+ }
+
#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
- return _listdir_windows_no_opendir(self, args, kwargs);
+ return_value = _listdir_windows_no_opendir(&path, list);
#else
- return _posix_listdir(self, args, kwargs);
+ return_value = _posix_listdir(&path, list);
#endif
+ path_cleanup(&path);
+ return return_value;
}
#ifdef MS_WINDOWS