blob: 045fc7c2d1a6f1010297dc0455f9f2f7a160b64d [file] [log] [blame]
Guido van Rossum6dbd1901996-08-21 15:03:37 +00001#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/*
20This 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
33BOOL 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*/
51static char *
52getpythonregpath(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
107char *
108Py_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
168char *
169Py_GetPrefix()
170{
171 return PREFIX;
172}
173
174char *
175Py_GetExecPrefix()
176{
177 return EXEC_PREFIX;
178}