[Python-checkins] cpython: Issue #15478: Use path_error() in more posix functions, especially in Windows

victor.stinner python-checkins at python.org
Wed Oct 31 22:25:38 CET 2012


http://hg.python.org/cpython/rev/13ebaa36d87d
changeset:   80137:13ebaa36d87d
parent:      80135:016d1a1fd601
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Wed Oct 31 22:24:06 2012 +0100
summary:
  Issue #15478: Use path_error() in more posix functions, especially in Windows
implementation

files:
  Modules/posixmodule.c |  93 ++++++++-----------------------
  1 files changed, 24 insertions(+), 69 deletions(-)


diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -1035,17 +1035,6 @@
 }
 
 static PyObject *
-win32_error_unicode(char* function, wchar_t* filename)
-{
-    /* XXX - see win32_error for comments on 'function' */
-    errno = GetLastError();
-    if (filename)
-        return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
-    else
-        return PyErr_SetFromWindowsErr(errno);
-}
-
-static PyObject *
 win32_error_object(char* function, PyObject* filename)
 {
     /* XXX - see win32_error for comments on 'function' */
@@ -1119,44 +1108,6 @@
 
 
 #ifdef MS_WINDOWS
-static PyObject*
-win32_1str(PyObject* args, char* func,
-           char* format, BOOL (__stdcall *funcA)(LPCSTR),
-           char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
-{
-    PyObject *uni;
-    const char *ansi;
-    BOOL result;
-
-    if (PyArg_ParseTuple(args, wformat, &uni))
-    {
-        wchar_t *wstr = PyUnicode_AsUnicode(uni);
-        if (wstr == NULL)
-            return NULL;
-        Py_BEGIN_ALLOW_THREADS
-        result = funcW(wstr);
-        Py_END_ALLOW_THREADS
-        if (!result)
-            return win32_error_object(func, uni);
-        Py_INCREF(Py_None);
-        return Py_None;
-    }
-    PyErr_Clear();
-
-    if (!PyArg_ParseTuple(args, format, &ansi))
-        return NULL;
-    if (win32_warn_bytes_api())
-        return NULL;
-    Py_BEGIN_ALLOW_THREADS
-    result = funcA(ansi);
-    Py_END_ALLOW_THREADS
-    if (!result)
-        return win32_error(func, ansi);
-    Py_INCREF(Py_None);
-    return Py_None;
-
-}
-
 /* This is a reimplementation of the C library's chdir function,
    but one that produces Win32 errors instead of DOS error codes.
    chdir is essentially a wrapper around SetCurrentDirectory; however,
@@ -2533,7 +2484,7 @@
     Py_END_ALLOW_THREADS
 
     if (!result) {
-        return_value = win32_error_object("chmod", path.object);
+        return_value = path_error(&path);
         goto exit;
     }
 #else /* MS_WINDOWS */
@@ -2989,11 +2940,13 @@
             return NULL;
         }
         if (!len) {
-            if (wbuf2 != wbuf) free(wbuf2);
-            return win32_error("getcwdu", NULL);
+            if (wbuf2 != wbuf)
+                free(wbuf2);
+            return PyErr_SetFromWindowsErr(0);
         }
         resobj = PyUnicode_FromWideChar(wbuf2, len);
-        if (wbuf2 != wbuf) free(wbuf2);
+        if (wbuf2 != wbuf)
+            free(wbuf2);
         return resobj;
     }
 
@@ -3101,7 +3054,7 @@
     Py_END_ALLOW_THREADS
 
     if (!result) {
-        return_value = win32_error_object("link", dst.object);
+        return_value = path_error(&dst);
         goto exit;
     }
 #else
@@ -3225,8 +3178,7 @@
             if (error == ERROR_FILE_NOT_FOUND)
                 goto exit;
             Py_DECREF(list);
-            list = NULL;
-            win32_error_unicode("FindFirstFileW", wnamebuf);
+            list = path_error(&path);
             goto exit;
         }
         do {
@@ -3255,7 +3207,7 @@
                it got to the end of the directory. */
             if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
                 Py_DECREF(list);
-                list = win32_error_unicode("FindNextFileW", wnamebuf);
+                list = path_error(&path);
                 goto exit;
             }
         } while (result == TRUE);
