NT specific files supplied by Mark Hammond · pythoncapi/cpython@6dbd190 · GitHub
Skip to content

Commit 6dbd190

Browse files
committed
NT specific files supplied by Mark Hammond
1 parent 019f424 commit 6dbd190

4 files changed

Lines changed: 293 additions & 0 deletions

File tree

PC/dl_nt.c

Lines changed: 34 additions & 0 deletions

PC/getpath_nt.c

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
#include "Python.h"
2+
#include "osdefs.h"
3+
#include <windows.h>
4+
5+
#ifndef WIN32_PATCH_LEVEL
6+
#define WIN32_PATCH_LEVEL "000"
7+
#endif
8+
9+
/* PREFIX and EXEC_PREFIX are meaningless on Windows */
10+
11+
#ifndef PREFIX
12+
#define PREFIX ""
13+
#endif
14+
15+
#ifndef EXEC_PREFIX
16+
#define EXEC_PREFIX ""
17+
#endif
18+
19+
/*
20+
This is a special Win32 version of getpath.
21+
22+
* There is no default path. There is nothing even remotely resembling
23+
a standard location. Maybe later "Program Files/Python", but not yet.
24+
25+
* The Registry is used as the primary store for the Python path.
26+
27+
* The environment variable PYTHONPATH _overrides_ the registry. This should
28+
allow a "standard" Python environment, but allow you to manually setup
29+
another (eg, a beta version).
30+
31+
*/
32+
33+
BOOL PyWin_IsWin32s()
34+
{
35+
static BOOL bIsWin32s = -1; // flag as "not yet looked"
36+
37+
if (bIsWin32s==-1) {
38+
OSVERSIONINFO ver;
39+
ver.dwOSVersionInfoSize = sizeof(ver);
40+
GetVersionEx(&ver);
41+
bIsWin32s = ver.dwPlatformId == VER_PLATFORM_WIN32s;
42+
}
43+
return bIsWin32s;
44+
}
45+
46+
/* Load a PYTHONPATH value from the registry
47+
Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER
48+
49+
Returns NULL, or a pointer that should be free'd.
50+
*/
51+
static char *
52+
getpythonregpath(HKEY keyBase, BOOL bWin32s)
53+
{
54+
HKEY newKey = 0;
55+
DWORD nameSize = 0;
56+
DWORD dataSize = 0;
57+
DWORD numEntries = 0;
58+
LONG rc;
59+
char *retval = NULL;
60+
char *dataBuf;
61+
if ((rc=RegOpenKey(keyBase, "Software\\Python\\PythonCore\\" WIN32_PATCH_LEVEL "\\PythonPath",
62+
&newKey))==ERROR_SUCCESS) {
63+
RegQueryInfoKey(newKey, NULL, NULL, NULL, NULL, NULL, NULL,
64+
&numEntries, &nameSize, &dataSize, NULL, NULL );
65+
}
66+
if (numEntries==0) {
67+
if (newKey)
68+
CloseHandle(newKey);
69+
if ((rc=RegOpenKey(keyBase, "Software\\Python\\PythonPath",
70+
&newKey))==ERROR_SUCCESS) {
71+
RegQueryInfoKey(newKey, NULL, NULL, NULL, NULL, NULL, NULL,
72+
&numEntries, &nameSize, &dataSize, NULL, NULL );
73+
}
74+
}
75+
if (bWin32s && numEntries==0 && dataSize==0) { /* must hardcode for Win32s */
76+
numEntries = 1;
77+
dataSize = 511;
78+
}
79+
if (numEntries) {
80+
dataBuf = malloc(dataSize);
81+
// on NT, datasize is unicode - ie, 2xstrlen,
82+
// even when ascii string returned.
83+
// presumably will be 1xstrlen on 95/win3.1
84+
// Additionally, win32s doesnt work as expected, so
85+
// the specific strlen() is required for 3.1.
86+
rc = RegQueryValue(newKey, "", dataBuf, &dataSize);
87+
if (rc==ERROR_SUCCESS) {
88+
if (strlen(dataBuf)==0)
89+
free(dataBuf);
90+
else
91+
retval = dataBuf; // caller will free
92+
}
93+
else
94+
free(dataBuf);
95+
}
96+
97+
if (newKey)
98+
CloseHandle(newKey);
99+
return retval;
100+
}
101+
/* Return the initial python search path. This is called once from
102+
initsys() to initialize sys.path. The environment variable
103+
PYTHONPATH is fetched and the default path appended. The default
104+
path may be passed to the preprocessor; if not, a system-dependent
105+
default is used. */
106+
107+
char *
108+
Py_GetPath()
109+
{
110+
char *path = getenv("PYTHONPATH");
111+
char *defpath = PYTHONPATH;
112+
static char *buf = NULL;
113+
char *p;
114+
int n;
115+
116+
if (buf != NULL) {
117+
free(buf);
118+
buf = NULL;
119+
}
120+
121+
if (path == NULL) {
122+
char *machinePath, *userPath;
123+
int machineLen, userLen;
124+
/* lookup the registry */
125+
BOOL bWin32s = PyWin_IsWin32s();
126+
127+
if (bWin32s) { /* are we running under Windows 3.1 Win32s */
128+
/* only CLASSES_ROOT is supported */
129+
machinePath = getpythonregpath(HKEY_CLASSES_ROOT, TRUE);
130+
userPath = NULL;
131+
} else {
132+
machinePath = getpythonregpath(HKEY_LOCAL_MACHINE, FALSE);
133+
userPath = getpythonregpath(HKEY_CURRENT_USER, FALSE);
134+
}
135+
if (machinePath==NULL && userPath==NULL) return defpath;
136+
machineLen = machinePath ? strlen(machinePath) : 0;
137+
userLen = userPath ? strlen(userPath) : 0;
138+
n = machineLen + userLen + 1;
139+
// this is a memory leak, as Python never frees it. Only ever called once, so big deal!
140+
buf = malloc(n);
141+
if (buf == NULL)
142+
Py_FatalError("not enough memory to copy module search path");
143+
p = buf;
144+
*p = '\0';
145+
if (machineLen) {
146+
strcpy(p, machinePath);
147+
p += machineLen;
148+
}
149+
if (userLen) {
150+
if (machineLen)
151+
*p++ = DELIM;
152+
strcpy(p, userPath);
153+
}
154+
if (userPath) free(userPath);
155+
if (machinePath) free(machinePath);
156+
} else {
157+
158+
buf = malloc(strlen(path)+1);
159+
if (buf == NULL)
160+
Py_FatalError("not enough memory to copy module search path");
161+
strcpy(buf, path);
162+
}
163+
return buf;
164+
}
165+
166+
/* Similar for Makefile variables $prefix and $exec_prefix */
167+
168+
char *
169+
Py_GetPrefix()
170+
{
171+
return PREFIX;
172+
}
173+
174+
char *
175+
Py_GetExecPrefix()
176+
{
177+
return EXEC_PREFIX;
178+
}

