[Python-checkins] r82695 - in python/branches/import_unicode: Doc/library/sys.rst Python/import.c

victor.stinner python-checkins at python.org
Fri Jul 9 01:32:57 CEST 2010


Author: victor.stinner
Date: Fri Jul  9 01:32:57 2010
New Revision: 82695

Log:
_find_module() fills directly path argument

 * find_module(): rename "path" argument to "search_path"
 * find_module(): result can be NULL
 * reject bytes path
 * fix refleak

Modified:
   python/branches/import_unicode/Doc/library/sys.rst
   python/branches/import_unicode/Python/import.c

Modified: python/branches/import_unicode/Doc/library/sys.rst
==============================================================================
--- python/branches/import_unicode/Doc/library/sys.rst	(original)
+++ python/branches/import_unicode/Doc/library/sys.rst	Fri Jul  9 01:32:57 2010
@@ -599,9 +599,9 @@
 
    .. index:: triple: module; search; path
 
-   A list of strings that specifies the search path for modules. Initialized from
-   the environment variable :envvar:`PYTHONPATH`, plus an installation-dependent
-   default.
+   A list of unicode strings that specifies the search path for modules.
+   Initialized from the environment variable :envvar:`PYTHONPATH`, plus an
+   installation-dependent default.
 
    As initialized upon program startup, the first item of this list, ``path[0]``,
    is the directory containing the script that was used to invoke the Python

