getpath.c
来自「python s60 1.4.5版本的源代码」· C语言 代码 · 共 665 行 · 第 1/2 页
C
665 行
joinpath(exec_prefix, "lib-dynload");
if (isdir(exec_prefix))
return 1;
exec_prefix[n] = '\0';
reduce(exec_prefix);
} while (exec_prefix[0]);
/* Look at configure's EXEC_PREFIX */
strncpy(exec_prefix, EXEC_PREFIX, MAXPATHLEN);
joinpath(exec_prefix, lib_python);
joinpath(exec_prefix, "lib-dynload");
if (isdir(exec_prefix))
return 1;
/* Fail */
return 0;
}
static void
calculate_path(void)
{
extern char *Py_GetProgramName(void);
static char delimiter[2] = {DELIM, '\0'};
static char separator[2] = {SEP, '\0'};
char *pythonpath = PYTHONPATH;
char *rtpypath = Py_GETENV("PYTHONPATH");
char *home = Py_GetPythonHome();
char *path = getenv("PATH");
char *prog = Py_GetProgramName();
char argv0_path[MAXPATHLEN+1];
int pfound, efound; /* 1 if found; -1 if found build directory */
char *buf;
size_t bufsz;
size_t prefixsz;
char *defpath = pythonpath;
#ifdef WITH_NEXT_FRAMEWORK
NSModule pythonModule;
#endif
/* 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
* other way to find a directory to start the search from. If
* $PATH isn't exported, you lose.
*/
if (strchr(prog, SEP))
strncpy(progpath, prog, MAXPATHLEN);
else if (path) {
while (1) {
char *delim = strchr(path, DELIM);
if (delim) {
size_t len = delim - path;
if (len > MAXPATHLEN)
len = MAXPATHLEN;
strncpy(progpath, path, len);
*(progpath + len) = '\0';
}
else
strncpy(progpath, path, MAXPATHLEN);
joinpath(progpath, prog);
if (isxfile(progpath))
break;
if (!delim) {
progpath[0] = '\0';
break;
}
path = delim + 1;
}
}
else
progpath[0] = '\0';
if (progpath[0] != SEP)
absolutize(progpath);
strncpy(argv0_path, progpath, MAXPATHLEN);
argv0_path[MAXPATHLEN] = '\0';
#ifdef WITH_NEXT_FRAMEWORK
/* On Mac OS X we have a special case if we're running from a framework.
** This is because the python home should be set relative to the library,
** which is in the framework, not relative to the executable, which may
** be outside of the framework. Except when we're in the build directory...
*/
pythonModule = NSModuleForSymbol(NSLookupAndBindSymbol("_Py_Initialize"));
/* Use dylib functions to find out where the framework was loaded from */
buf = NSLibraryNameForModule(pythonModule);
if (buf != NULL) {
/* We're in a framework. */
/* See if we might be in the build directory. The framework in the
** build directory is incomplete, it only has the .dylib and a few
** needed symlinks, it doesn't have the Lib directories and such.
** If we're running with the framework from the build directory we must
** be running the interpreter in the build directory, so we use the
** build-directory-specific logic to find Lib and such.
*/
strncpy(argv0_path, buf, MAXPATHLEN);
reduce(argv0_path);
joinpath(argv0_path, lib_python);
joinpath(argv0_path, LANDMARK);
if (!ismodule(argv0_path)) {
/* We are in the build directory so use the name of the
executable - we know that the absolute path is passed */
strncpy(argv0_path, prog, MAXPATHLEN);
}
else {
/* Use the location of the library as the progpath */
strncpy(argv0_path, buf, MAXPATHLEN);
}
}
#endif
#if HAVE_READLINK
{
char tmpbuffer[MAXPATHLEN+1];
int linklen = readlink(progpath, tmpbuffer, MAXPATHLEN);
while (linklen != -1) {
/* It's not null terminated! */
tmpbuffer[linklen] = '\0';
if (tmpbuffer[0] == SEP)
/* tmpbuffer should never be longer than MAXPATHLEN,
but extra check does not hurt */
strncpy(argv0_path, tmpbuffer, MAXPATHLEN);
else {
/* Interpret relative to progpath */
reduce(argv0_path);
joinpath(argv0_path, tmpbuffer);
}
linklen = readlink(argv0_path, tmpbuffer, MAXPATHLEN);
}
}
#endif /* HAVE_READLINK */
reduce(argv0_path);
/* At this point, argv0_path is guaranteed to be less than
MAXPATHLEN bytes long.
*/
if (!(pfound = search_for_prefix(argv0_path, home))) {
if (!Py_FrozenFlag)
fprintf(stderr,
"Could not find platform independent libraries <prefix>\n");
strncpy(prefix, PREFIX, MAXPATHLEN);
joinpath(prefix, lib_python);
}
else
reduce(prefix);
if (!(efound = search_for_exec_prefix(argv0_path, home))) {
if (!Py_FrozenFlag)
fprintf(stderr,
"Could not find platform dependent libraries <exec_prefix>\n");
strncpy(exec_prefix, EXEC_PREFIX, MAXPATHLEN);
joinpath(exec_prefix, "lib/lib-dynload");
}
/* If we found EXEC_PREFIX do *not* reduce it! (Yet.) */
if ((!pfound || !efound) && !Py_FrozenFlag)
fprintf(stderr,
"Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]\n");
/* Calculate size of return buffer.
*/
bufsz = 0;
if (rtpypath)
bufsz += strlen(rtpypath) + 1;
prefixsz = strlen(prefix) + 1;
while (1) {
char *delim = strchr(defpath, DELIM);
if (defpath[0] != SEP)
/* Paths are relative to prefix */
bufsz += prefixsz;
if (delim)
bufsz += delim - defpath + 1;
else {
bufsz += strlen(defpath) + 1;
break;
}
defpath = delim + 1;
}
bufsz += strlen(exec_prefix) + 1;
/* This is the only malloc call in this file */
buf = PyMem_Malloc(bufsz);
if (buf == NULL) {
/* We can't exit, so print a warning and limp along */
fprintf(stderr, "Not enough memory for dynamic PYTHONPATH.\n");
fprintf(stderr, "Using default static PYTHONPATH.\n");
module_search_path = PYTHONPATH;
}
else {
/* Run-time value of $PYTHONPATH goes first */
if (rtpypath) {
strcpy(buf, rtpypath);
strcat(buf, delimiter);
}
else
buf[0] = '\0';
/* Next goes merge of compile-time $PYTHONPATH with
* dynamically located prefix.
*/
defpath = pythonpath;
while (1) {
char *delim = strchr(defpath, DELIM);
if (defpath[0] != SEP) {
strcat(buf, prefix);
strcat(buf, separator);
}
if (delim) {
size_t len = delim - defpath + 1;
size_t end = strlen(buf) + len;
strncat(buf, defpath, len);
*(buf + end) = '\0';
}
else {
strcat(buf, defpath);
break;
}
defpath = delim + 1;
}
strcat(buf, delimiter);
/* Finally, on goes the directory for dynamic-load modules */
strcat(buf, exec_prefix);
/* And publish the results */
module_search_path = buf;
}
/* Reduce prefix and exec_prefix to their essence,
* e.g. /usr/local/lib/python1.5 is reduced to /usr/local.
* If we're loading relative to the build directory,
* return the compiled-in defaults instead.
*/
if (pfound > 0) {
reduce(prefix);
reduce(prefix);
}
else
strncpy(prefix, PREFIX, MAXPATHLEN);
if (efound > 0) {
reduce(exec_prefix);
reduce(exec_prefix);
reduce(exec_prefix);
}
else
strncpy(exec_prefix, EXEC_PREFIX, MAXPATHLEN);
}
/* External interface */
char *
Py_GetPath(void)
{
if (!module_search_path)
calculate_path();
return module_search_path;
}
char *
Py_GetPrefix(void)
{
if (!module_search_path)
calculate_path();
return prefix;
}
char *
Py_GetExecPrefix(void)
{
if (!module_search_path)
calculate_path();
return exec_prefix;
}
char *
Py_GetProgramFullPath(void)
{
if (!module_search_path)
calculate_path();
return progpath;
}
#else /* SYMBIAN */
const static char *const prefix = "\0";
const static char *const progpath = "\0";
/* External interface */
/* We'd like to get this from CSPyInterpreter.h but that's a C++ header... */
extern char* SPy_get_path(void);
DL_EXPORT(char *)
Py_GetPath(void)
{
return SPy_get_path();
}
DL_EXPORT(char *)
Py_GetPrefix(void)
{
// XXX:CW32
return (char *)prefix;
}
DL_EXPORT(char *)
Py_GetExecPrefix(void)
{
return Py_GetPrefix();
}
DL_EXPORT(char *)
Py_GetProgramFullPath(void)
{
// XXX:CW32
return (char *)progpath;
}
#endif /* SYMBIAN */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?