[Python-checkins] cpython: Refactor the common code out of the posix and windows listdir

gregory.p.smith python-checkins at python.org
Thu Mar 21 05:01:10 CET 2013


http://hg.python.org/cpython/rev/231a7fa6c5be
changeset: 82863:231a7fa6c5be
user: Gregory P. Smith <greg at krypto.org>
date: Wed Mar 20 20:52:50 2013 -0700
summary:
 Refactor the common code out of the posix and windows listdir
implementations from my previous commit into the higher level
function.
files:
 Modules/posixmodule.c | 103 +++++++++++++----------------
 1 files changed, 46 insertions(+), 57 deletions(-)
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -3251,12 +3251,9 @@
 
 #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
 static PyObject *
-_listdir_windows_no_opendir(PyObject *self, PyObject *args, PyObject *kwargs)
-{
- path_t path;
- PyObject *list = NULL;
+_listdir_windows_no_opendir(path_t *path, PyObject *list)
+{
 static char *keywords[] = {"path", NULL};
- int fd = -1;
 PyObject *v;
 HANDLE hFindFile = INVALID_HANDLE_VALUE;
 BOOL result;
@@ -3268,25 +3265,16 @@
 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));
@@ -3312,7 +3300,7 @@
 if (error == ERROR_FILE_NOT_FOUND)
 goto exit;
 Py_DECREF(list);
- list = path_error(&path);
+ list = path_error(path);
 goto exit;
 }
 do {
@@ -3341,15 +3329,15 @@
 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 != ':')
@@ -3368,7 +3356,7 @@
 if (error == ERROR_FILE_NOT_FOUND)
 goto exit;
 Py_DECREF(list);
- list = path_error(&path);
+ list = path_error(path);
 goto exit;
 }
 do {
@@ -3396,7 +3384,7 @@
 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);
@@ -3406,26 +3394,21 @@
 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)
-{
- path_t path;
- PyObject *list = NULL;
- static char *keywords[] = {"path", NULL};
+_posix_listdir(path_t *path, PyObject *list)
+{
 int fd = -1;
 
 PyObject *v;
@@ -3433,24 +3416,12 @@
 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) {
@@ -3468,10 +3439,10 @@
 #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 = ".";
@@ -3484,7 +3455,7 @@
 }
 
 if (dirp == NULL) {
- list = path_error(&path);
+ list = path_error(path);
 goto exit;
 }
 if ((list = PyList_New(0)) == NULL) {
@@ -3500,7 +3471,7 @@
 break;
 } else {
 Py_DECREF(list);
- list = path_error(&path);
+ list = path_error(path);
 goto exit;
 }
 }
@@ -3533,8 +3504,6 @@
 Py_END_ALLOW_THREADS
 }
 
- path_cleanup(&path);
-
 return list;
 } /* end of _posix_listdir */
 #endif /* which OS */
@@ -3542,11 +3511,31 @@
 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);
-#else
- return _posix_listdir(self, args, kwargs);
-#endif
+ 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
-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /