blob: 99e8b06af756c6e2b662267aed36cf1afd056778 [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 }
62 if (numEntries==0) {
63 if (newKey)
64 CloseHandle(newKey);
65 if ((rc=RegOpenKey(keyBase, "Software\\Python\\PythonPath",
66 &newKey))==ERROR_SUCCESS) {
67 RegQueryInfoKey(newKey, NULL, NULL, NULL, NULL, NULL, NULL,
68 &numEntries, &nameSize, &dataSize, NULL, NULL );
69 }
70 }
71 if (bWin32s && numEntries==0 && dataSize==0) { /* must hardcode for Win32s */
72 numEntries = 1;
73 dataSize = 511;
74 }
75 if (numEntries) {
76 dataBuf = malloc(dataSize);
77 // on NT, datasize is unicode - ie, 2xstrlen,
78 // even when ascii string returned.
79 // presumably will be 1xstrlen on 95/win3.1
80 // Additionally, win32s doesnt work as expected, so
81 // the specific strlen() is required for 3.1.
82 rc = RegQueryValue(newKey, "", dataBuf, &dataSize);
83 if (rc==ERROR_SUCCESS) {
84 if (strlen(dataBuf)==0)
85 free(dataBuf);
86 else
87 retval = dataBuf; // caller will free
88 }
89 else
90 free(dataBuf);
91 }
92
93 if (newKey)
94 CloseHandle(newKey);
95 return retval;
96}
97/* Return the initial python search path. This is called once from
98 initsys() to initialize sys.path. The environment variable
99 PYTHONPATH is fetched and the default path appended. The default
100 path may be passed to the preprocessor; if not, a system-dependent
101 default is used. */
102
103char *
104Py_GetPath()
105{
106 char *path = getenv("PYTHONPATH");
107 char *defpath = PYTHONPATH;
108 static char *buf = NULL;
109 char *p;
110 int n;
111
112 if (buf != NULL) {
113 free(buf);
114 buf = NULL;
115 }
116
117 if (path == NULL) {
118 char *machinePath, *userPath;
119 int machineLen, userLen;
120 /* lookup the registry */
121 BOOL bWin32s = PyWin_IsWin32s();
122
123 if (bWin32s) { /* are we running under Windows 3.1 Win32s */
124 /* only CLASSES_ROOT is supported */
125 machinePath = getpythonregpath(HKEY_CLASSES_ROOT, TRUE);
126 userPath = NULL;
127 } else {
128 machinePath = getpythonregpath(HKEY_LOCAL_MACHINE, FALSE);
129 userPath = getpythonregpath(HKEY_CURRENT_USER, FALSE);
130 }
131 if (machinePath==NULL && userPath==NULL) return defpath;
132 machineLen = machinePath ? strlen(machinePath) : 0;
133 userLen = userPath ? strlen(userPath) : 0;
134 n = machineLen + userLen + 1;
135 // this is a memory leak, as Python never frees it. Only ever called once, so big deal!
136 buf = malloc(n);
137 if (buf == NULL)
138 Py_FatalError("not enough memory to copy module search path");
139 p = buf;
140 *p = '\0';
141 if (machineLen) {
142 strcpy(p, machinePath);
143 p += machineLen;
144 }
145 if (userLen) {
146 if (machineLen)
147 *p++ = DELIM;
148 strcpy(p, userPath);
149 }
150 if (userPath) free(userPath);
151 if (machinePath) free(machinePath);
152 } else {
153
154 buf = malloc(strlen(path)+1);
155 if (buf == NULL)
156 Py_FatalError("not enough memory to copy module search path");
157 strcpy(buf, path);
158 }
159 return buf;
160}
161
162/* Similar for Makefile variables $prefix and $exec_prefix */
163
164char *
165Py_GetPrefix()
166{
167 return PREFIX;
168}
169
170char *
171Py_GetExecPrefix()
172{
173 return EXEC_PREFIX;
174}