mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
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()
This commit is contained in:
parent
db536afee1
commit
4d6c1c476a
2 changed files with 88 additions and 54 deletions
121
PC/import_nt.c
121
PC/import_nt.c
|
@ -15,72 +15,93 @@
|
||||||
/* a string loaded from the DLL at startup */
|
/* a string loaded from the DLL at startup */
|
||||||
extern const char *PyWin_DLLVersionString;
|
extern const char *PyWin_DLLVersionString;
|
||||||
|
|
||||||
FILE *_PyWin_FindRegisteredModule(const char *moduleName,
|
/* Find a module on Windows.
|
||||||
struct filedescr **ppFileDesc,
|
|
||||||
char *pathBuf,
|
Read the registry Software\Python\PythonCore\<version>\Modules\<name> (or
|
||||||
Py_ssize_t pathLen)
|
Software\Python\PythonCore\<version>\Modules\<name>\Debug in debug mode)
|
||||||
|
from HKEY_CURRENT_USER, or HKEY_LOCAL_MACHINE. Find the file descriptor using
|
||||||
|
the file extension. Open the file.
|
||||||
|
|
||||||
|
On success, write the file descriptor into *ppFileDesc, the module path
|
||||||
|
(Unicode object) into *pPath, and return the opened file object. If the
|
||||||
|
module cannot be found (e.g. no registry key or the file doesn't exist),
|
||||||
|
return NULL. On error, raise a Python exception and return NULL.
|
||||||
|
*/
|
||||||
|
FILE *
|
||||||
|
_PyWin_FindRegisteredModule(PyObject *moduleName,
|
||||||
|
struct filedescr **ppFileDesc,
|
||||||
|
PyObject **pPath)
|
||||||
{
|
{
|
||||||
char *moduleKey;
|
wchar_t pathBuf[MAXPATHLEN+1];
|
||||||
const char keyPrefix[] = "Software\\Python\\PythonCore\\";
|
int pathLen = MAXPATHLEN+1;
|
||||||
const char keySuffix[] = "\\Modules\\";
|
PyObject *path, *moduleKey, *suffix;
|
||||||
#ifdef _DEBUG
|
struct filedescr *fdp;
|
||||||
/* In debugging builds, we _must_ have the debug version
|
HKEY keyBase;
|
||||||
* registered.
|
|
||||||
*/
|
|
||||||
const char debugString[] = "\\Debug";
|
|
||||||
#else
|
|
||||||
const char debugString[] = "";
|
|
||||||
#endif
|
|
||||||
struct filedescr *fdp = NULL;
|
|
||||||
FILE *fp;
|
|
||||||
HKEY keyBase = HKEY_CURRENT_USER;
|
|
||||||
int modNameSize;
|
int modNameSize;
|
||||||
long regStat;
|
long regStat;
|
||||||
|
Py_ssize_t extLen;
|
||||||
|
FILE *fp;
|
||||||
|
|
||||||
/* Calculate the size for the sprintf buffer.
|
moduleKey = PyUnicode_FromFormat(
|
||||||
* Get the size of the chars only, plus 1 NULL.
|
#ifdef _DEBUG
|
||||||
*/
|
/* In debugging builds, we _must_ have the debug version registered */
|
||||||
size_t bufSize = sizeof(keyPrefix)-1 +
|
"Software\\Python\\PythonCore\\%s\\Modules\\%U\\Debug",
|
||||||
strlen(PyWin_DLLVersionString) +
|
#else
|
||||||
sizeof(keySuffix) +
|
"Software\\Python\\PythonCore\\%s\\Modules\\%U",
|
||||||
strlen(moduleName) +
|
#endif
|
||||||
sizeof(debugString) - 1;
|
PyWin_DLLVersionString, moduleName);
|
||||||
/* alloca == no free required, but memory only local to fn,
|
if (moduleKey == NULL)
|
||||||
* also no heap fragmentation!
|
return NULL;
|
||||||
*/
|
|
||||||
moduleKey = alloca(bufSize);
|
|
||||||
PyOS_snprintf(moduleKey, bufSize,
|
|
||||||
"Software\\Python\\PythonCore\\%s\\Modules\\%s%s",
|
|
||||||
PyWin_DLLVersionString, moduleName, debugString);
|
|
||||||
|
|
||||||
assert(pathLen < INT_MAX);
|
keyBase = HKEY_CURRENT_USER;
|
||||||
modNameSize = (int)pathLen;
|
modNameSize = pathLen;
|
||||||
regStat = RegQueryValue(keyBase, moduleKey, pathBuf, &modNameSize);
|
regStat = RegQueryValueW(keyBase, PyUnicode_AS_UNICODE(moduleKey),
|
||||||
|
pathBuf, &modNameSize);
|
||||||
if (regStat != ERROR_SUCCESS) {
|
if (regStat != ERROR_SUCCESS) {
|
||||||
/* No user setting - lookup in machine settings */
|
/* No user setting - lookup in machine settings */
|
||||||
keyBase = HKEY_LOCAL_MACHINE;
|
keyBase = HKEY_LOCAL_MACHINE;
|
||||||
/* be anal - failure may have reset size param */
|
/* be anal - failure may have reset size param */
|
||||||
modNameSize = (int)pathLen;
|
modNameSize = pathLen;
|
||||||
regStat = RegQueryValue(keyBase, moduleKey,
|
regStat = RegQueryValueW(keyBase, PyUnicode_AS_UNICODE(moduleKey),
|
||||||
pathBuf, &modNameSize);
|
pathBuf, &modNameSize);
|
||||||
|
if (regStat != ERROR_SUCCESS) {
|
||||||
if (regStat != ERROR_SUCCESS)
|
Py_DECREF(moduleKey);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Py_DECREF(moduleKey);
|
||||||
|
if (modNameSize < 3) {
|
||||||
|
/* path shorter than "a.o" or negative length (cast to
|
||||||
|
size_t is wrong) */
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
/* use the file extension to locate the type entry. */
|
/* use the file extension to locate the type entry. */
|
||||||
for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
|
for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
|
||||||
size_t extLen = strlen(fdp->suffix);
|
suffix = PyUnicode_FromString(fdp->suffix);
|
||||||
assert(modNameSize >= 0); /* else cast to size_t is wrong */
|
if (suffix == NULL)
|
||||||
if ((size_t)modNameSize > extLen &&
|
return NULL;
|
||||||
strnicmp(pathBuf + ((size_t)modNameSize-extLen-1),
|
extLen = PyUnicode_GET_SIZE(suffix);
|
||||||
fdp->suffix,
|
if ((Py_ssize_t)modNameSize > extLen &&
|
||||||
extLen) == 0)
|
_wcsnicmp(pathBuf + ((Py_ssize_t)modNameSize-extLen-1),
|
||||||
|
PyUnicode_AS_UNICODE(suffix),
|
||||||
|
extLen) == 0)
|
||||||
|
{
|
||||||
|
Py_DECREF(suffix);
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
Py_DECREF(suffix);
|
||||||
}
|
}
|
||||||
if (fdp->suffix == NULL)
|
if (fdp->suffix == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
fp = fopen(pathBuf, fdp->mode);
|
path = PyUnicode_FromUnicode(pathBuf, wcslen(pathBuf));
|
||||||
if (fp != NULL)
|
if (path == NULL)
|
||||||
*ppFileDesc = fdp;
|
return NULL;
|
||||||
|
fp = _Py_fopen(path, fdp->mode);
|
||||||
|
if (fp == NULL) {
|
||||||
|
Py_DECREF(path);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
*pPath = path;
|
||||||
|
*ppFileDesc = fdp;
|
||||||
return fp;
|
return fp;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1591,8 +1591,8 @@ PyImport_GetImporter(PyObject *path) {
|
||||||
pathname and an open file. Return NULL if the module is not found. */
|
pathname and an open file. Return NULL if the module is not found. */
|
||||||
|
|
||||||
#ifdef MS_COREDLL
|
#ifdef MS_COREDLL
|
||||||
extern FILE *_PyWin_FindRegisteredModule(const char *, struct filedescr **,
|
extern FILE *_PyWin_FindRegisteredModule(PyObject *, struct filedescr **,
|
||||||
char *, Py_ssize_t);
|
PyObject **p_path);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static int case_ok(char *, Py_ssize_t, Py_ssize_t, char *);
|
static int case_ok(char *, Py_ssize_t, Py_ssize_t, char *);
|
||||||
|
@ -1679,6 +1679,9 @@ find_module(char *fullname, char *subname, PyObject *path, char *buf,
|
||||||
}
|
}
|
||||||
|
|
||||||
if (path == NULL) {
|
if (path == NULL) {
|
||||||
|
#ifdef MS_COREDLL
|
||||||
|
PyObject *filename, *filename_bytes;
|
||||||
|
#endif
|
||||||
nameobj = PyUnicode_FromString(name);
|
nameobj = PyUnicode_FromString(name);
|
||||||
if (nameobj == NULL)
|
if (nameobj == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -1687,14 +1690,24 @@ find_module(char *fullname, char *subname, PyObject *path, char *buf,
|
||||||
strcpy(buf, name);
|
strcpy(buf, name);
|
||||||
return &fd_builtin;
|
return &fd_builtin;
|
||||||
}
|
}
|
||||||
Py_DECREF(nameobj);
|
|
||||||
#ifdef MS_COREDLL
|
#ifdef MS_COREDLL
|
||||||
fp = _PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
|
fp = _PyWin_FindRegisteredModule(nameobj, &fdp, &filename);
|
||||||
if (fp != NULL) {
|
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;
|
*p_fp = fp;
|
||||||
return fdp;
|
return fdp;
|
||||||
}
|
}
|
||||||
|
else if (PyErr_Occurred())
|
||||||
|
return NULL;
|
||||||
#endif
|
#endif
|
||||||
|
Py_DECREF(nameobj);
|
||||||
path = PySys_GetObject("path");
|
path = PySys_GetObject("path");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue