bpo-32030: Update sys.path[0] earlier for main importer by vstinner · Pull Request #4868 · python/cpython
static int pymain_run_main_from_importer(_PyMain *pymain) { /* Assume sys_path0 has already been checked by pymain_get_importer(), * so put it in sys.path[0] and import __main__ */ PyObject *sys_path = PySys_GetObject("path"); if (sys_path == NULL) { PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path"); goto error; }
if (PyList_Insert(sys_path, 0, pymain->main_importer_path)) { goto error; }
int sts = pymain_run_module(L"__main__", 0); return (sts != 0);
error: Py_CLEAR(pymain->main_importer_path); PyErr_Print(); return 1; }
static wchar_t* pymain_wstrdup(_PyMain *pymain, const wchar_t *str) {
if (pymain->main_importer_path != NULL) { /* Let pymain_run_main_from_importer() adjust sys.path[0] later */ return 0; /* Assume path0 has already been checked by pymain_get_importer(), so put it in sys.path[0] */ path0 = pymain->main_importer_path; Py_INCREF(path0); }
if (Py_IsolatedFlag) { else if (Py_IsolatedFlag) { /* Don't modify sys.path in isolated mode */ return 0; } else { /* Prepend argv[0] to sys.path. If argv[0] is a symlink, use the real path. */ path0 = _PyPathConfig_ComputeArgv0(pymain->sys_argc, pymain->sys_argv); if (path0 == NULL) { pymain->err = _Py_INIT_NO_MEMORY(); return -1; } }
/* Prepend argv[0] to sys.path. If argv[0] is a symlink, use the real path. */ /* Prepend path0 to sys.path */ PyObject *sys_path = PySys_GetObject("path"); if (sys_path == NULL) { Py_DECREF(path0); pymain->err = _Py_INIT_ERR("can't get sys.path"); return -1; }
PyObject *path0 = _PyPathConfig_ComputeArgv0(pymain->sys_argc, pymain->sys_argv); if (path0 == NULL) { pymain->err = _Py_INIT_NO_MEMORY(); return -1; }
/* Prepend path0 to sys.path */ if (PyList_Insert(sys_path, 0, path0) < 0) { Py_DECREF(path0); pymain->err = _Py_INIT_ERR("sys.path.insert(0, path0) failed");
if (pymain->main_importer_path != NULL) { pymain->status = pymain_run_main_from_importer(pymain); /* Import __main__. Note: sys_path0 was put in sys.path[0] by pymain_update_sys_path(). */ int sts = pymain_run_module(L"__main__", 0); pymain->status = (sts != 0); return; }