| 1 | n/a | /******************************************************************** |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | import_nt.c |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | Win32 specific import code. |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | */ |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | #include "Python.h" |
|---|
| 10 | n/a | #include "osdefs.h" |
|---|
| 11 | n/a | #include <windows.h> |
|---|
| 12 | n/a | #include "importdl.h" |
|---|
| 13 | n/a | #include "malloc.h" /* for alloca */ |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | /* a string loaded from the DLL at startup */ |
|---|
| 16 | n/a | extern const char *PyWin_DLLVersionString; |
|---|
| 17 | n/a | |
|---|
| 18 | n/a | /* Find a module on Windows. |
|---|
| 19 | n/a | |
|---|
| 20 | n/a | Read the registry Software\Python\PythonCore\<version>\Modules\<name> (or |
|---|
| 21 | n/a | Software\Python\PythonCore\<version>\Modules\<name>\Debug in debug mode) |
|---|
| 22 | n/a | from HKEY_CURRENT_USER, or HKEY_LOCAL_MACHINE. Find the file descriptor using |
|---|
| 23 | n/a | the file extension. Open the file. |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | On success, write the file descriptor into *ppFileDesc, the module path |
|---|
| 26 | n/a | (Unicode object) into *pPath, and return the opened file object. If the |
|---|
| 27 | n/a | module cannot be found (e.g. no registry key or the file doesn't exist), |
|---|
| 28 | n/a | return NULL. On error, raise a Python exception and return NULL. |
|---|
| 29 | n/a | */ |
|---|
| 30 | n/a | FILE * |
|---|
| 31 | n/a | _PyWin_FindRegisteredModule(PyObject *moduleName, |
|---|
| 32 | n/a | struct filedescr **ppFileDesc, |
|---|
| 33 | n/a | PyObject **pPath) |
|---|
| 34 | n/a | { |
|---|
| 35 | n/a | wchar_t pathBuf[MAXPATHLEN+1]; |
|---|
| 36 | n/a | int pathLen = MAXPATHLEN+1; |
|---|
| 37 | n/a | PyObject *path, *moduleKey, *suffix; |
|---|
| 38 | n/a | wchar_t *wmoduleKey, *wsuffix; |
|---|
| 39 | n/a | struct filedescr *fdp; |
|---|
| 40 | n/a | HKEY keyBase; |
|---|
| 41 | n/a | int modNameSize; |
|---|
| 42 | n/a | long regStat; |
|---|
| 43 | n/a | Py_ssize_t extLen; |
|---|
| 44 | n/a | FILE *fp; |
|---|
| 45 | n/a | |
|---|
| 46 | n/a | moduleKey = PyUnicode_FromFormat( |
|---|
| 47 | n/a | #ifdef _DEBUG |
|---|
| 48 | n/a | /* In debugging builds, we _must_ have the debug version registered */ |
|---|
| 49 | n/a | "Software\\Python\\PythonCore\\%s\\Modules\\%U\\Debug", |
|---|
| 50 | n/a | #else |
|---|
| 51 | n/a | "Software\\Python\\PythonCore\\%s\\Modules\\%U", |
|---|
| 52 | n/a | #endif |
|---|
| 53 | n/a | PyWin_DLLVersionString, moduleName); |
|---|
| 54 | n/a | if (moduleKey == NULL) |
|---|
| 55 | n/a | return NULL; |
|---|
| 56 | n/a | wmoduleKey = PyUnicode_AsUnicode(moduleKey); |
|---|
| 57 | n/a | if (wmoduleKey == NULL) { |
|---|
| 58 | n/a | Py_DECREF(moduleKey); |
|---|
| 59 | n/a | return NULL; |
|---|
| 60 | n/a | } |
|---|
| 61 | n/a | |
|---|
| 62 | n/a | keyBase = HKEY_CURRENT_USER; |
|---|
| 63 | n/a | modNameSize = pathLen; |
|---|
| 64 | n/a | regStat = RegQueryValueW(keyBase, wmoduleKey, |
|---|
| 65 | n/a | pathBuf, &modNameSize); |
|---|
| 66 | n/a | if (regStat != ERROR_SUCCESS) { |
|---|
| 67 | n/a | /* No user setting - lookup in machine settings */ |
|---|
| 68 | n/a | keyBase = HKEY_LOCAL_MACHINE; |
|---|
| 69 | n/a | /* be anal - failure may have reset size param */ |
|---|
| 70 | n/a | modNameSize = pathLen; |
|---|
| 71 | n/a | regStat = RegQueryValueW(keyBase, wmoduleKey, |
|---|
| 72 | n/a | pathBuf, &modNameSize); |
|---|
| 73 | n/a | if (regStat != ERROR_SUCCESS) { |
|---|
| 74 | n/a | Py_DECREF(moduleKey); |
|---|
| 75 | n/a | return NULL; |
|---|
| 76 | n/a | } |
|---|
| 77 | n/a | } |
|---|
| 78 | n/a | Py_DECREF(moduleKey); |
|---|
| 79 | n/a | if (modNameSize < 3) { |
|---|
| 80 | n/a | /* path shorter than "a.o" or negative length (cast to |
|---|
| 81 | n/a | size_t is wrong) */ |
|---|
| 82 | n/a | return NULL; |
|---|
| 83 | n/a | } |
|---|
| 84 | n/a | /* use the file extension to locate the type entry. */ |
|---|
| 85 | n/a | for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) { |
|---|
| 86 | n/a | suffix = PyUnicode_FromString(fdp->suffix); |
|---|
| 87 | n/a | if (suffix == NULL) |
|---|
| 88 | n/a | return NULL; |
|---|
| 89 | n/a | wsuffix = PyUnicode_AsUnicodeAndSize(suffix, &extLen); |
|---|
| 90 | n/a | if (wsuffix == NULL) { |
|---|
| 91 | n/a | Py_DECREF(suffix); |
|---|
| 92 | n/a | return NULL; |
|---|
| 93 | n/a | } |
|---|
| 94 | n/a | if ((Py_ssize_t)modNameSize > extLen && |
|---|
| 95 | n/a | _wcsnicmp(pathBuf + ((Py_ssize_t)modNameSize-extLen-1), |
|---|
| 96 | n/a | wsuffix, |
|---|
| 97 | n/a | extLen) == 0) |
|---|
| 98 | n/a | { |
|---|
| 99 | n/a | Py_DECREF(suffix); |
|---|
| 100 | n/a | break; |
|---|
| 101 | n/a | } |
|---|
| 102 | n/a | Py_DECREF(suffix); |
|---|
| 103 | n/a | } |
|---|
| 104 | n/a | if (fdp->suffix == NULL) |
|---|
| 105 | n/a | return NULL; |
|---|
| 106 | n/a | path = PyUnicode_FromWideChar(pathBuf, wcslen(pathBuf)); |
|---|
| 107 | n/a | if (path == NULL) |
|---|
| 108 | n/a | return NULL; |
|---|
| 109 | n/a | fp = _Py_fopen(path, fdp->mode); |
|---|
| 110 | n/a | if (fp == NULL) { |
|---|
| 111 | n/a | Py_DECREF(path); |
|---|
| 112 | n/a | return NULL; |
|---|
| 113 | n/a | } |
|---|
| 114 | n/a | *pPath = path; |
|---|
| 115 | n/a | *ppFileDesc = fdp; |
|---|
| 116 | n/a | return fp; |
|---|
| 117 | n/a | } |
|---|