Skip to content

[3.7] bpo-34725: Adds _Py_SetProgramFullPath so embedders may override sys.executable (GH-9860) #9861

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Include/pylifecycle.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ PyAPI_FUNC(void) Py_SetPythonHome(const wchar_t *);
PyAPI_FUNC(wchar_t *) Py_GetPythonHome(void);

#ifndef Py_LIMITED_API
PyAPI_FUNC(void) _Py_SetProgramFullPath(const wchar_t *);

/* Only used by applications that embed the interpreter and need to
* override the standard encoding determination mechanism
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Adds _Py_SetProgramFullPath so embedders may override sys.executable
26 changes: 26 additions & 0 deletions Modules/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1206,6 +1206,25 @@ config_init_program_name(_PyCoreConfig *config)
}


static _PyInitError
config_init_executable(_PyCoreConfig *config)
{
assert(config->executable == NULL);

/* If Py_SetProgramFullPath() was called, use its value */
const wchar_t *program_full_path = _Py_path_config.program_full_path;
if (program_full_path != NULL) {
config->executable = _PyMem_RawWcsdup(program_full_path);
if (config->executable == NULL) {
return _Py_INIT_NO_MEMORY();
}
return _Py_INIT_OK();
}

return _Py_INIT_OK();
}


static void
pymain_header(_PyMain *pymain)
{
Expand Down Expand Up @@ -2350,6 +2369,13 @@ _PyCoreConfig_Read(_PyCoreConfig *config)
}
}

if (config->executable == NULL) {
err = config_init_executable(config);
if (_Py_INIT_FAILED(err)) {
return err;
}
}

if (config->utf8_mode < 0 || config->coerce_c_locale < 0) {
config_init_locale(config);
}
Expand Down
21 changes: 21 additions & 0 deletions Python/pathconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,27 @@ Py_SetProgramName(const wchar_t *program_name)
}


void
_Py_SetProgramFullPath(const wchar_t *program_full_path)
{
if (program_full_path == NULL || program_full_path[0] == L'\0') {
return;
}

PyMemAllocatorEx old_alloc;
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);

PyMem_RawFree(_Py_path_config.program_full_path);
_Py_path_config.program_full_path = _PyMem_RawWcsdup(program_full_path);

PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);

if (_Py_path_config.program_full_path == NULL) {
Py_FatalError("Py_SetProgramFullPath() failed: out of memory");
}
}


wchar_t *
Py_GetPath(void)
{
Expand Down