diff Python/import.c @ 68715:bab42673674a

Issue #3080: _PyWin_FindRegisteredModule() returns the path as Unicode * Document the function * Use RegQueryValueW() instead of RegQueryValueA() * Use _Py_fopen() instead of fopen() * Allocate registry key on the heap, not on the stack, and handle memory allocation failure * Handle Python exception in find_module()
author Victor Stinner <victor.stinner@haypocalc.com>
date Tue, 08 Mar 2011 23:49:04 +0100
parents ced52fcd95f6
children ef2b6305d395
line wrap: on
line diff
--- a/Python/import.c	Mon Mar 07 18:34:59 2011 +0100
+++ b/Python/import.c	Tue Mar 08 23:49:04 2011 +0100
@@ -1591,8 +1591,8 @@
    pathname and an open file.  Return NULL if the module is not found. */
 
 #ifdef MS_COREDLL
-extern FILE *_PyWin_FindRegisteredModule(const char *, struct filedescr **,
-                                         char *, Py_ssize_t);
+extern FILE *_PyWin_FindRegisteredModule(PyObject *, struct filedescr **,
+                                         PyObject **p_path);
 #endif
 
 static int case_ok(char *, Py_ssize_t, Py_ssize_t, char *);
@@ -1679,6 +1679,9 @@
     }
 
     if (path == NULL) {
+#ifdef MS_COREDLL
+        PyObject *filename, *filename_bytes;
+#endif
         nameobj = PyUnicode_FromString(name);
         if (nameobj == NULL)
             return NULL;
@@ -1687,14 +1690,24 @@
             strcpy(buf, name);
             return &fd_builtin;
         }
-        Py_DECREF(nameobj);
 #ifdef MS_COREDLL
-        fp = _PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
+        fp = _PyWin_FindRegisteredModule(nameobj, &fdp, &filename);
         if (fp != NULL) {
+            Py_DECREF(nameobj);
+            filename_bytes = PyUnicode_EncodeFSDefault(filename);
+            Py_DECREF(filename);
+            if (filename_bytes == NULL)
+                return NULL;
+            strncpy(buf, PyBytes_AS_STRING(filename_bytes), buflen);
+            buf[buflen-1] = '\0';
+            Py_DECREF(filename_bytes);
             *p_fp = fp;
             return fdp;
         }
+        else if (PyErr_Occurred())
+            return NULL;
 #endif
+        Py_DECREF(nameobj);
         path = PySys_GetObject("path");
     }