Modified: python/branches/import_unicode/Python/import.c
==============================================================================
--- python/branches/import_unicode/Python/import.c	(original)
+++ python/branches/import_unicode/Python/import.c	Fri Jul  9 01:32:57 2010
@@ -1477,7 +1477,7 @@
 {
     PyObject *m, *d;
     PyObject *file = NULL;
-    PyObject *path = NULL;
+    PyObject *search_path = NULL;
     int err;
     FILE *fp = NULL;
     struct filedescr *fdp;
@@ -1492,15 +1492,15 @@
     file = get_sourcefile(pathobj);
     if (file == NULL)
         goto error;
-    path = Py_BuildValue("[O]", file);
-    if (path == NULL)
+    search_path = Py_BuildValue("[O]", file);
+    if (search_path == NULL)
         goto error;
     err = PyDict_SetItemString(d, "__file__", file);
     if (err == 0)
-        err = PyDict_SetItemString(d, "__path__", path);
+        err = PyDict_SetItemString(d, "__path__", search_path);
     if (err != 0)
         goto error;
-    fdp = find_module(name, "__init__", path, &bufobj, &fp, NULL);
+    fdp = find_module(name, "__init__", search_path, &bufobj, &fp, NULL);
     if (fdp == NULL) {
         if (PyErr_ExceptionMatches(PyExc_ImportError)) {
             PyErr_Clear();
@@ -1511,6 +1511,7 @@
         goto cleanup;
     }
     m = load_module(name, fp, bufobj, fdp->type, NULL);
+    Py_XDECREF(bufobj);
     if (fp != NULL)
         fclose(fp);
     goto cleanup;
@@ -1518,7 +1519,7 @@
   error:
     m = NULL;
   cleanup:
-    Py_XDECREF(path);
+    Py_XDECREF(search_path);
     Py_XDECREF(file);
     return m;
 }
@@ -1634,8 +1635,9 @@
 static struct filedescr importhookdescr = {"", "", IMP_HOOK};
 
 static struct filedescr *
-_find_module(char *fullname, char *subname, PyObject *path, char *buf,
-            size_t buflen, FILE **p_fp, PyObject **p_loader)
+_find_module(char *fullname, char *subname, PyObject *search_path,
+            char *buf, size_t buflen, PyObject **path,
+            FILE **p_fp, PyObject **p_loader)
 {
     Py_ssize_t i, npath;
     size_t len, namelen;
@@ -1653,6 +1655,8 @@
     size_t saved_namelen;
     char *saved_buf = NULL;
 #endif
+
+    *path = NULL;
     if (p_loader != NULL)
         *p_loader = NULL;
 
@@ -1681,8 +1685,8 @@
             PyObject *hook = PyList_GetItem(meta_path, i);
             loader = PyObject_CallMethod(hook, "find_module",
                                          "sO", fullname,
-                                         path != NULL ?
-                                         path : Py_None);
+                                         search_path != NULL ?
+                                         search_path : Py_None);
             if (loader == NULL) {
                 Py_DECREF(meta_path);
                 return NULL;  /* true error */
@@ -1699,26 +1703,35 @@
     }
 
     if (find_frozen(fullname) != NULL) {
-        strcpy(buf, fullname);
+        *path = PyUnicode_DecodeFSDefault(fullname);
+        if (*path == NULL)
+            return NULL;
         return &fd_frozen;
     }
 
-    if (path == NULL) {
+    if (search_path == NULL) {
         if (is_builtin(name)) {
-            strcpy(buf, name);
+            *path = PyUnicode_DecodeFSDefault(name);
+            if (*path == NULL)
+                return NULL;
             return &fd_builtin;
         }
 #ifdef MS_COREDLL
         fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
         if (fp != NULL) {
+            *path = PyUnicode_DecodeFSDefault(buf);
+            if (*path == NULL) {
+                fclose(fp);
+                return NULL;
+            }
             *p_fp = fp;
             return fdp;
         }
 #endif
-        path = PySys_GetObject("path");
+        search_path = PySys_GetObject("path");
     }
 
-    if (path == NULL || !PyList_Check(path)) {
+    if (search_path == NULL || !PyList_Check(search_path)) {
         PyErr_SetString(PyExc_ImportError,
                         "sys.path must be a list of directory names");
         return NULL;
@@ -1739,10 +1752,10 @@
         return NULL;
     }
 
-    npath = PyList_Size(path);
+    npath = PyList_Size(search_path);
     namelen = strlen(name);
     for (i = 0; i < npath; i++) {
-        PyObject *v = PyList_GetItem(path, i);
+        PyObject *v = PyList_GetItem(search_path, i);
         PyObject *origv = v;
         const char *base;
         Py_ssize_t size;
@@ -1753,10 +1766,8 @@
             if (v == NULL)
                 return NULL;
         }
-        else if (!PyBytes_Check(v))
-            continue;
         else
-            Py_INCREF(v);
+            continue;
 
         base = PyBytes_AS_STRING(v);
         size = PyBytes_GET_SIZE(v);
@@ -1816,6 +1827,9 @@
             S_ISDIR(statbuf.st_mode) &&         /* it's a directory */
             case_ok(buf, len, namelen, name)) { /* case matches */
             if (find_init_module(buf)) { /* and has __init__.py */
+                *path = PyUnicode_DecodeFSDefault(buf);
+                if (*path == NULL)
+                    return NULL;
                 return &fd_package;
             }
             else {
@@ -1900,29 +1914,25 @@
                      "No module named %.200s", name);
         return NULL;
     }
+    *path = PyUnicode_DecodeFSDefault(buf);
+    if (*path == NULL) {
+        fclose(fp);
+        return NULL;
+    }
     *p_fp = fp;
     return fdp;
 }
 
 static struct filedescr *
-find_module(char *fullname, char *subname, PyObject *path, PyObject **result,
+find_module(char *fullname, char *subname, PyObject *search_path, PyObject **path,
             FILE **p_fp, PyObject **p_loader)
 {
     char buf[MAXPATHLEN+1];
     struct filedescr *fd;
     buf[0] = '\0';
-    fd = _find_module(fullname, subname, path,
-                      buf, sizeof(buf),
+    fd = _find_module(fullname, subname, search_path,
+                      buf, sizeof(buf), path,
                       p_fp, p_loader);
-    if (fd != NULL) {
-        *result = PyUnicode_DecodeFSDefault(buf);
-        if (*result == NULL) {
-            if (*p_fp != NULL)
-                fclose(*p_fp);
-            return NULL;
-        }
-    } else
-        *result = NULL;
     return fd;
 }
 
@@ -2206,6 +2216,11 @@
         }
     }
 
+    if (pathobj == NULL && type != IMP_HOOK) {
+        PyErr_BadInternalCall();
+        return NULL;
+    }
+
     switch (type) {
 
     case PY_SOURCE:
@@ -2235,7 +2250,7 @@
         if (loader == NULL) {
             PyErr_SetString(PyExc_ImportError,
                             "import hook without loader");
-            Py_DECREF(pathobj);
+            Py_XDECREF(pathobj);
             return NULL;
         }
         m = PyObject_CallMethod(loader, "load_module", "s", name);
@@ -2984,24 +2999,24 @@
         Py_INCREF(m);
     }
     else {
-        PyObject *path, *loader = NULL;
+        PyObject *search_path, *loader = NULL;
         struct filedescr *fdp;
         FILE *fp = NULL;
 
         if (mod == Py_None)
-            path = NULL;
+            search_path = NULL;
         else {
-            path = PyObject_GetAttrString(mod, "__path__");
-            if (path == NULL) {
+            search_path = PyObject_GetAttrString(mod, "__path__");
+            if (search_path == NULL) {
                 PyErr_Clear();
                 Py_INCREF(Py_None);
                 return Py_None;
             }
         }
 
-        fdp = find_module(fullname, subname, path, &pathobj,
+        fdp = find_module(fullname, subname, search_path, &pathobj,
                           &fp, &loader);
-        Py_XDECREF(path);
+        Py_XDECREF(search_path);
         if (fdp == NULL) {
             if (!PyErr_ExceptionMatches(PyExc_ImportError))
                 return NULL;
@@ -3010,7 +3025,7 @@
             return Py_None;
         }
         m = load_module(fullname, fp, pathobj, fdp->type, loader);
-        Py_DECREF(pathobj);
+        Py_XDECREF(pathobj);
         Py_XDECREF(loader);
         if (fp)
             fclose(fp);
@@ -3033,7 +3048,7 @@
     PyInterpreterState *interp = PyThreadState_Get()->interp;
     PyObject *modules_reloading = interp->modules_reloading;
     PyObject *modules = PyImport_GetModuleDict();
-    PyObject *path = NULL, *loader = NULL, *existing_m = NULL;
+    PyObject *search_path = NULL, *loader = NULL, *existing_m = NULL;
     char *name, *subname;
     struct filedescr *fdp;
     FILE *fp = NULL;
@@ -3091,12 +3106,12 @@
         }
         Py_DECREF(parentname);
         subname++;
-        path = PyObject_GetAttrString(parent, "__path__");
-        if (path == NULL)
+        search_path = PyObject_GetAttrString(parent, "__path__");
+        if (search_path == NULL)
             PyErr_Clear();
     }
-    fdp = find_module(name, subname, path, &pathobj, &fp, &loader);
-    Py_XDECREF(path);
+    fdp = find_module(name, subname, search_path, &pathobj, &fp, &loader);
+    Py_XDECREF(search_path);
 
     if (fdp == NULL) {
         Py_XDECREF(loader);
@@ -3105,7 +3120,7 @@
     }
 
     newm = load_module(name, fp, pathobj, fdp->type, loader);
-    Py_DECREF(pathobj);
+    Py_XDECREF(pathobj);
     Py_XDECREF(loader);
 
     if (fp)
@@ -3256,7 +3271,7 @@
 }
 
 static PyObject *
-call_find_module(char *name, PyObject *path)
+call_find_module(char *name, PyObject *search_path)
 {
     extern int fclose(FILE *);
     PyObject *fob, *ret;
@@ -3267,9 +3282,9 @@
     char *found_encoding = NULL;
     char *encoding = NULL;
 
-    if (path == Py_None)
-        path = NULL;
-    fdp = find_module(NULL, name, path, &pathobj, &fp, NULL);
+    if (search_path == Py_None)
+        search_path = NULL;
+    fdp = find_module(NULL, name, search_path, &pathobj, &fp, NULL);
     if (fdp == NULL)
         return NULL;
     if (fp != NULL) {
@@ -3286,7 +3301,7 @@
             found_encoding = PyTokenizer_FindEncoding(fd);
             lseek(fd, 0, 0); /* Reset position */
             if (found_encoding == NULL && PyErr_Occurred()) {
-                Py_DECREF(pathobj);
+                Py_XDECREF(pathobj);
                 return NULL;
             }
             encoding = (found_encoding != NULL) ? found_encoding :
@@ -3296,7 +3311,7 @@
                                     encoding, NULL, NULL, 1);
         if (fob == NULL) {
             close(fd);
-            Py_DECREF(pathobj);
+            Py_XDECREF(pathobj);
             PyMem_FREE(found_encoding);
             return NULL;
         }
@@ -3306,7 +3321,7 @@
         Py_INCREF(fob);
     }
     ret = Py_BuildValue("NN(ssi)",
-                        fob, pathobj,
+                        fob, (pathobj != NULL) ? pathobj : Py_None,
                         fdp->suffix, fdp->mode, fdp->type);
     PyMem_FREE(found_encoding);
 


More information about the Python-checkins mailing list