Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which

is now obsolete), and changed the default path calculations.
$PYTHONPATH is now added as a prefix (like it's always been on Unix);
$PYTHONHOME takes precedence over the program pathname; and only one
landmark is needed.
This commit is contained in:
Guido van Rossum 1997-08-13 21:30:44 +00:00
parent 407a22d2ce
commit eea1449a34
1 changed files with 223 additions and 56 deletions

View File

@ -37,6 +37,7 @@ PERFORMANCE OF THIS SOFTWARE.
#ifdef MS_WIN32 #ifdef MS_WIN32
#include <windows.h> #include <windows.h>
extern BOOL PyWin_IsWin32s();
#endif #endif
#include <sys/types.h> #include <sys/types.h>
@ -70,13 +71,14 @@ PERFORMANCE OF THIS SOFTWARE.
*/ */
#ifndef LANDMARK #ifndef LANDMARK
#define LANDMARK "Modules\\Setup.in" #define LANDMARK "lib\\string.py"
#endif #endif
static char prefix[MAXPATHLEN+1]; static char prefix[MAXPATHLEN+1];
static char progpath[MAXPATHLEN+1]; static char progpath[MAXPATHLEN+1];
static char *module_search_path = NULL; static char *module_search_path = NULL;
static int static int
is_sep(ch) /* determine if "ch" is a separator character */ is_sep(ch) /* determine if "ch" is a separator character */
char ch; char ch;
@ -88,6 +90,7 @@ is_sep(ch) /* determine if "ch" is a separator character */
#endif #endif
} }
static void static void
reduce(dir) reduce(dir)
char *dir; char *dir;
@ -141,25 +144,121 @@ search_for_prefix(argv0_path, landmark)
do { do {
n = strlen(prefix); n = strlen(prefix);
join(prefix, landmark); join(prefix, landmark);
if (exists(prefix)) if (exists(prefix)) {
prefix[n] = '\0';
return 1; return 1;
}
prefix[n] = '\0'; prefix[n] = '\0';
reduce(prefix); reduce(prefix);
} while (prefix[0]); } while (prefix[0]);
return 0; return 0;
} }
#ifdef MS_WIN32
/* Load a PYTHONPATH value from the registry.
Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER.
Returns NULL, or a pointer that should be freed.
*/
static char *
getpythonregpath(HKEY keyBase, BOOL bWin32s)
{
HKEY newKey = 0;
DWORD nameSize = 0;
DWORD dataSize = 0;
DWORD numEntries = 0;
LONG rc;
char *retval = NULL;
char *dataBuf;
rc=RegOpenKey(keyBase,
"Software\\Python\\PythonCore\\"
MS_DLL_ID "\\PythonPath",
&newKey);
if (rc==ERROR_SUCCESS) {
RegQueryInfoKey(newKey, NULL, NULL, NULL, NULL, NULL, NULL,
&numEntries, &nameSize, &dataSize, NULL, NULL);
}
if (bWin32s && numEntries==0 && dataSize==0) {
/* must hardcode for Win32s */
numEntries = 1;
dataSize = 511;
}
if (numEntries) {
/* Loop over all subkeys. */
/* Win32s doesnt know how many subkeys, so we do
it twice */
char keyBuf[MAX_PATH+1];
int index = 0;
int off = 0;
for(index=0;;index++) {
long reqdSize = 0;
DWORD rc = RegEnumKey(newKey,
index, keyBuf, MAX_PATH+1);
if (rc) break;
rc = RegQueryValue(newKey, keyBuf, NULL, &reqdSize);
if (rc) break;
if (bWin32s && reqdSize==0) reqdSize = 512;
dataSize += reqdSize + 1; /* 1 for the ";" */
}
dataBuf = malloc(dataSize+1);
if (dataBuf==NULL)
return NULL; /* pretty serious? Raise error? */
/* Now loop over, grabbing the paths.
Subkeys before main library */
for(index=0;;index++) {
int adjust;
long reqdSize = dataSize;
DWORD rc = RegEnumKey(newKey,
index, keyBuf,MAX_PATH+1);
if (rc) break;
rc = RegQueryValue(newKey,
keyBuf, dataBuf+off, &reqdSize);
if (rc) break;
if (reqdSize>1) {
/* If Nothing, or only '\0' copied. */
adjust = strlen(dataBuf+off);
dataSize -= adjust;
off += adjust;
dataBuf[off++] = ';';
dataBuf[off] = '\0';
dataSize--;
}
}
/* Additionally, win32s doesnt work as expected, so
the specific strlen() is required for 3.1. */
rc = RegQueryValue(newKey, "", dataBuf+off, &dataSize);
if (rc==ERROR_SUCCESS) {
if (strlen(dataBuf)==0)
free(dataBuf);
else
retval = dataBuf; /* caller will free */
}
else
free(dataBuf);
}
if (newKey)
RegCloseKey(newKey);
return retval;
}
#endif /* MS_WIN32 */
static void static void
get_progpath() get_progpath()
{ {
#ifdef MS_WIN32
if (!GetModuleFileName(NULL, progpath, MAXPATHLEN))
progpath[0] = '\0'; /* failure */
#else
extern char *Py_GetProgramName(); extern char *Py_GetProgramName();
char *path = getenv("PATH"); char *path = getenv("PATH");
char *prog = Py_GetProgramName(); char *prog = Py_GetProgramName();
#ifdef MS_WIN32
if (GetModuleFileName(NULL, progpath, MAXPATHLEN))
return;
#endif
if (prog == NULL || *prog == '\0')
prog = "python";
/* If there is no slash in the argv0 path, then we have to /* If there is no slash in the argv0 path, then we have to
* assume python is on the user's $PATH, since there's no * assume python is on the user's $PATH, since there's no
* other way to find a directory to start the search from. If * other way to find a directory to start the search from. If
@ -196,76 +295,142 @@ get_progpath()
} }
else else
progpath[0] = '\0'; progpath[0] = '\0';
#endif
} }
static void static void
calculate_path() calculate_path()
{ {
char ch, *pt, *pt2;
char argv0_path[MAXPATHLEN+1]; char argv0_path[MAXPATHLEN+1];
char *buf; char *buf;
int bufsz; int bufsz;
char *pythonhome = getenv("PYTHONHOME");
char *envpath = getenv("PYTHONPATH");
#ifdef MS_WIN32
char *machinepath, *userpath;
/* Are we running under Windows 3.1(1) Win32s? */
if (PyWin_IsWin32s()) {
/* Only CLASSES_ROOT is supported */
machinepath = getpythonregpath(HKEY_CLASSES_ROOT, TRUE);
userpath = NULL;
} else {
machinepath = getpythonregpath(HKEY_LOCAL_MACHINE, FALSE);
userpath = getpythonregpath(HKEY_CURRENT_USER, FALSE);
}
#endif
get_progpath(); get_progpath();
strcpy(argv0_path, progpath); strcpy(argv0_path, progpath);
reduce(argv0_path); reduce(argv0_path);
if (pythonhome == NULL || *pythonhome == '\0') {
if (search_for_prefix(argv0_path, LANDMARK))
pythonhome = prefix;
else
pythonhome = NULL;
}
else
strcpy(prefix, pythonhome);
if (search_for_prefix(argv0_path, LANDMARK)) { if (envpath && *envpath == '\0')
reduce(prefix); envpath = NULL;
reduce(prefix);
join(prefix, "lib"); /* We need to construct a path from the following parts:
} (1) the PYTHONPATH environment variable, if set;
else if ((module_search_path = getenv("PYTHONPATH")) != 0) { (2) for Win32, the machinepath and userpath, if set;
return; /* if PYTHONPATH environment variable exists, we are done */ (3) the PYTHONPATH config macro, with the leading "."
} of each component replaced with pythonhome, if set;
else { /* Try the executable_directory/lib */ (4) the directory containing the executable (argv0_path).
strcpy(prefix, progpath); The length calculation calculates #3 first.
reduce(prefix); */
join(prefix, "lib");
join(prefix, "string.py"); /* Look for lib/string.py */ /* Calculate size of return buffer */
if (exists(prefix)) { if (pythonhome != NULL) {
reduce(prefix); char *p;
} bufsz = 1;
else { /* No module search path!!! */ for (p = PYTHONPATH; *p; p++) {
module_search_path = PYTHONPATH; if (*p == DELIM)
return; bufsz++; /* number of DELIM plus one */
} }
bufsz *= strlen(pythonhome);
} }
else
bufsz = 0;
/* If we get here, we need to return a path equal to the compiled bufsz += strlen(PYTHONPATH);
PYTHONPATH with ".\lib" replaced by our "prefix" directory */ if (envpath != NULL)
bufsz += strlen(envpath) + 1;
bufsz = 1; /* Calculate size of return buffer. */
for (pt = PYTHONPATH; *pt; pt++)
if (*pt == DELIM)
bufsz++; /* number of DELIM plus one */
bufsz *= strlen(PYTHONPATH) + strlen(prefix); /* high estimate */
bufsz += strlen(argv0_path) + 1; bufsz += strlen(argv0_path) + 1;
#ifdef MS_WIN32
if (machinepath)
bufsz += strlen(machinepath) + 1;
if (userpath)
bufsz += strlen(userpath) + 1;
#endif
module_search_path = buf = malloc(bufsz); module_search_path = buf = malloc(bufsz);
if (buf == NULL) { if (buf == NULL) {
/* We can't exit, so print a warning and limp along */ /* We can't exit, so print a warning and limp along */
fprintf(stderr, "Not enough memory for dynamic PYTHONPATH.\n"); fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
fprintf(stderr, "Using default static PYTHONPATH.\n"); if (envpath) {
module_search_path = PYTHONPATH; fprintf(stderr, "Using default static $PYTHONPATH.\n");
module_search_path = envpath;
}
else {
fprintf(stderr, "Using environment $PYTHONPATH.\n");
module_search_path = PYTHONPATH;
}
return; return;
} }
for (pt = PYTHONPATH; *pt; pt++) {
if (!strncmp(pt, ".\\lib", 5) && if (envpath) {
((ch = *(pt + 5)) == '\\' || ch == DELIM || !ch)) { strcpy(buf, envpath);
pt += 4; buf = strchr(buf, '\0');
for (pt2 = prefix; *pt2; pt2++) *buf++ = DELIM;
*buf++ = *pt2; }
} #ifdef MS_WIN32
else if (machinepath) {
*buf++ = *pt; strcpy(buf, machinepath);
buf = strchr(buf, '\0');
*buf++ = DELIM;
}
if (userpath) {
strcpy(buf, userpath);
buf = strchr(buf, '\0');
*buf++ = DELIM;
}
#endif
if (pythonhome == NULL) {
strcpy(buf, PYTHONPATH);
buf = strchr(buf, '\0');
}
else {
char *p = PYTHONPATH;
char *q;
int n;
for (;;) {
q = strchr(p, DELIM);
if (q == NULL)
n = strlen(p);
else
n = q-p;
if (p[0] == '.' && is_sep(p[1])) {
strcpy(buf, pythonhome);
buf = strchr(buf, '\0');
p++;
n--;
}
strncpy(buf, p, n);
buf += n;
if (q == NULL)
break;
*buf++ = DELIM;
p = q+1;
}
}
if (argv0_path) {
*buf++ = DELIM;
strcpy(buf, argv0_path);
buf = strchr(buf, '\0');
} }
*buf++ = DELIM;
strcpy(buf, argv0_path);
buf += strlen(buf);
*buf = '\0'; *buf = '\0';
} }
@ -283,17 +448,19 @@ Py_GetPath()
char * char *
Py_GetPrefix() Py_GetPrefix()
{ {
return ""; if (!module_search_path)
calculate_path();
return prefix;
} }
char * char *
Py_GetExecPrefix() Py_GetExecPrefix()
{ {
return ""; return Py_GetPrefix();
} }
char * char *
Py_GetProgramFullPath() /* Full path to Python executable */ Py_GetProgramFullPath()
{ {
if (!module_search_path) if (!module_search_path)
calculate_path(); calculate_path();