Skip to content

bpo-32030: Add Python/pathconfig.c #4668

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 1 commit into from
Dec 1, 2017
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
4 changes: 4 additions & 0 deletions Include/internal/pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ typedef struct {

#define _PyPathConfig_INIT {.module_search_path = NULL}

PyAPI_DATA(_PyPathConfig) _Py_path_config;

PyAPI_FUNC(void) _PyPathConfig_Clear(_PyPathConfig *config);


/* Full Python runtime state */

Expand Down
1 change: 0 additions & 1 deletion Include/pylifecycle.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ PyAPI_FUNC(wchar_t *) Py_GetPath(void);
#ifdef Py_BUILD_CORE
PyAPI_FUNC(_PyInitError) _PyPathConfig_Init(
const _PyMainInterpreterConfig *main_config);
PyAPI_FUNC(void) _PyPathConfig_Fini(void);
#endif
PyAPI_FUNC(void) Py_SetPath(const wchar_t *);
#ifdef MS_WINDOWS
Expand Down
3 changes: 2 additions & 1 deletion Makefile.pre.in
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,9 @@ PYTHON_OBJS= \
Python/importdl.o \
Python/marshal.o \
Python/modsupport.o \
Python/mystrtoul.o \
Python/mysnprintf.o \
Python/mystrtoul.o \
Python/pathconfig.o \
Python/peephole.o \
Python/pyarena.o \
Python/pyctype.o \
Expand Down
114 changes: 1 addition & 113 deletions Modules/getpath.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ typedef struct {

static const wchar_t delimiter[2] = {DELIM, '\0'};
static const wchar_t separator[2] = {SEP, '\0'};
static _PyPathConfig _Py_path_config = _PyPathConfig_INIT;


/* Get file status. Encode the path to the locale encoding. */
Expand Down Expand Up @@ -1009,23 +1008,6 @@ calculate_path_impl(const _PyMainInterpreterConfig *main_config,
}


static void
pathconfig_clear(_PyPathConfig *config)
{
#define CLEAR(ATTR) \
do { \
PyMem_RawFree(ATTR); \
ATTR = NULL; \
} while (0)

CLEAR(config->prefix);
CLEAR(config->exec_prefix);
CLEAR(config->program_full_path);
CLEAR(config->module_search_path);
#undef CLEAR
}


/* Initialize paths for Py_GetPath(), Py_GetPrefix(), Py_GetExecPrefix()
and Py_GetProgramFullPath() */
_PyInitError
Expand All @@ -1049,7 +1031,7 @@ _PyPathConfig_Init(const _PyMainInterpreterConfig *main_config)

err = calculate_path_impl(main_config, &calculate, &new_path_config);
if (_Py_INIT_FAILED(err)) {
pathconfig_clear(&new_path_config);
_PyPathConfig_Clear(&new_path_config);
goto done;
}

Expand All @@ -1061,100 +1043,6 @@ _PyPathConfig_Init(const _PyMainInterpreterConfig *main_config)
return err;
}


static void
pathconfig_global_init(void)
{
if (_Py_path_config.module_search_path) {
/* Already initialized */
return;
}

_PyInitError err;
_PyMainInterpreterConfig config = _PyMainInterpreterConfig_INIT;

err = _PyMainInterpreterConfig_ReadEnv(&config);
if (_Py_INIT_FAILED(err)) {
goto error;
}

err = _PyMainInterpreterConfig_Read(&config);
if (_Py_INIT_FAILED(err)) {
goto error;
}

err = _PyPathConfig_Init(&config);
if (_Py_INIT_FAILED(err)) {
goto error;
}

_PyMainInterpreterConfig_Clear(&config);
return;

error:
_PyMainInterpreterConfig_Clear(&config);
_Py_FatalInitError(err);
}


void
_PyPathConfig_Fini(void)
{
pathconfig_clear(&_Py_path_config);
}


/* External interface */
void
Py_SetPath(const wchar_t *path)
{
if (path == NULL) {
pathconfig_clear(&_Py_path_config);
return;
}

_PyPathConfig new_config;
new_config.program_full_path = _PyMem_RawWcsdup(Py_GetProgramName());
new_config.exec_prefix = _PyMem_RawWcsdup(L"");
new_config.prefix = _PyMem_RawWcsdup(L"");
new_config.module_search_path = _PyMem_RawWcsdup(path);

pathconfig_clear(&_Py_path_config);
_Py_path_config = new_config;
}


wchar_t *
Py_GetPath(void)
{
pathconfig_global_init();
return _Py_path_config.module_search_path;
}


wchar_t *
Py_GetPrefix(void)
{
pathconfig_global_init();
return _Py_path_config.prefix;
}


wchar_t *
Py_GetExecPrefix(void)
{
pathconfig_global_init();
return _Py_path_config.exec_prefix;
}


wchar_t *
Py_GetProgramFullPath(void)
{
pathconfig_global_init();
return _Py_path_config.program_full_path;
}

#ifdef __cplusplus
}
#endif
6 changes: 3 additions & 3 deletions Modules/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1665,12 +1665,12 @@ pymain_impl(_PyMain *pymain)
pymain->status = 120;
}

