Skip to content

Commit 2d32227

Browse files
author
Victor Stinner
committed
Issue python#11619: _PyImport_LoadDynamicModule() doesn't encode the path to bytes
on Windows.
1 parent 54e7135 commit 2d32227

3 files changed

Lines changed: 30 additions & 17 deletions

File tree

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.3 Alpha 1?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #11619: _PyImport_LoadDynamicModule() doesn't encode the path to bytes
14+
on Windows.
15+
1316
- Issue #10998: Remove mentions of -Q, sys.flags.division_warning and
1417
Py_DivisionWarningFlag left over from Python 2.
1518

Python/dynload_win.c

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ static char *GetPythonImport (HINSTANCE hModule)
171171
return NULL;
172172
}
173173

174-
dl_funcptr _PyImport_GetDynLoadFunc(const char *shortname,
175-
const char *pathname, FILE *fp)
174+
dl_funcptr _PyImport_GetDynLoadWindows(const char *shortname,
175+
PyObject *pathname, FILE *fp)
176176
{
177177
dl_funcptr p;
178178
char funcname[258], *import_python;
@@ -185,8 +185,7 @@ dl_funcptr _PyImport_GetDynLoadFunc(const char *shortname,
185185

186186
{
187187
HINSTANCE hDLL = NULL;
188-
char pathbuf[260];
189-
LPTSTR dummy;
188+
wchar_t pathbuf[260];
190189
unsigned int old_mode;
191190
ULONG_PTR cookie = 0;
192191
/* We use LoadLibraryEx so Windows looks for dependent DLLs
@@ -198,14 +197,14 @@ dl_funcptr _PyImport_GetDynLoadFunc(const char *shortname,
198197
/* Don't display a message box when Python can't load a DLL */
199198
old_mode = SetErrorMode(SEM_FAILCRITICALERRORS);
200199

201-
if (GetFullPathName(pathname,
202-
sizeof(pathbuf),
203-
pathbuf,
204-
&dummy)) {
200+
if (GetFullPathNameW(PyUnicode_AS_UNICODE(pathname),
201+
sizeof(pathbuf) / sizeof(pathbuf[0]),
202+
pathbuf,
203+
NULL)) {
205204
ULONG_PTR cookie = _Py_ActivateActCtx();
206205
/* XXX This call doesn't exist in Windows CE */
207-
hDLL = LoadLibraryEx(pathname, NULL,
208-
LOAD_WITH_ALTERED_SEARCH_PATH);
206+
hDLL = LoadLibraryExW(PyUnicode_AS_UNICODE(pathname), NULL,
207+
LOAD_WITH_ALTERED_SEARCH_PATH);
209208
_Py_DeactivateActCtx(cookie);
210209
}
211210

@@ -264,21 +263,21 @@ dl_funcptr _PyImport_GetDynLoadFunc(const char *shortname,
264263
} else {
265264
char buffer[256];
266265

266+
PyOS_snprintf(buffer, sizeof(buffer),
267267
#ifdef _DEBUG
268-
PyOS_snprintf(buffer, sizeof(buffer), "python%d%d_d.dll",
268+
"python%d%d_d.dll",
269269
#else
270-
PyOS_snprintf(buffer, sizeof(buffer), "python%d%d.dll",
270+
"python%d%d.dll",
271271
#endif
272272
PY_MAJOR_VERSION,PY_MINOR_VERSION);
273273
import_python = GetPythonImport(hDLL);
274274

275275
if (import_python &&
276276
strcasecmp(buffer,import_python)) {
277-
PyOS_snprintf(buffer, sizeof(buffer),
278-
"Module use of %.150s conflicts "
279-
"with this version of Python.",
280-
import_python);
281-
PyErr_SetString(PyExc_ImportError,buffer);
277+
PyErr_Format(PyExc_ImportError,
278+
"Module use of %.150s conflicts "
279+
"with this version of Python.",
280+
import_python);
282281
FreeLibrary(hDLL);
283282
return NULL;
284283
}

Python/importdl.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@
1212

1313
#include "importdl.h"
1414

15+
#ifdef MS_WINDOWS
16+
extern dl_funcptr _PyImport_GetDynLoadWindows(const char *shortname,
17+
PyObject *pathname, FILE *fp);
18+
#else
1519
extern dl_funcptr _PyImport_GetDynLoadFunc(const char *shortname,
1620
const char *pathname, FILE *fp);
21+
#endif
1722

1823
/* name should be ASCII only because the C language doesn't accept non-ASCII
1924
identifiers, and dynamic modules are written in C. */
@@ -22,7 +27,9 @@ PyObject *
2227
_PyImport_LoadDynamicModule(PyObject *name, PyObject *path, FILE *fp)
2328
{
2429
PyObject *m;
30+
#ifndef MS_WINDOWS
2531
PyObject *pathbytes;
32+
#endif
2633
char *namestr, *lastdot, *shortname, *packagecontext, *oldcontext;
2734
dl_funcptr p0;
2835
PyObject* (*p)(void);
@@ -48,12 +55,16 @@ _PyImport_LoadDynamicModule(PyObject *name, PyObject *path, FILE *fp)
4855
shortname = lastdot+1;
4956
}
5057

58+
#ifdef MS_WINDOWS
59+
p0 = _PyImport_GetDynLoadWindows(shortname, path, fp);
60+
#else
5161
pathbytes = PyUnicode_EncodeFSDefault(path);
5262
if (pathbytes == NULL)
5363
return NULL;
5464
p0 = _PyImport_GetDynLoadFunc(shortname,
5565
PyBytes_AS_STRING(pathbytes), fp);
5666
Py_DECREF(pathbytes);
67+
#endif
5768
p = (PyObject*(*)(void))p0;
5869
if (PyErr_Occurred())
5970
return NULL;

0 commit comments

Comments
 (0)