PyAPI_FUNC(wchar_t *) Py_GetPath(void);
#ifdef Py_BUILD_CORE
PyAPI_FUNC(_PyInitError) _PyPathConfig_Init(const _PyCoreConfig *core_config);
-PyAPI_FUNC(PyObject*) _PyPathConfig_ComputeArgv0(int argc, wchar_t **argv);
+PyAPI_FUNC(int) _PyPathConfig_ComputeArgv0(
+ int argc, wchar_t **argv,
+ PyObject **argv0_p);
PyAPI_FUNC(int) _Py_FindEnvConfigValue(
FILE *env_file,
const wchar_t *key,
--- /dev/null
+At Python initialization, the current directory is no longer prepended to
+:data:`sys.path` if it has been removed.
}
-static int
-pymain_compute_path0(_PyMain *pymain, _PyCoreConfig *config, PyObject **path0)
-{
- if (pymain->main_importer_path != NULL) {
- /* Let pymain_run_main_from_importer() adjust sys.path[0] later */
- *path0 = NULL;
- return 0;
- }
-
- if (Py_IsolatedFlag) {
- *path0 = NULL;
- return 0;
- }
-
- *path0 = _PyPathConfig_ComputeArgv0(config->argc, config->argv);
- if (*path0 == NULL) {
- pymain->err = _Py_INIT_NO_MEMORY();
- return -1;
- }
- return 0;
-}
-
-
static int
pymain_update_sys_path(_PyMain *pymain, PyObject *path0)
{
pymain->main_importer_path = pymain_get_importer(pymain->filename);
}
- PyObject *path0;
- if (pymain_compute_path0(pymain, config, &path0) < 0) {
+ if (pymain->main_importer_path != NULL) {
+ /* Let pymain_run_main_from_importer() adjust sys.path[0] later */
+ return 0;
+ }
+
+ if (Py_IsolatedFlag) {
+ return 0;
+ }
+
+ PyObject *path0 = NULL;
+ if (!_PyPathConfig_ComputeArgv0(config->argc, config->argv, &path0)) {
+ return 0;
+ }
+ if (path0 == NULL) {
+ pymain->err = _Py_INIT_NO_MEMORY();
return -1;
}
- if (path0 != NULL) {
- if (pymain_update_sys_path(pymain, path0) < 0) {
- Py_DECREF(path0);
- return -1;
- }
+ if (pymain_update_sys_path(pymain, path0) < 0) {
Py_DECREF(path0);
+ return -1;
}
+ Py_DECREF(path0);
return 0;
}
}
/* Compute argv[0] which will be prepended to sys.argv */
-PyObject*
-_PyPathConfig_ComputeArgv0(int argc, wchar_t **argv)
+int
+_PyPathConfig_ComputeArgv0(int argc, wchar_t **argv, PyObject **argv0_p)
{
wchar_t *argv0;
wchar_t *p = NULL;
wchar_t fullpath[MAX_PATH];
#endif
+ assert(*argv0_p == NULL);
+
argv0 = argv[0];
if (argc > 0 && argv0 != NULL) {
have_module_arg = (wcscmp(argv0, L"-m") == 0);
if (have_module_arg) {
#if defined(HAVE_REALPATH) || defined(MS_WINDOWS)
- _Py_wgetcwd(fullpath, Py_ARRAY_LENGTH(fullpath));
+ if (!_Py_wgetcwd(fullpath, Py_ARRAY_LENGTH(fullpath))) {
+ return 0;
+ }
argv0 = fullpath;
n = wcslen(argv0);
#else
}
#endif /* All others */
- return PyUnicode_FromWideChar(argv0, n);
+ *argv0_p = PyUnicode_FromWideChar(argv0, n);
+ return 1;
}
if (updatepath) {
/* If argv[0] is not '-c' nor '-m', prepend argv[0] to sys.path.
If argv[0] is a symlink, use the real path. */
- PyObject *argv0 = _PyPathConfig_ComputeArgv0(argc, argv);
+ PyObject *argv0 = NULL;
+ if (!_PyPathConfig_ComputeArgv0(argc, argv, &argv0)) {
+ return;
+ }
if (argv0 == NULL) {
Py_FatalError("can't compute path0 from argv");
}