PC/import_nt.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/********************************************************************
2+
3+
importnt.c
4+
5+
Win32 specific import code.
6+
7+
*/
8+
9+
#include "allobjects.h"
10+
#include "osdefs.h"
11+
#include <windows.h>
12+
#include "import.h"
13+
#include "importdl.h"
14+
15+
#ifndef WIN32_PATCH_LEVEL
16+
#define WIN32_PATCH_LEVEL "000"
17+
#endif
18+
19+
extern BOOL PyWin_IsWin32s();
20+
21+
FILE *PyWin_FindRegisteredModule( const char *moduleName, struct filedescr **ppFileDesc, char *pathBuf, int pathLen)
22+
{
23+
char moduleKey[128];
24+
struct filedescr *fdp = NULL;
25+
FILE *fp;
26+
int modNameSize = pathLen;
27+
HKEY keyBase = PyWin_IsWin32s() ? HKEY_CLASSES_ROOT : HKEY_LOCAL_MACHINE;
28+
strcpy(moduleKey, "Software\\Python\\PythonCore\\" WIN32_PATCH_LEVEL "\\Modules\\");
29+
strcat(moduleKey, moduleName);
30+
if (RegQueryValue(keyBase, moduleKey, pathBuf, &modNameSize)!=ERROR_SUCCESS)
31+
return NULL;
32+
// use the file extension to locate the type entry.
33+
for (fdp = import_filetab; fdp->suffix != NULL; fdp++) {
34+
int extLen=strlen(fdp->suffix);
35+
if (modNameSize>extLen && strnicmp(pathBuf+(modNameSize-extLen-1),fdp->suffix,extLen)==0)
36+
break;
37+
}
38+
if (fdp->suffix==NULL)
39+
return NULL;
40+
fp = fopen(pathBuf, fdp->mode);
41+
if (fp != NULL)
42+
*ppFileDesc = fdp;
43+
return fp;
44+
}

PC/main_nt.c

Lines changed: 37 additions & 0 deletions

0 commit comments

Comments
 (0)