Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 43ff114

Browse files
committed
Sigh. More restructuring was needed.
Make an explicit test for whether the prefix is in fact the source directory, and then don't use the registry.
1 parent 7fb6688 commit 43ff114

1 file changed

Lines changed: 57 additions & 27 deletions

File tree

PC/getpathp.c

Lines changed: 57 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,23 @@ exists(filename)
9898
}
9999

100100

101+
static int
102+
ismodule(filename) /* Is module -- check for .pyc/.pyo too */
103+
char *filename;
104+
{
105+
if (exists(filename))
106+
return 1;
107+
108+
/* Check for the compiled version of prefix. */
109+
if (strlen(filename) < MAXPATHLEN) {
110+
strcat(filename, Py_OptimizeFlag ? "o" : "c");
111+
if (exists(filename))
112+
return 1;
113+
}
114+
return 0;
115+
}
116+
117+
101118
static void
102119
join(buffer, stuff)
103120
char *buffer;
@@ -131,7 +148,7 @@ search_for_prefix(argv0_path, landmark)
131148
do {
132149
n = strlen(prefix);
133150
join(prefix, landmark);
134-
if (exists(prefix)) {
151+
if (ismodule(prefix)) {
135152
prefix[n] = '\0';
136153
return 1;
137154
}
@@ -142,6 +159,22 @@ search_for_prefix(argv0_path, landmark)
142159
}
143160

144161
#ifdef MS_WIN32
162+
163+
#ifndef BUILD_LANDMARK
164+
#define BUILD_LANDMARK "PC\\getpathp.c"
165+
#endif
166+
167+
static int
168+
prefixisbuilddir()
169+
{
170+
int n = strlen(prefix);
171+
int ok;
172+
join(prefix, BUILD_LANDMARK);
173+
ok = exists(prefix);
174+
prefix[n] = '\0';
175+
return ok;
176+
}
177+
145178
#include "malloc.h" // for alloca - see comments below!
146179
extern const char *PyWin_DLLVersionString; // a string loaded from the DLL at startup.
147180

@@ -309,18 +342,10 @@ calculate_path()
309342
int bufsz;
310343
char *pythonhome = Py_GetPythonHome();
311344
char *envpath = getenv("PYTHONPATH");
345+
312346
#ifdef MS_WIN32
313-
char *machinepath, *userpath;
314-
315-
/* Are we running under Windows 3.1(1) Win32s? */
316-
if (PyWin_IsWin32s()) {
317-
/* Only CLASSES_ROOT is supported */
318-
machinepath = getpythonregpath(HKEY_CLASSES_ROOT, TRUE);
319-
userpath = NULL;
320-
} else {
321-
machinepath = getpythonregpath(HKEY_LOCAL_MACHINE, FALSE);
322-
userpath = getpythonregpath(HKEY_CURRENT_USER, FALSE);
323-
}
347+
char *machinepath = NULL;
348+
char *userpath = NULL;
324349
#endif
325350

326351
get_progpath();
@@ -329,34 +354,39 @@ calculate_path()
329354
if (pythonhome == NULL || *pythonhome == '\0') {
330355
if (search_for_prefix(argv0_path, LANDMARK))
331356
pythonhome = prefix;
332-
else {
333-
/* Couldnt find a source version - lets see if a compiled version exists. */
334-
char LANDMARK_Look[MAX_PATH+1];
335-
strcpy(LANDMARK_Look, LANDMARK);
336-
/* Turn it into ".pyc" or ".pyc" depending on the current mode. */
337-
strcat(LANDMARK_Look, Py_OptimizeFlag ? "o": "c");
338-
/* And search again */
339-
if (search_for_prefix(argv0_path, LANDMARK_Look))
340-
pythonhome = prefix;
341-
else
342-
/* Give up in disgust - just use the default! */
343-
pythonhome = NULL;
344-
}
357+
else
358+
pythonhome = NULL;
345359
}
346360
else
347361
strcpy(prefix, pythonhome);
348362

349363
if (envpath && *envpath == '\0')
350364
envpath = NULL;
351365

352-
/* We need to construct a path from the following parts:
366+
#ifdef MS_WIN32
367+
if (!prefixisbuilddir()) {
368+
/* Are we running under Windows 3.1(1) Win32s? */
369+
if (PyWin_IsWin32s()) {
370+
/* Only CLASSES_ROOT is supported */
371+
machinepath = getpythonregpath(HKEY_CLASSES_ROOT, TRUE);
372+
userpath = NULL;
373+
} else {
374+
machinepath = getpythonregpath(HKEY_LOCAL_MACHINE, FALSE);
375+
userpath = getpythonregpath(HKEY_CURRENT_USER, FALSE);
376+
}
377+
}
378+
#endif
379+
380+
/* We need to construct a path from the following parts.
353381
(1) the PYTHONPATH environment variable, if set;
354382
(2) for Win32, the machinepath and userpath, if set;
355-
The following only if neither machinepath nor userpath is set:
356383
(3) the PYTHONPATH config macro, with the leading "."
357384
of each component replaced with pythonhome, if set;
358385
(4) the directory containing the executable (argv0_path).
359386
The length calculation calculates #3 first.
387+
Extra rules:
388+
- If PYTHONHOME is set (in any way) item (2) is ignored.
389+
- If registry values are used, (3) and (4) are ignored.
360390
*/
361391

362392
/* Calculate size of return buffer */

0 commit comments

Comments
 (0)