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