blob: 196a774af35eb1e81596e7afa39ef04e7257ccc6 [file] [log] [blame]
Guido van Rossum6dbd1901996-08-21 15:03:37 +00001/********************************************************************
2
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003 import_nt.c
Guido van Rossum6dbd1901996-08-21 15:03:37 +00004
5 Win32 specific import code.
6
7*/
8
Guido van Rossum7688bba1997-05-05 21:45:44 +00009#include "Python.h"
Guido van Rossum6dbd1901996-08-21 15:03:37 +000010#include "osdefs.h"
11#include <windows.h>
Guido van Rossum6dbd1901996-08-21 15:03:37 +000012#include "importdl.h"
Tim Petersdb638292000-07-03 23:51:17 +000013#include "malloc.h" /* for alloca */
Guido van Rossum271f9771997-09-29 23:39:31 +000014
Tim Petersdb638292000-07-03 23:51:17 +000015/* a string loaded from the DLL at startup */
16extern const char *PyWin_DLLVersionString;
Guido van Rossum6dbd1901996-08-21 15:03:37 +000017
Tim Petersdb638292000-07-03 23:51:17 +000018FILE *PyWin_FindRegisteredModule(const char *moduleName,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000019 struct filedescr **ppFileDesc,
20 char *pathBuf,
21 Py_ssize_t pathLen)
Guido van Rossum6dbd1901996-08-21 15:03:37 +000022{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000023 char *moduleKey;
24 const char keyPrefix[] = "Software\\Python\\PythonCore\\";
25 const char keySuffix[] = "\\Modules\\";
Guido van Rossum1c096b71998-05-26 13:53:23 +000026#ifdef _DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000027 /* In debugging builds, we _must_ have the debug version
28 * registered.
29 */
30 const char debugString[] = "\\Debug";
Guido van Rossum1c096b71998-05-26 13:53:23 +000031#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000032 const char debugString[] = "";
Guido van Rossum1c096b71998-05-26 13:53:23 +000033#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000034 struct filedescr *fdp = NULL;
35 FILE *fp;
36 HKEY keyBase = HKEY_CURRENT_USER;
37 int modNameSize;
38 long regStat;
Guido van Rossum271f9771997-09-29 23:39:31 +000039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 /* Calculate the size for the sprintf buffer.
41 * Get the size of the chars only, plus 1 NULL.
42 */
43 size_t bufSize = sizeof(keyPrefix)-1 +
44 strlen(PyWin_DLLVersionString) +
45 sizeof(keySuffix) +
46 strlen(moduleName) +
47 sizeof(debugString) - 1;
48 /* alloca == no free required, but memory only local to fn,
49 * also no heap fragmentation!
50 */
51 moduleKey = alloca(bufSize);
52 PyOS_snprintf(moduleKey, bufSize,
53 "Software\\Python\\PythonCore\\%s\\Modules\\%s%s",
54 PyWin_DLLVersionString, moduleName, debugString);
Guido van Rossum271f9771997-09-29 23:39:31 +000055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000056 assert(pathLen < INT_MAX);
57 modNameSize = (int)pathLen;
58 regStat = RegQueryValue(keyBase, moduleKey, pathBuf, &modNameSize);
59 if (regStat != ERROR_SUCCESS) {
60 /* No user setting - lookup in machine settings */
61 keyBase = HKEY_LOCAL_MACHINE;
62 /* be anal - failure may have reset size param */
63 modNameSize = (int)pathLen;
64 regStat = RegQueryValue(keyBase, moduleKey,
65 pathBuf, &modNameSize);
Mark Hammond48b3eee2000-08-22 11:20:21 +000066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000067 if (regStat != ERROR_SUCCESS)
68 return NULL;
69 }
70 /* use the file extension to locate the type entry. */
71 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
72 size_t extLen = strlen(fdp->suffix);
73 assert(modNameSize >= 0); /* else cast to size_t is wrong */
74 if ((size_t)modNameSize > extLen &&
75 strnicmp(pathBuf + ((size_t)modNameSize-extLen-1),
76 fdp->suffix,
77 extLen) == 0)
78 break;
79 }
80 if (fdp->suffix == NULL)
81 return NULL;
82 fp = fopen(pathBuf, fdp->mode);
83 if (fp != NULL)
84 *ppFileDesc = fdp;
85 return fp;
Guido van Rossum6dbd1901996-08-21 15:03:37 +000086}