[Python-checkins] cpython: Issue #11619: _PyImport_LoadDynamicModule() doesn't encode the path to bytes

victor.stinner python-checkins at python.org
Mon Apr 4 23:13:51 CEST 2011


http://hg.python.org/cpython/rev/1b7f484bab6e
changeset:   69145:1b7f484bab6e
user:        Victor Stinner <victor.stinner at haypocalc.com>
date:        Mon Apr 04 23:05:53 2011 +0200
summary:
  Issue #11619: _PyImport_LoadDynamicModule() doesn't encode the path to bytes
on Windows.

files:
  Misc/NEWS            |   3 ++
  Python/dynload_win.c |  33 +++++++++++++++----------------
  Python/importdl.c    |  11 ++++++++++
  3 files changed, 30 insertions(+), 17 deletions(-)


diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
 Core and Builtins
 -----------------
 
+- Issue #11619: _PyImport_LoadDynamicModule() doesn't encode the path to bytes
+  on Windows.
+
 - Issue #10998: Remove mentions of -Q, sys.flags.division_warning and
   Py_DivisionWarningFlag left over from Python 2.
 
diff --git a/Python/dynload_win.c b/Python/dynload_win.c
--- a/Python/dynload_win.c
+++ b/Python/dynload_win.c
@@ -171,8 +171,8 @@
     return NULL;
 }
 
-dl_funcptr _PyImport_GetDynLoadFunc(const char *shortname,
-                                    const char *pathname, FILE *fp)
+dl_funcptr _PyImport_GetDynLoadWindows(const char *shortname,
+                                       PyObject *pathname, FILE *fp)
 {
     dl_funcptr p;
     char funcname[258], *import_python;
@@ -185,8 +185,7 @@
 
     {
         HINSTANCE hDLL = NULL;
-        char pathbuf[260];
-        LPTSTR dummy;
+        wchar_t pathbuf[260];
         unsigned int old_mode;
         ULONG_PTR cookie = 0;
         /* We use LoadLibraryEx so Windows looks for dependent DLLs
@@ -198,14 +197,14 @@
         /* Don't display a message box when Python can't load a DLL */
         old_mode = SetErrorMode(SEM_FAILCRITICALERRORS);
 
-        if (GetFullPathName(pathname,
-                            sizeof(pathbuf),
-                            pathbuf,
-                            &dummy)) {
+        if (GetFullPathNameW(PyUnicode_AS_UNICODE(pathname),
+                             sizeof(pathbuf) / sizeof(pathbuf[0]),
+                             pathbuf,
+                             NULL)) {
             ULONG_PTR cookie = _Py_ActivateActCtx();
             /* XXX This call doesn't exist in Windows CE */
-            hDLL = LoadLibraryEx(pathname, NULL,
-                                 LOAD_WITH_ALTERED_SEARCH_PATH);
+            hDLL = LoadLibraryExW(PyUnicode_AS_UNICODE(pathname), NULL,
+                                  LOAD_WITH_ALTERED_SEARCH_PATH);
             _Py_DeactivateActCtx(cookie);
         }
 
@@ -264,21 +263,21 @@
         } else {
             char buffer[256];
 
+            PyOS_snprintf(buffer, sizeof(buffer),
 #ifdef _DEBUG
-            PyOS_snprintf(buffer, sizeof(buffer), "python%d%d_d.dll",
+                          "python%d%d_d.dll",
 #else
-            PyOS_snprintf(buffer, sizeof(buffer), "python%d%d.dll",
+                          "python%d%d.dll",
 #endif
                           PY_MAJOR_VERSION,PY_MINOR_VERSION);
             import_python = GetPythonImport(hDLL);
 
             if (import_python &&
                 strcasecmp(buffer,import_python)) {
-                PyOS_snprintf(buffer, sizeof(buffer),
-                              "Module use of %.150s conflicts "
-                              "with this version of Python.",
-                              import_python);
-                PyErr_SetString(PyExc_ImportError,buffer);
+                PyErr_Format(PyExc_ImportError,
+                             "Module use of %.150s conflicts "
+                             "with this version of Python.",
+                             import_python);
                 FreeLibrary(hDLL);
                 return NULL;
             }
diff --git a/Python/importdl.c b/Python/importdl.c
--- a/Python/importdl.c
+++ b/Python/importdl.c
@@ -12,8 +12,13 @@
 
 #include "importdl.h"
 
+#ifdef MS_WINDOWS
+extern dl_funcptr _PyImport_GetDynLoadWindows(const char *shortname,
+                                              PyObject *pathname, FILE *fp);
+#else
 extern dl_funcptr _PyImport_GetDynLoadFunc(const char *shortname,
                                            const char *pathname, FILE *fp);
+#endif
 
 /* name should be ASCII only because the C language doesn't accept non-ASCII
    identifiers, and dynamic modules are written in C. */
@@ -22,7 +27,9 @@
 _PyImport_LoadDynamicModule(PyObject *name, PyObject *path, FILE *fp)
 {
     PyObject *m;
+#ifndef MS_WINDOWS
     PyObject *pathbytes;
+#endif
     char *namestr, *lastdot, *shortname, *packagecontext, *oldcontext;
     dl_funcptr p0;
     PyObject* (*p)(void);
@@ -48,12 +55,16 @@
         shortname = lastdot+1;
     }
 
+#ifdef MS_WINDOWS
+    p0 = _PyImport_GetDynLoadWindows(shortname, path, fp);
+#else
     pathbytes = PyUnicode_EncodeFSDefault(path);
     if (pathbytes == NULL)
         return NULL;
     p0 = _PyImport_GetDynLoadFunc(shortname,
                                   PyBytes_AS_STRING(pathbytes), fp);
     Py_DECREF(pathbytes);
+#endif
     p = (PyObject*(*)(void))p0;
     if (PyErr_Occurred())
         return NULL;

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


More information about the Python-checkins mailing list