[Python-checkins] r83825 - in python/branches/import_unicode: Modules/zipimport.c Python/import.c

victor.stinner python-checkins at python.org
Sun Aug 8 14:30:27 CEST 2010


Author: victor.stinner
Date: Sun Aug  8 14:30:27 2010
New Revision: 83825

Log:
NullImporter_init() uses PyUnicode_FSConverter

To restore the support of bytes path on non Windows OSes. On Windows, it
only accepts unicode path.

Modified:
   python/branches/import_unicode/Modules/zipimport.c
   python/branches/import_unicode/Python/import.c

Modified: python/branches/import_unicode/Modules/zipimport.c
==============================================================================
--- python/branches/import_unicode/Modules/zipimport.c	(original)
+++ python/branches/import_unicode/Modules/zipimport.c	Sun Aug  8 14:30:27 2010
@@ -61,6 +61,7 @@
 zipimporter_init(ZipImporter *self, PyObject *args, PyObject *kwds)
 {
     PyObject *pathbytes;
+    /* FIXME: work on unicode strings, not byte strings */
     char *path, *p, *prefix, buf[MAXPATHLEN+2];
     size_t len;
     PyObject *files;

Modified: python/branches/import_unicode/Python/import.c
==============================================================================
--- python/branches/import_unicode/Python/import.c	(original)
+++ python/branches/import_unicode/Python/import.c	Sun Aug  8 14:30:27 2010
@@ -3845,7 +3845,40 @@
 static int
 NullImporter_init(NullImporter *self, PyObject *args, PyObject *kwds)
 {
+#ifndef MS_WINDOWS
+    PyObject *path;
+    struct stat statbuf;
+    int rv;
+
+    if (!_PyArg_NoKeywords("NullImporter()", kwds))
+        return -1;
+
+    if (!PyArg_ParseTuple(args, "O&:NullImporter",
+                          PyUnicode_FSConverter, &path))
+        return -1;
+
+    if (PyBytes_GET_SIZE(path) == 0) {
+        PyErr_SetString(PyExc_ImportError, "empty pathname");
+        Py_DECREF(path);
+        return -1;
+    }
+
+    rv = stat(PyBytes_AS_STRING(path), &statbuf);
+    Py_DECREF(path);
+    if (rv == 0) {
+        /* it exists */
+        if (S_ISDIR(statbuf.st_mode)) {
+            /* it's a directory */
+            PyErr_SetString(PyExc_ImportError,
+                            "existing directory");
+            return -1;
+        }
+    }
+#else /* MS_WINDOWS */
     PyObject *pathobj;
+    DWORD rv;
+    wchar_t path[MAXPATHLEN+1];
+    Py_ssize_t len;
 
     if (!_PyArg_NoKeywords("NullImporter()", kwds))
         return -1;
@@ -3857,45 +3890,27 @@
     if (PyUnicode_GET_SIZE(pathobj) == 0) {
         PyErr_SetString(PyExc_ImportError, "empty pathname");
         return -1;
-    } else {
-#ifndef MS_WINDOWS
-        struct stat statbuf;
-        int rv;
+    }
 
-        rv = _Py_stat(pathobj, &statbuf);
-        if (rv == 0) {
-            /* it exists */
-            if (S_ISDIR(statbuf.st_mode)) {
-                /* it's a directory */
-                PyErr_SetString(PyExc_ImportError,
-                                "existing directory");
-                return -1;
-            }
-        }
-#else /* MS_WINDOWS */
-        DWORD rv;
-        wchar_t path[MAXPATHLEN+1];
-        Py_ssize_t len;
-        len = PyUnicode_AsWideChar((PyUnicodeObject*)pathobj,
-                                   path, sizeof(path) / sizeof(path[0]));
-        if (len == -1)
+    len = PyUnicode_AsWideChar((PyUnicodeObject*)pathobj,
+                               path, sizeof(path) / sizeof(path[0]));
+    if (len == -1)
+        return -1;
+    /* see issue1293 and issue3677:
+     * stat() on Windows doesn't recognise paths like
+     * "e:\\shared\\" and "\\\\whiterab-c2znlh\\shared" as dirs.
+     */
+    rv = GetFileAttributesW(path);
+    if (rv != INVALID_FILE_ATTRIBUTES) {
+        /* it exists */
+        if (rv & FILE_ATTRIBUTE_DIRECTORY) {
+            /* it's a directory */
+            PyErr_SetString(PyExc_ImportError,
+                            "existing directory");
             return -1;
-        /* see issue1293 and issue3677:
-         * stat() on Windows doesn't recognise paths like
-         * "e:\\shared\\" and "\\\\whiterab-c2znlh\\shared" as dirs.
-         */
-        rv = GetFileAttributesW(path);
-        if (rv != INVALID_FILE_ATTRIBUTES) {
-            /* it exists */
-            if (rv & FILE_ATTRIBUTE_DIRECTORY) {
-                /* it's a directory */
-                PyErr_SetString(PyExc_ImportError,
-                                "existing directory");
-                return -1;
-            }
         }
-#endif
     }
+#endif
     return 0;
 }
 


More information about the Python-checkins mailing list