/* _PyPathConfig_Fini() cannot be called in Py_FinalizeEx().
/* _PyPathConfig_Clear() cannot be called in Py_FinalizeEx().
Py_Initialize() and Py_Finalize() can be called multiple times, but it
must not "forget" parameters set by Py_SetProgramName(), Py_SetPath() or
Py_SetPythonHome(), whereas _PyPathConfig_Fini() clear all these
Py_SetPythonHome(), whereas _PyPathConfig_Clear() clear all these
parameters. */
_PyPathConfig_Fini();
_PyPathConfig_Clear(&_Py_path_config);

return 0;
}
Expand Down
119 changes: 1 addition & 118 deletions PC/getpathp.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,6 @@ typedef struct {
} PyCalculatePath;


static _PyPathConfig _Py_path_config = _PyPathConfig_INIT;


/* determine if "ch" is a separator character */
static int
is_sep(wchar_t ch)
Expand Down Expand Up @@ -1061,23 +1058,6 @@ calculate_free(PyCalculatePath *calculate)
}


static void
pathconfig_clear(_PyPathConfig *config)
{
#define CLEAR(ATTR) \
do { \
PyMem_RawFree(ATTR); \
ATTR = NULL; \
} while (0)

CLEAR(config->prefix);
CLEAR(config->program_full_path);
CLEAR(config->dll_path);
CLEAR(config->module_search_path);
#undef CLEAR
}


/* Initialize paths for Py_GetPath(), Py_GetPrefix(), Py_GetExecPrefix()
and Py_GetProgramFullPath() */
_PyInitError
Expand Down Expand Up @@ -1108,110 +1088,13 @@ _PyPathConfig_Init(const _PyMainInterpreterConfig *main_config)

done:
if (_Py_INIT_FAILED(err)) {
pathconfig_clear(&new_path_config);
_PyPathConfig_Clear(&new_path_config);
}
calculate_free(&calculate);
return err;
}


static void
pathconfig_global_init(void)
{
if (_Py_path_config.module_search_path) {
/* Already initialized */
return;
}

_PyInitError err;
_PyMainInterpreterConfig config = _PyMainInterpreterConfig_INIT;

err = _PyMainInterpreterConfig_ReadEnv(&config);
if (_Py_INIT_FAILED(err)) {
goto error;
}

err = _PyMainInterpreterConfig_Read(&config);
if (_Py_INIT_FAILED(err)) {
goto error;
}

err = _PyPathConfig_Init(&config);
if (_Py_INIT_FAILED(err)) {
goto error;
}

_PyMainInterpreterConfig_Clear(&config);
return;

error:
_PyMainInterpreterConfig_Clear(&config);
_Py_FatalInitError(err);
}


void
_PyPathConfig_Fini(void)
{
pathconfig_clear(&_Py_path_config);
}


/* External interface */

void
Py_SetPath(const wchar_t *path)
{
if (_Py_path_config.module_search_path != NULL) {
pathconfig_clear(&_Py_path_config);
}

if (path == NULL) {
return;
}

_PyPathConfig new_config;
new_config.program_full_path = _PyMem_RawWcsdup(Py_GetProgramName());
new_config.prefix = _PyMem_RawWcsdup(L"");
new_config.dll_path = _PyMem_RawWcsdup(L"");
new_config.module_search_path = _PyMem_RawWcsdup(path);

pathconfig_clear(&_Py_path_config);
_Py_path_config = new_config;
}


wchar_t *
Py_GetPath(void)
{
pathconfig_global_init();
return _Py_path_config.module_search_path;
}


wchar_t *
Py_GetPrefix(void)
{
pathconfig_global_init();
return _Py_path_config.prefix;
}


wchar_t *
Py_GetExecPrefix(void)
{
return Py_GetPrefix();
}


wchar_t *
Py_GetProgramFullPath(void)
{
pathconfig_global_init();
return _Py_path_config.program_full_path;
}


/* Load python3.dll before loading any extension module that might refer
to it. That way, we can be sure that always the python3.dll corresponding
to this python DLL is loaded, not a python3.dll that might be on the path
Expand Down
1 change: 1 addition & 0 deletions PCbuild/pythoncore.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@
<ClCompile Include="..\Python\modsupport.c" />
<ClCompile Include="..\Python\mysnprintf.c" />
<ClCompile Include="..\Python\mystrtoul.c" />
<ClCompile Include="..\Python\pathconfig.c" />
<ClCompile Include="..\Python\peephole.c" />
<ClCompile Include="..\Python\pyarena.c" />
<ClCompile Include="..\Python\pyctype.c" />
Expand Down
3 changes: 3 additions & 0 deletions PCbuild/pythoncore.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,9 @@
<ClCompile Include="..\Python\mystrtoul.c">
<Filter>Python</Filter>
</ClCompile>
<ClCompile Include="..\Python\pathconfig.c">
<Filter>Python</Filter>
</ClCompile>
<ClCompile Include="..\Python\peephole.c">
<Filter>Python</Filter>
</ClCompile>
Expand Down
Loading