@@ -3282,7 +3234,8 @@
         if (error == ERROR_FILE_NOT_FOUND)
             goto exit;
         Py_DECREF(list);
-        list = win32_error("FindFirstFile", namebuf);
+        path.func = "FindFirstFile";
+        list = path_error(&path);
         goto exit;
     }
     do {
@@ -3310,7 +3263,8 @@
            it got to the end of the directory. */
         if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
             Py_DECREF(list);
-            list = win32_error("FindNextFile", namebuf);
+            path.func = "FindNextFile";
+            list = path_error(&path);
             goto exit;
         }
     } while (result == TRUE);
@@ -3320,7 +3274,7 @@
         if (FindClose(hFindFile) == FALSE) {
             if (list != NULL) {
                 Py_DECREF(list);
-                list = win32_error_object("FindClose", path.object);
+                list = path_error(&path);
             }
         }
     }
@@ -3569,7 +3523,7 @@
         return posix_error();
 
     if (!GetFileInformationByHandle(hFile, &info))
-        return win32_error("_getfileinformation", NULL);
+        return PyErr_SetFromWindowsErr(0);
 
     return Py_BuildValue("iii", info.dwVolumeSerialNumber,
                                 info.nFileIndexHigh,
@@ -3658,7 +3612,7 @@
     Py_END_ALLOW_THREADS
 
     if (!result) {
-        return_value = win32_error_object("mkdir", path.object);
+        return_value = path_error(&path);
         goto exit;
     }
 #else
@@ -3828,7 +3782,7 @@
     Py_END_ALLOW_THREADS
 
     if (!result) {
-        return_value = win32_error_object(function_name, dst.object);
+        return_value = path_error(&dst);
         goto exit;
     }
 
@@ -4396,6 +4350,7 @@
     PyObject *return_value = NULL;
 
     memset(&path, 0, sizeof(path));
+    path.function_name = "utime";
 #if UTIME_HAVE_FD
     path.allow_fd = 1;
 #endif
@@ -4484,7 +4439,7 @@
                             FILE_FLAG_BACKUP_SEMANTICS, NULL);
     Py_END_ALLOW_THREADS
     if (hFile == INVALID_HANDLE_VALUE) {
-        win32_error_object("utime", path.object);
+        path_error(&path);
         goto exit;
     }
 
@@ -4493,7 +4448,7 @@
         GetSystemTime(&now);
         if (!SystemTimeToFileTime(&now, &mtime) ||
             !SystemTimeToFileTime(&now, &atime)) {
-            win32_error("utime", NULL);
+            PyErr_SetFromWindowsErr(0);
             goto exit;
         }
     }
@@ -4506,7 +4461,7 @@
            as that may confuse the user into believing that
            something is wrong with the file, when it also
            could be the time stamp that gives a problem. */
-        win32_error("utime", NULL);
+        PyErr_SetFromWindowsErr(0);
         goto exit;
     }
 #else /* MS_WINDOWS */
@@ -6736,7 +6691,7 @@
     Py_END_ALLOW_THREADS
 
     if (!result) {
-        return_value = win32_error_object("symlink", src.object);
+        return_value = path_error(&src);
         goto exit;
     }
 
@@ -7648,7 +7603,7 @@
     Py_END_ALLOW_THREADS
     if (res != 0) {
 #ifdef MS_WINDOWS
-        return win32_error("fstat", NULL);
+        return PyErr_SetFromWindowsErr(0);
 #else
         return posix_error();
 #endif
@@ -7694,7 +7649,7 @@
     BOOL ok;
     ok = CreatePipe(&read, &write, NULL, 0);
     if (!ok)
-        return win32_error("CreatePipe", NULL);
+        return PyErr_SetFromWindowsErr(0);
     read_fd = _open_osfhandle((Py_intptr_t)read, 0);
     write_fd = _open_osfhandle((Py_intptr_t)write, 1);
     return Py_BuildValue("(ii)", read_fd, write_fd);

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list