static void get_progpath(void) { extern char *Py_GetProgramName(void); char *path = getenv("PATH"); char *prog = Py_GetProgramName(); PPIB pib; if ((DosGetInfoBlocks(NULL, &pib) == 0) && (DosQueryModuleName(pib->pib_hmte, sizeof(progpath), progpath) == 0)) return; if (prog == NULL || *prog == '\0') prog = "python"; /* 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. */ #ifdef ALTSEP if (strchr(prog, SEP) || strchr(prog, ALTSEP)) #else if (strchr(prog, SEP)) #endif strncpy(progpath, prog, MAXPATHLEN); else if (path) { while (1) { char *delim = strchr(path, DELIM); if (delim) { size_t len = delim - path; /* ensure we can't overwrite buffer */ #if !defined(PYCC_GCC) len = min(MAXPATHLEN,len); #else len = MAXPATHLEN < len ? MAXPATHLEN : len; #endif strncpy(progpath, path, len); *(progpath + len) = '\0'; } else strncpy(progpath, path, MAXPATHLEN); /* join() is safe for MAXPATHLEN+1 size buffer */ join(progpath, prog); if (exists(progpath)) break; if (!delim) { progpath[0] = '\0'; break; } path = delim + 1; } } else progpath[0] = '\0'; }
static const char *fullprogpath(void) { static char path[MAXPATHLEN*2]; static char prog[MAXPATHLEN]; BPTR dir; extern BOOL from_WB; /* in main.c */ // If the program name contains ':' or '/' it's not in the user's path // and probably set by using Py_SetProgramName. In that case, just // use this. If it exists! strcpy(path,Py_GetProgramName()); if(strchr(path,':') || strchr(path,'/')) { if(!isxfile(path)) { // Error; the specified file does not exist or is no exe path[0]='\0'; } return path; } // Construct the full path of our executable program. if(from_WB) { /* We're launced from WB, GetProgramName() won't work */ /* Use WB's argv[0] as the executable path */ int argc; char **argv; Py_GetArgcArgv(&argc, &argv); if(argc>0) strcpy(path,argv[0]); else strcpy(path,"!error!"); } else { /* Launced from CLI, use GetProgramName */ /* However, first check if the specified name exists */ if(!isxfile(path)) { path[0]='\0'; return path; } path[0]=0; if(dir=GetProgramDir()) { (void)NameFromLock(dir,path,MAXPATHLEN); if(!GetProgramName(prog,MAXPATHLEN)) // this is a dos.library function! strcpy(prog,"!error!"); if(!AddPart(path,prog,MAXPATHLEN*2)) strcpy(path,"!error!"); } } return path; }
dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname, const char *pathname, FILE *fp) { char funcname[258]; PyOS_snprintf(funcname, sizeof(funcname), "init%.200s", shortname); return dl_loadmod(Py_GetProgramName(), pathname, funcname); }
void PyRun::reinit(const Eref& e, ProcPtr p) { PyObject * main_module; if (globals_ == NULL){ main_module = PyImport_AddModule("__main__"); globals_ = PyModule_GetDict(main_module); Py_XINCREF(globals_); } if (locals_ == NULL){ locals_ = PyDict_New(); if (!locals_){ cerr << "Could not initialize locals dict" << endl; } } initcompiled_ = (PyCodeObject*)Py_CompileString( initstr_.c_str(), Py_GetProgramName(), Py_file_input); if (!initcompiled_){ cerr << "Error compiling initString" << endl; handleError(true); } else { PyEval_EvalCode(initcompiled_, globals_, locals_); if (PyErr_Occurred()){ PyErr_Print (); } } runcompiled_ = (PyCodeObject*)Py_CompileString( runstr_.c_str(), Py_GetProgramName(), Py_file_input); if (!runcompiled_){ cerr << "Error compiling runString" << endl; handleError(true); } else { PyEval_EvalCode(runcompiled_, globals_, locals_); if (PyErr_Occurred()){ PyErr_Print (); } } }
/* External interface */ void Py_SetPath(const wchar_t *path) { if (module_search_path != NULL) { free(module_search_path); module_search_path = NULL; } if (path != NULL) { extern wchar_t *Py_GetProgramName(void); wchar_t *prog = Py_GetProgramName(); wcsncpy(progpath, prog, MAXPATHLEN); exec_prefix[0] = prefix[0] = L'\0'; module_search_path = malloc((wcslen(path) + 1) * sizeof(wchar_t)); if (module_search_path != NULL) wcscpy(module_search_path, path); } }
static void calcPathes() { if(pathCalculated) return; char* p = stpcpy(progPath, Py_GetProgramName()); while(--p > progPath) { if(*p == '/') { *p = 0; break; } } strcpy(modulePathes, progPath); strcat(modulePathes, "/pylib/lib:"); strcat(modulePathes, progPath); strcat(modulePathes, "/pylib/otherlibs"); strcpy(execPrefixPath, progPath); strcat(execPrefixPath, "/pylib/exec"); pathCalculated = 1; }
void Py_SetPath(const wchar_t *path) { if (path == NULL) { _PyPathConfig_Clear(&_Py_path_config); return; } PyMemAllocatorEx old_alloc; _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); _PyPathConfig new_config; new_config.program_full_path = _PyMem_RawWcsdup(Py_GetProgramName()); int alloc_error = (new_config.program_full_path == NULL); new_config.prefix = _PyMem_RawWcsdup(L""); alloc_error |= (new_config.prefix == NULL); #ifdef MS_WINDOWS new_config.dll_path = _PyMem_RawWcsdup(L""); alloc_error |= (new_config.dll_path == NULL); #else new_config.exec_prefix = _PyMem_RawWcsdup(L""); alloc_error |= (new_config.exec_prefix == NULL); #endif new_config.module_search_path = _PyMem_RawWcsdup(path); alloc_error |= (new_config.module_search_path == NULL); /* steal the home and program_name values (to leave them unchanged) */ new_config.home = _Py_path_config.home; _Py_path_config.home = NULL; new_config.program_name = _Py_path_config.program_name; _Py_path_config.program_name = NULL; _PyPathConfig_Clear(&_Py_path_config); _Py_path_config = new_config; PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); if (alloc_error) { Py_FatalError("Py_SetPath() failed: out of memory"); } }
static void calculate_path() { char *pypath = getenv("Python$Path"); if (pypath) { int pathlen = strlen(pypath); module_search_path = malloc(pathlen + 1); if (module_search_path) strncpy(module_search_path, pypath, pathlen + 1); else { fprintf(stderr, "Not enough memory for dynamic PYTHONPATH.\n" "Using default static PYTHONPATH.\n"); } } if (!module_search_path) module_search_path = "<Python$Dir>.Lib"; prefix = "<Python$Dir>"; exec_prefix = prefix; progpath = Py_GetProgramName(); }
static void calculate_path(void) { extern wchar_t *Py_GetProgramName(void); static wchar_t delimiter[2] = {DELIM, '\0'}; static wchar_t separator[2] = {SEP, '\0'}; char *_rtpypath = Py_GETENV("PYTHONPATH"); /* XXX use wide version on Windows */ wchar_t rtpypath[MAXPATHLEN+1]; wchar_t *home = Py_GetPythonHome(); char *_path = getenv("PATH"); wchar_t *path_buffer = NULL; wchar_t *path = NULL; wchar_t *prog = Py_GetProgramName(); wchar_t argv0_path[MAXPATHLEN+1]; wchar_t zip_path[MAXPATHLEN+1]; int pfound, efound; /* 1 if found; -1 if found build directory */ wchar_t *buf; size_t bufsz; size_t prefixsz; wchar_t *defpath; #ifdef WITH_NEXT_FRAMEWORK NSModule pythonModule; #endif #ifdef __APPLE__ #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4 uint32_t nsexeclength = MAXPATHLEN; #else unsigned long nsexeclength = MAXPATHLEN; #endif char execpath[MAXPATHLEN+1]; #endif wchar_t *_pythonpath, *_prefix, *_exec_prefix; _pythonpath = _Py_char2wchar(PYTHONPATH, NULL); _prefix = _Py_char2wchar(PREFIX, NULL); _exec_prefix = _Py_char2wchar(EXEC_PREFIX, NULL); if (!_pythonpath || !_prefix || !_exec_prefix) { Py_FatalError( "Unable to decode path variables in getpath.c: " "memory error"); } if (_path) { path_buffer = _Py_char2wchar(_path, NULL); path = path_buffer; } /* 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 (wcschr(prog, SEP)) wcsncpy(progpath, prog, MAXPATHLEN); #ifdef __APPLE__ /* On Mac OS X, if a script uses an interpreter of the form * "#!/opt/python2.3/bin/python", the kernel only passes "python" * as argv[0], which falls through to the $PATH search below. * If /opt/python2.3/bin isn't in your path, or is near the end, * this algorithm may incorrectly find /usr/bin/python. To work * around this, we can use _NSGetExecutablePath to get a better * hint of what the intended interpreter was, although this * will fail if a relative path was used. but in that case, * absolutize() should help us out below */ else if(0 == _NSGetExecutablePath(execpath, &nsexeclength) && execpath[0] == SEP) { size_t r = mbstowcs(progpath, execpath, MAXPATHLEN+1); if (r == (size_t)-1 || r > MAXPATHLEN) { /* Could not convert execpath, or it's too long. */ progpath[0] = '\0'; } } #endif /* __APPLE__ */ else if (path) { while (1) { wchar_t *delim = wcschr(path, DELIM); if (delim) { size_t len = delim - path; if (len > MAXPATHLEN) len = MAXPATHLEN; wcsncpy(progpath, path, len); *(progpath + len) = '\0'; } else wcsncpy(progpath, path, MAXPATHLEN); joinpath(progpath, prog); if (isxfile(progpath)) break; if (!delim) { progpath[0] = L'\0'; break; } path = delim + 1; } } else progpath[0] = '\0'; if (path_buffer != NULL) PyMem_Free(path_buffer); if (progpath[0] != SEP && progpath[0] != '\0') absolutize(progpath); wcsncpy(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 = (wchar_t *)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. */ wcsncpy(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 */ wcsncpy(argv0_path, progpath, MAXPATHLEN); } else { /* Use the location of the library as the progpath */ wcsncpy(argv0_path, buf, MAXPATHLEN); } } #endif #if HAVE_READLINK { wchar_t tmpbuffer[MAXPATHLEN+1]; int linklen = _Py_wreadlink(progpath, tmpbuffer, MAXPATHLEN); while (linklen != -1) { if (tmpbuffer[0] == SEP) /* tmpbuffer should never be longer than MAXPATHLEN, but extra check does not hurt */ wcsncpy(argv0_path, tmpbuffer, MAXPATHLEN); else { /* Interpret relative to progpath */ reduce(argv0_path); joinpath(argv0_path, tmpbuffer); } linklen = _Py_wreadlink(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, _prefix))) { if (!Py_FrozenFlag) fprintf(stderr, "Could not find platform independent libraries <prefix>\n"); wcsncpy(prefix, _prefix, MAXPATHLEN); joinpath(prefix, lib_python); } else reduce(prefix); wcsncpy(zip_path, prefix, MAXPATHLEN); zip_path[MAXPATHLEN] = L'\0'; if (pfound > 0) { /* Use the reduced prefix returned by Py_GetPrefix() */ reduce(zip_path); reduce(zip_path); } else wcsncpy(zip_path, _prefix, MAXPATHLEN); joinpath(zip_path, L"lib/python00.zip"); bufsz = wcslen(zip_path); /* Replace "00" with version */ zip_path[bufsz - 6] = VERSION[0]; zip_path[bufsz - 5] = VERSION[2]; if (!(efound = search_for_exec_prefix(argv0_path, home, _exec_prefix))) { if (!Py_FrozenFlag) fprintf(stderr, "Could not find platform dependent libraries <exec_prefix>\n"); wcsncpy(exec_prefix, _exec_prefix, MAXPATHLEN); joinpath(exec_prefix, L"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) { size_t s = mbstowcs(rtpypath, _rtpypath, sizeof(rtpypath)/sizeof(wchar_t)); if (s == (size_t)-1 || s >=sizeof(rtpypath)) /* XXX deal with errors more gracefully */ _rtpypath = NULL; if (_rtpypath) bufsz += wcslen(rtpypath) + 1; } defpath = _pythonpath; prefixsz = wcslen(prefix) + 1; while (1) { wchar_t *delim = wcschr(defpath, DELIM); if (defpath[0] != SEP) /* Paths are relative to prefix */ bufsz += prefixsz; if (delim) bufsz += delim - defpath + 1; else { bufsz += wcslen(defpath) + 1; break; } defpath = delim + 1; } bufsz += wcslen(zip_path) + 1; bufsz += wcslen(exec_prefix) + 1; /* This is the only malloc call in this file */ buf = (wchar_t *)PyMem_Malloc(bufsz*sizeof(wchar_t)); 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 = L"" PYTHONPATH; } else { /* Run-time value of $PYTHONPATH goes first */ if (_rtpypath) { wcscpy(buf, rtpypath); wcscat(buf, delimiter); } else buf[0] = '\0'; /* Next is the default zip path */ wcscat(buf, zip_path); wcscat(buf, delimiter); /* Next goes merge of compile-time $PYTHONPATH with * dynamically located prefix. */ defpath = _pythonpath; while (1) { wchar_t *delim = wcschr(defpath, DELIM); if (defpath[0] != SEP) { wcscat(buf, prefix); wcscat(buf, separator); } if (delim) { size_t len = delim - defpath + 1; size_t end = wcslen(buf) + len; wcsncat(buf, defpath, len); *(buf + end) = '\0'; } else { wcscat(buf, defpath); break; } defpath = delim + 1; } wcscat(buf, delimiter); /* Finally, on goes the directory for dynamic-load modules */ wcscat(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); /* The prefix is the root directory, but reduce() chopped * off the "/". */ if (!prefix[0]) wcscpy(prefix, separator); } else wcsncpy(prefix, _prefix, MAXPATHLEN); if (efound > 0) { reduce(exec_prefix); reduce(exec_prefix); reduce(exec_prefix); if (!exec_prefix[0]) wcscpy(exec_prefix, separator); } else wcsncpy(exec_prefix, _exec_prefix, MAXPATHLEN); PyMem_Free(_pythonpath); PyMem_Free(_prefix); PyMem_Free(_exec_prefix); }
static void get_progpath(void) { extern char *Py_GetProgramName(void); char *path = getenv("PATH"); char *prog = Py_GetProgramName(); #ifdef MS_WINDOWS extern HANDLE PyWin_DLLhModule; #ifdef UNICODE WCHAR wprogpath[MAXPATHLEN+1]; /* Windows documents that GetModuleFileName() will "truncate", but makes no mention of the null terminator. Play it safe. PLUS Windows itself defines MAX_PATH as the same, but anyway... */ #ifdef Py_ENABLE_SHARED wprogpath[MAXPATHLEN]=_T('\0'); if (PyWin_DLLhModule && GetModuleFileName(PyWin_DLLhModule, wprogpath, MAXPATHLEN)) { WideCharToMultiByte(CP_ACP, 0, wprogpath, -1, dllpath, MAXPATHLEN+1, NULL, NULL); } #else dllpath[0] = 0; #endif wprogpath[MAXPATHLEN]=_T('\0'); if (GetModuleFileName(NULL, wprogpath, MAXPATHLEN)) { WideCharToMultiByte(CP_ACP, 0, wprogpath, -1, progpath, MAXPATHLEN+1, NULL, NULL); return; } #else /* static init of progpath ensures final char remains \0 */ #ifdef Py_ENABLE_SHARED if (PyWin_DLLhModule) if (!GetModuleFileName(PyWin_DLLhModule, dllpath, MAXPATHLEN)) dllpath[0] = 0; #else dllpath[0] = 0; #endif if (GetModuleFileName(NULL, progpath, MAXPATHLEN)) return; #endif #endif if (prog == NULL || *prog == '\0') prog = "python"; /* 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. */ #ifdef ALTSEP if (strchr(prog, SEP) || strchr(prog, ALTSEP)) #else if (strchr(prog, SEP)) #endif strncpy(progpath, prog, MAXPATHLEN); else if (path) { while (1) { char *delim = strchr(path, DELIM); if (delim) { size_t len = delim - path; /* ensure we can't overwrite buffer */ len = min(MAXPATHLEN,len); strncpy(progpath, path, len); *(progpath + len) = '\0'; } else strncpy(progpath, path, MAXPATHLEN); /* join() is safe for MAXPATHLEN+1 size buffer */ join(progpath, prog); if (exists(progpath)) break; if (!delim) { progpath[0] = '\0'; break; } path = delim + 1; } } else progpath[0] = '\0'; }
static void get_progpath(void) { extern wchar_t *Py_GetProgramName(void); wchar_t *path = _wgetenv(L"PATH"); wchar_t *prog = Py_GetProgramName(); #ifdef Py_ENABLE_SHARED extern HANDLE PyWin_DLLhModule; /* static init of progpath ensures final char remains \0 */ if (PyWin_DLLhModule) if (!GetModuleFileNameW(PyWin_DLLhModule, dllpath, MAXPATHLEN)) dllpath[0] = 0; #else dllpath[0] = 0; #endif if (GetModuleFileNameW(NULL, progpath, MAXPATHLEN)) return; if (prog == NULL || *prog == '\0') prog = L"python"; /* 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. */ #ifdef ALTSEP if (wcschr(prog, SEP) || wcschr(prog, ALTSEP)) #else if (wcschr(prog, SEP)) #endif wcsncpy(progpath, prog, MAXPATHLEN); else if (path) { while (1) { wchar_t *delim = wcschr(path, DELIM); if (delim) { size_t len = delim - path; /* ensure we can't overwrite buffer */ len = min(MAXPATHLEN,len); wcsncpy(progpath, path, len); *(progpath + len) = '\0'; } else wcsncpy(progpath, path, MAXPATHLEN); /* join() is safe for MAXPATHLEN+1 size buffer */ join(progpath, prog); if (exists(progpath)) break; if (!delim) { progpath[0] = '\0'; break; } path = delim + 1; } } else progpath[0] = '\0'; }
PyObject* initializePython2(const std::vector<char*>& commandLineArgsUtf8) #endif { //See https://developer.blender.org/T31507 //Python will not load anything in site-packages if this is set //We are sure that nothing in system wide site-packages is loaded, for instance on OS X with Python installed //through macports on the system, the following printf show the following: /*Py_GetProgramName is /Applications/Natron.app/Contents/MacOS/Natron Py_GetPrefix is /Applications/Natron.app/Contents/MacOS/../Frameworks/Python.framework/Versions/2.7 Py_GetExecPrefix is /Applications/Natron.app/Contents/MacOS/../Frameworks/Python.framework/Versions/2.7 Py_GetProgramFullPath is /Applications/Natron.app/Contents/MacOS/Natron Py_GetPath is /Applications/Natron.app/Contents/MacOS/../Frameworks/Python.framework/Versions/2.7/lib/python2.7:/Applications/Natron.app/Contents/MacOS/../Plugins:/Applications/Natron.app/Contents/MacOS/../Frameworks/Python.framework/Versions/2.7/lib/python27.zip:/Applications/Natron.app/Contents/MacOS/../Frameworks/Python.framework/Versions/2.7/lib/python2.7/:/Applications/Natron.app/Contents/MacOS/../Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin:/Applications/Natron.app/Contents/MacOS/../Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac:/Applications/Natron.app/Contents/MacOS/../Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages:/Applications/Natron.app/Contents/MacOS/../Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk:/Applications/Natron.app/Contents/MacOS/../Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old:/Applications/Natron.app/Contents/MacOS/../Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload Py_GetPythonHome is ../Frameworks/Python.framework/Versions/2.7/lib Python library is in /Applications/Natron.app/Contents/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages*/ //Py_NoSiteFlag = 1; ///////////////////////////////////////// // Py_SetProgramName ///////////////////////////////////////// // // Must be done before Py_Initialize (see doc of Py_Initialize) // #if PY_MAJOR_VERSION >= 3 // Python 3 Py_SetProgramName(commandLineArgsWide[0]); #else // Python 2 printf( "Py_SetProgramName(\"%s\")\n", commandLineArgsUtf8[0] ); Py_SetProgramName(commandLineArgsUtf8[0]); #endif ///////////////////////////////////////// // Py_Initialize ///////////////////////////////////////// // // Initialize the Python interpreter. In an application embedding Python, this should be called before using any other Python/C API functions; with the exception of Py_SetProgramName(), Py_SetPythonHome() and Py_SetPath(). #if defined(NATRON_CONFIG_SNAPSHOT) || defined(DEBUG) printf("Py_Initialize()\n"); #endif Py_Initialize(); // pythonHome must be const, so that the c_str() pointer is never invalidated ///////////////////////////////////////// // PySys_SetArgv ///////////////////////////////////////// // #if PY_MAJOR_VERSION >= 3 // Python 3 PySys_SetArgv( commandLineArgsWide.size(), const_cast<wchar_t**>(&commandLineArgsWide[0]) ); /// relative module import #else // Python 2 PySys_SetArgv( commandLineArgsUtf8.size(), const_cast<char**>(&commandLineArgsUtf8[0]) ); /// relative module import #endif PyObject* mainModule = PyImport_ImportModule("__main__"); //create main module , new ref //See http://wiki.blender.org/index.php/Dev:2.4/Source/Python/API/Threads //Python releases the GIL every 100 virtual Python instructions, we do not want that to happen in the middle of an expression. //_PyEval_SetSwitchInterval(LONG_MAX); //See answer for http://stackoverflow.com/questions/15470367/pyeval-initthreads-in-python-3-how-when-to-call-it-the-saga-continues-ad-naus PyEval_InitThreads(); ///Do as per http://wiki.blender.org/index.php/Dev:2.4/Source/Python/API/Threads ///All calls to the Python API should call PythonGILLocker beforehand. //_imp->mainThreadState = PyGILState_GetThisThreadState(); //PyEval_ReleaseThread(_imp->mainThreadState); std::string err; #if defined(NATRON_CONFIG_SNAPSHOT) || defined(DEBUG) /// print info about python lib { printf( "PATH is %s\n", Py_GETENV("PATH") ); printf( "PYTHONPATH is %s\n", Py_GETENV("PYTHONPATH") ); printf( "PYTHONHOME is %s\n", Py_GETENV("PYTHONHOME") ); printf( "Py_DebugFlag is %d\n", Py_DebugFlag ); printf( "Py_VerboseFlag is %d\n", Py_VerboseFlag ); printf( "Py_InteractiveFlag is %d\n", Py_InteractiveFlag ); printf( "Py_InspectFlag is %d\n", Py_InspectFlag ); printf( "Py_OptimizeFlag is %d\n", Py_OptimizeFlag ); printf( "Py_NoSiteFlag is %d\n", Py_NoSiteFlag ); printf( "Py_BytesWarningFlag is %d\n", Py_BytesWarningFlag ); printf( "Py_UseClassExceptionsFlag is %d\n", Py_UseClassExceptionsFlag ); printf( "Py_FrozenFlag is %d\n", Py_FrozenFlag ); printf( "Py_TabcheckFlag is %d\n", Py_TabcheckFlag ); printf( "Py_UnicodeFlag is %d\n", Py_UnicodeFlag ); printf( "Py_IgnoreEnvironmentFlag is %d\n", Py_IgnoreEnvironmentFlag ); printf( "Py_DivisionWarningFlag is %d\n", Py_DivisionWarningFlag ); printf( "Py_DontWriteBytecodeFlag is %d\n", Py_DontWriteBytecodeFlag ); printf( "Py_NoUserSiteDirectory is %d\n", Py_NoUserSiteDirectory ); printf( "Py_GetProgramName is %s\n", Py_GetProgramName() ); printf( "Py_GetPrefix is %s\n", Py_GetPrefix() ); printf( "Py_GetExecPrefix is %s\n", Py_GetPrefix() ); printf( "Py_GetProgramFullPath is %s\n", Py_GetProgramFullPath() ); printf( "Py_GetPath is %s\n", Py_GetPath() ); printf( "Py_GetPythonHome is %s\n", Py_GetPythonHome() ); PyObject* dict = PyModule_GetDict(mainModule); ///This is faster than PyRun_SimpleString since is doesn't call PyImport_AddModule("__main__") std::string script("from distutils.sysconfig import get_python_lib; print('Python library is in ' + get_python_lib())"); PyObject* v = PyRun_String(script.c_str(), Py_file_input, dict, 0); if (v) { Py_DECREF(v); } } #endif return mainModule; } // initializePython
std::wstring PySetup::get_program_name() { return Py_GetProgramName(); }
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]; char zip_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 = (char *)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); strncpy(zip_path, prefix, MAXPATHLEN); zip_path[MAXPATHLEN] = '\0'; if (pfound > 0) { /* Use the reduced prefix returned by Py_GetPrefix() */ reduce(zip_path); reduce(zip_path); } else strncpy(zip_path, PREFIX, MAXPATHLEN); joinpath(zip_path, "lib/python00.zip"); bufsz = strlen(zip_path); /* Replace "00" with version */ zip_path[bufsz - 6] = VERSION[0]; zip_path[bufsz - 5] = VERSION[2]; 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(zip_path) + 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 is the default zip path */ strcat(buf, zip_path); strcat(buf, delimiter); /* 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); }
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 = 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 #ifdef WITH_NEXT_FRAMEWORK /* XXX Need to check this code for buffer overflows */ 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. */ strcpy(progpath, buf); /* Frameworks have support for versioning */ strcpy(lib_python, "lib"); } else { /* If we're not in a framework, fall back to the old way (even though NSNameOfModule() probably does the same thing.) */ #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) { int bufspace = MAXPATHLEN; while (1) { char *delim = strchr(path, DELIM); if (delim) { size_t len = delim - path; if (len > bufspace) len = bufspace; strncpy(progpath, path, len); *(progpath + len) = '\0'; bufspace -= len; } else strncpy(progpath, path, bufspace); joinpath(progpath, prog); if (isxfile(progpath)) break; if (!delim) { progpath[0] = '\0'; break; } path = delim + 1; } } else progpath[0] = '\0'; #ifdef WITH_NEXT_FRAMEWORK } #endif strncpy(argv0_path, progpath, MAXPATHLEN); #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); }
static void calculate_path (void) { extern char *Py_GetProgramName(void); static char delimiter[2] = {DELIM, '\0'}; char *rtpypath = Py_GETENV("PYTHONPATH"); char *path = getenv("PATH"); char *prog = Py_GetProgramName(); static char proc_exe_path[] = "/proc/self/exe"; char *xzip_path; char *buf; /* 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 && progpath[0] != '\0') absolutize(progpath); /**** pts ****/ { int fd = open(proc_exe_path, O_RDONLY); /* fprintf(stderr, "progpath=(%s)\n", progpath); */ if (fd < 0) { /* If /proc is not avaialbe, e.g. in chroot */ xzip_path = progpath; /* Use argv[0] for the .zip filename */ } else { xzip_path = proc_exe_path; close(fd); } } /**** pts ****/ if (rtpypath == NULL || rtpypath[0] == '\0') { module_search_path = xzip_path; } else if (NULL == (buf = (char *)PyMem_Malloc( 2 + strlen(xzip_path) + strlen(rtpypath)))) { /* 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 = xzip_path; } else { strcpy(buf, rtpypath); strcat(buf, delimiter); strcat(buf, xzip_path); module_search_path = buf; } }
/** Determine paths. Two directories must be found, the platform independent directory (prefix), containing the common .py and .pyc files, and the platform dependent directory (exec_prefix), containing the shared library modules. Note that prefix and exec_prefix are the same directory for UEFI installations. Separate searches are carried out for prefix and exec_prefix. Each search tries a number of different locations until a ``landmark'' file or directory is found. If no prefix or exec_prefix is found, a warning message is issued and the preprocessor defined PREFIX and EXEC_PREFIX are used (even though they may not work); python carries on as best as is possible, but some imports may fail. Before any searches are done, the location of the executable is determined. If argv[0] has one or more slashes in it, it is used unchanged. Otherwise, it must have been invoked from the shell's path, so we search %PATH% for the named executable and use that. If the executable was not found on %PATH% (or there was no %PATH% environment variable), the original argv[0] string is used. Finally, argv0_path is set to the directory containing the executable (i.e. the last component is stripped). With argv0_path in hand, we perform a number of steps. The same steps are performed for prefix and for exec_prefix, but with a different landmark. The prefix landmark will always be lib/python.VERSION/os.py and the exec_prefix will always be lib/python.VERSION/dynaload, where VERSION is Python's version number as defined at the beginning of this file. First. See if the %PYTHONHOME% environment variable points to the installed location of the Python libraries. If %PYTHONHOME% is set, then it points to prefix and exec_prefix. %PYTHONHOME% can be a single directory, which is used for both, or the prefix and exec_prefix directories separated by the DELIM character. Next. Search the directories pointed to by the preprocessor variables PREFIX and EXEC_PREFIX. These paths are prefixed with the volume name extracted from argv0_path. The volume names correspond to the UEFI shell "map" names. That's it! Well, almost. Once we have determined prefix and exec_prefix, the preprocessor variable PYTHONPATH is used to construct a path. Each relative path on PYTHONPATH is prefixed with prefix. Then the directory containing the shared library modules is appended. The environment variable $PYTHONPATH is inserted in front of it all. Finally, the prefix and exec_prefix globals are tweaked so they reflect the values expected by other code, by stripping the "lib/python$VERSION/..." stuff off. This seems to make more sense given that currently the only known use of sys.prefix and sys.exec_prefix is for the ILU installation process to find the installed Python tree. The final, fully resolved, paths should look something like: fs0:/Efi/Tools/python.efi fs0:/Efi/StdLib/lib/python27 fs0:/Efi/StdLib/lib/python27/dynaload **/ 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]; char zip_path[MAXPATHLEN+1]; char *buf; size_t bufsz; size_t prefixsz; char *defpath; /* ########################################################################### Determine path to the Python.efi binary. Produces progpath, argv0_path, and volume_name. ########################################################################### */ /* 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 ( (!is_absolute(progpath)) && (progpath[0] != '\0') ) absolutize(progpath); strncpy(argv0_path, progpath, MAXPATHLEN); argv0_path[MAXPATHLEN] = '\0'; set_volume(volume_name, argv0_path); reduce(argv0_path); /* At this point, argv0_path is guaranteed to be less than MAXPATHLEN bytes long. */ /* ########################################################################### Build the FULL prefix string, including volume name. This is the full path to the platform independent libraries. ########################################################################### */ strncpy(prefix, volume_name, MAXPATHLEN); joinpath(prefix, PREFIX); joinpath(prefix, lib_python); /* ########################################################################### Build the FULL path to the zipped-up Python library. ########################################################################### */ strncpy(zip_path, prefix, MAXPATHLEN); zip_path[MAXPATHLEN] = '\0'; reduce(zip_path); joinpath(zip_path, "python00.zip"); bufsz = strlen(zip_path); /* Replace "00" with version */ zip_path[bufsz - 6] = VERSION[0]; zip_path[bufsz - 5] = VERSION[1]; /* ########################################################################### Build the FULL path to dynamically loadable libraries. ########################################################################### */ strncpy(exec_prefix, volume_name, MAXPATHLEN); // "fs0:" joinpath(exec_prefix, EXEC_PREFIX); // "fs0:/Efi/StdLib" joinpath(exec_prefix, lib_python); // "fs0:/Efi/StdLib/lib/python.27" joinpath(exec_prefix, "lib-dynload"); // "fs0:/Efi/StdLib/lib/python.27/lib-dynload" /* ########################################################################### Build the module search path. ########################################################################### */ /* 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. */ reduce(prefix); reduce(prefix); /* The prefix is the root directory, but reduce() chopped * off the "/". */ if (!prefix[0]) { strcpy(prefix, volume_name); } bufsz = strlen(prefix); if(prefix[bufsz-1] == ':') { // if prefix consists solely of a volume_name prefix[bufsz] = SEP; // then append SEP indicating the root directory prefix[bufsz+1] = 0; // and ensure the new string is terminated } /* Calculate size of return buffer. */ defpath = pythonpath; bufsz = 0; if (rtpypath) bufsz += strlen(rtpypath) + 1; prefixsz = strlen(prefix) + 1; while (1) { char *delim = strchr(defpath, DELIM); if (is_absolute(defpath) == 0) /* Paths are relative to prefix */ bufsz += prefixsz; if (delim) bufsz += delim - defpath + 1; else { bufsz += strlen(defpath) + 1; break; } defpath = delim + 1; } bufsz += strlen(zip_path) + 1; bufsz += strlen(exec_prefix) + 1; /* This is the only malloc call in this file */ buf = (char *)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 is the default zip path */ strcat(buf, zip_path); strcat(buf, delimiter); /* Next goes merge of compile-time $PYTHONPATH with * dynamically located prefix. */ defpath = pythonpath; while (1) { char *delim = strchr(defpath, DELIM); if (is_absolute(defpath) != 1) { 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; } /* At this point, exec_prefix is set to VOL:/Efi/StdLib/lib/python.27/dynalib. We want to get back to the root value, so we have to remove the final three segments to get VOL:/Efi/StdLib. Because we don't know what VOL is, and EXEC_PREFIX is also indeterminate, we just remove the three final segments. */ reduce(exec_prefix); reduce(exec_prefix); reduce(exec_prefix); if (!exec_prefix[0]) { strcpy(exec_prefix, volume_name); } bufsz = strlen(exec_prefix); if(exec_prefix[bufsz-1] == ':') { exec_prefix[bufsz] = SEP; exec_prefix[bufsz+1] = 0; } if (Py_VerboseFlag) PySys_WriteStderr("%s[%d]: module_search_path = \"%s\"\n", __func__, __LINE__, module_search_path); if (Py_VerboseFlag) PySys_WriteStderr("%s[%d]: prefix = \"%s\"\n", __func__, __LINE__, prefix); if (Py_VerboseFlag) PySys_WriteStderr("%s[%d]: exec_prefix = \"%s\"\n", __func__, __LINE__, exec_prefix); if (Py_VerboseFlag) PySys_WriteStderr("%s[%d]: progpath = \"%s\"\n", __func__, __LINE__, progpath); }
/* * Class: org_jpy_PyLib * Method: startPython0 * Signature: ([Ljava/lang/String;)Z */ JNIEXPORT jboolean JNICALL Java_org_jpy_PyLib_startPython0 (JNIEnv* jenv, jclass jLibClass, jobjectArray jPathArray) { int pyInit = Py_IsInitialized(); JPy_DIAG_PRINT(JPy_DIAG_F_ALL, "PyLib_startPython: entered: jenv=%p, pyInit=%d, JPy_Module=%p\n", jenv, pyInit, JPy_Module); if (!pyInit) { Py_Initialize(); PyLib_RedirectStdOut(); pyInit = Py_IsInitialized(); } if (pyInit) { if (JPy_DiagFlags != 0) { printf("PyLib_startPython: global Python interpreter information:\n"); #if defined(JPY_COMPAT_33P) printf(" Py_GetProgramName() = \"%ls\"\n", Py_GetProgramName()); printf(" Py_GetPrefix() = \"%ls\"\n", Py_GetPrefix()); printf(" Py_GetExecPrefix() = \"%ls\"\n", Py_GetExecPrefix()); printf(" Py_GetProgramFullPath() = \"%ls\"\n", Py_GetProgramFullPath()); printf(" Py_GetPath() = \"%ls\"\n", Py_GetPath()); printf(" Py_GetPythonHome() = \"%ls\"\n", Py_GetPythonHome()); #elif defined(JPY_COMPAT_27) printf(" Py_GetProgramName() = \"%s\"\n", Py_GetProgramName()); printf(" Py_GetPrefix() = \"%s\"\n", Py_GetPrefix()); printf(" Py_GetExecPrefix() = \"%s\"\n", Py_GetExecPrefix()); printf(" Py_GetProgramFullPath() = \"%s\"\n", Py_GetProgramFullPath()); printf(" Py_GetPath() = \"%s\"\n", Py_GetPath()); printf(" Py_GetPythonHome() = \"%s\"\n", Py_GetPythonHome()); #endif printf(" Py_GetVersion() = \"%s\"\n", Py_GetVersion()); printf(" Py_GetPlatform() = \"%s\"\n", Py_GetPlatform()); printf(" Py_GetCompiler() = \"%s\"\n", Py_GetCompiler()); printf(" Py_GetBuildInfo() = \"%s\"\n", Py_GetBuildInfo()); } // If we've got jPathArray, add all entries to Python's "sys.path" // if (jPathArray != NULL) { PyObject* pyPathList; PyObject* pyPath; jstring jPath; jsize i, pathCount; pathCount = (*jenv)->GetArrayLength(jenv, jPathArray); //printf(">> pathCount=%d\n", pathCount); if (pathCount > 0) { JPy_BEGIN_GIL_STATE pyPathList = PySys_GetObject("path"); //printf(">> pyPathList=%p, len=%ld\n", pyPathList, PyList_Size(pyPathList)); if (pyPathList != NULL) { Py_INCREF(pyPathList); for (i = pathCount - 1; i >= 0; i--) { jPath = (*jenv)->GetObjectArrayElement(jenv, jPathArray, i); //printf(">> i=%d, jPath=%p\n", i, jPath); if (jPath != NULL) { pyPath = JPy_FromJString(jenv, jPath); //printf(">> i=%d, pyPath=%p\n", i, pyPath); if (pyPath != NULL) { PyList_Insert(pyPathList, 0, pyPath); } } } Py_DECREF(pyPathList); } //printf(">> pyPathList=%p, len=%ld\n", pyPathList, PyList_Size(pyPathList)); //printf(">> pyPathList=%p, len=%ld (check)\n", PySys_GetObject("path"), PyList_Size(PySys_GetObject("path"))); JPy_END_GIL_STATE } }