Skip to content

Commit 19fdbfb

Browse files
committed
Fix bug #410274 - sys.prefix isn't always set.
If after calculating sys.path we do not have sys.prefix set, we loop over all path entries checking if one can point to our home directory.
1 parent 71707f3 commit 19fdbfb

1 file changed

Lines changed: 35 additions & 1 deletion

File tree

PC/getpathp.c

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
4545
* When Python is hosted in another exe (different directory, embedded via
4646
COM, etc), the Python Home will not be deduced, so the core path from
47-
the registry is used. Other "application paths "in the registry are
47+
the registry is used. Other "application paths" in the registry are
4848
always read.
4949
5050
* If Python can't find its home and there is no registry (eg, frozen
@@ -576,6 +576,40 @@ calculate_path(void)
576576
buf = strchr(buf, '\0');
577577
}
578578
*buf = '\0';
579+
/* Now to pull one last hack/trick. If sys.prefix is
580+
empty, then try and find it somewhere on the paths
581+
we calculated. We scan backwards, as our general policy
582+
is that Python core directories are at the *end* of
583+
sys.path. We assume that our "lib" directory is
584+
on the path, and that our 'prefix' directory is
585+
the parent of that.
586+
*/
587+
if (*prefix=='\0') {
588+
char lookBuf[MAXPATHLEN+1];
589+
char *look = buf - 1; /* 'buf' is at the end of the buffer */
590+
while (1) {
591+
int nchars;
592+
char *lookEnd = look;
593+
/* 'look' will end up one character before the
594+
start of the path in question - even if this
595+
is one character before the start of the buffer
596+
*/
597+
while (*look != DELIM && look >= module_search_path)
598+
look--;
599+
nchars = lookEnd-look;
600+
strncpy(lookBuf, look+1, nchars);
601+
lookBuf[nchars] = '\0';
602+
/* Up one level to the parent */
603+
reduce(lookBuf);
604+
if (search_for_prefix(lookBuf, LANDMARK)) {
605+
break;
606+
}
607+
/* If we are out of paths to search - give up */
608+
if (look < module_search_path)
609+
break;
610+
look--;
611+
}
612+
}
579613
}
580614

581615

0 commit comments

Comments
 (0)