ยปCore Development>Code coverage>PC/import_nt.c

Python code coverage for PC/import_nt.c

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