Skip to content

Commit 0ea395a

Browse files
authored
bpo-32030: Add Python/pathconfig.c (#4668)
* Factorize code from PC/getpathp.c and Modules/getpath.c to remove duplicated code * rename pathconfig_clear() to _PyPathConfig_Clear() * Inline _PyPathConfig_Fini() in pymain_impl() and then remove it, since it's a oneliner
1 parent ebac19d commit 0ea395a

File tree

10 files changed

+204
-291
lines changed

10 files changed

+204
-291
lines changed

Include/internal/pystate.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ typedef struct {
5252

5353
#define _PyPathConfig_INIT {.module_search_path = NULL}
5454

55+
PyAPI_DATA(_PyPathConfig) _Py_path_config;
56+
57+
PyAPI_FUNC(void) _PyPathConfig_Clear(_PyPathConfig *config);
58+
5559

5660
/* Full Python runtime state */
5761

Include/pylifecycle.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ PyAPI_FUNC(wchar_t *) Py_GetPath(void);
105105
#ifdef Py_BUILD_CORE
106106
PyAPI_FUNC(_PyInitError) _PyPathConfig_Init(
107107
const _PyMainInterpreterConfig *main_config);
108-
PyAPI_FUNC(void) _PyPathConfig_Fini(void);
109108
#endif
110109
PyAPI_FUNC(void) Py_SetPath(const wchar_t *);
111110
#ifdef MS_WINDOWS

Makefile.pre.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,9 @@ PYTHON_OBJS= \
337337
Python/importdl.o \
338338
Python/marshal.o \
339339
Python/modsupport.o \
340-
Python/mystrtoul.o \
341340
Python/mysnprintf.o \
341+
Python/mystrtoul.o \
342+
Python/pathconfig.o \
342343
Python/peephole.o \
343344
Python/pyarena.o \
344345
Python/pyctype.o \

Modules/getpath.c

Lines changed: 1 addition & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ typedef struct {
132132

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

137136

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

10111010

1012-
static void
1013-
pathconfig_clear(_PyPathConfig *config)
1014-
{
1015-
#define CLEAR(ATTR) \
1016-
do { \
1017-
PyMem_RawFree(ATTR); \
1018-
ATTR = NULL; \
1019-
} while (0)
1020-
1021-
CLEAR(config->prefix);
1022-
CLEAR(config->exec_prefix);
1023-
CLEAR(config->program_full_path);
1024-
CLEAR(config->module_search_path);
1025-
#undef CLEAR
1026-
}
1027-
1028-
10291011
/* Initialize paths for Py_GetPath(), Py_GetPrefix(), Py_GetExecPrefix()
10301012
and Py_GetProgramFullPath() */
10311013
_PyInitError
@@ -1049,7 +1031,7 @@ _PyPathConfig_Init(const _PyMainInterpreterConfig *main_config)
10491031

10501032
err = calculate_path_impl(main_config, &calculate, &new_path_config);
10511033
if (_Py_INIT_FAILED(err)) {
1052-
pathconfig_clear(&new_path_config);
1034+
_PyPathConfig_Clear(&new_path_config);
10531035
goto done;
10541036
}
10551037

@@ -1061,100 +1043,6 @@ _PyPathConfig_Init(const _PyMainInterpreterConfig *main_config)
10611043
return err;
10621044
}
10631045

1064-
1065-
static void
1066-
pathconfig_global_init(void)
1067-
{
1068-
if (_Py_path_config.module_search_path) {
1069-
/* Already initialized */
1070-
return;
1071-
}
1072-
1073-
_PyInitError err;
1074-
_PyMainInterpreterConfig config = _PyMainInterpreterConfig_INIT;
1075-
1076-
err = _PyMainInterpreterConfig_ReadEnv(&config);
1077-
if (_Py_INIT_FAILED(err)) {
1078-
goto error;
1079-
}
1080-
1081-
err = _PyMainInterpreterConfig_Read(&config);
1082-
if (_Py_INIT_FAILED(err)) {
1083-
goto error;
1084-
}
1085-
1086-
err = _PyPathConfig_Init(&config);
1087-
if (_Py_INIT_FAILED(err)) {
1088-
goto error;
1089-
}
1090-
1091-
_PyMainInterpreterConfig_Clear(&config);
1092-
return;
1093-
1094-
error:
1095-
_PyMainInterpreterConfig_Clear(&config);
1096-
_Py_FatalInitError(err);
1097-
}
1098-
1099-
1100-
void
1101-
_PyPathConfig_Fini(void)
1102-
{
1103-
pathconfig_clear(&_Py_path_config);
1104-
}
1105-
1106-
1107-
/* External interface */
1108-
void
1109-
Py_SetPath(const wchar_t *path)
1110-
{
1111-
if (path == NULL) {
1112-
pathconfig_clear(&_Py_path_config);
1113-
return;
1114-
}
1115-
1116-
_PyPathConfig new_config;
1117-
new_config.program_full_path = _PyMem_RawWcsdup(Py_GetProgramName());
1118-
new_config.exec_prefix = _PyMem_RawWcsdup(L"");
1119-
new_config.prefix = _PyMem_RawWcsdup(L"");
1120-
new_config.module_search_path = _PyMem_RawWcsdup(path);
1121-
1122-
pathconfig_clear(&_Py_path_config);
1123-
_Py_path_config = new_config;
1124-
}
1125-
1126-
1127-
wchar_t *
1128-
Py_GetPath(void)
1129-
{
1130-
pathconfig_global_init();
1131-
return _Py_path_config.module_search_path;
1132-
}
1133-
1134-
1135-
wchar_t *
1136-
Py_GetPrefix(void)
1137-
{
1138-
pathconfig_global_init();
1139-
return _Py_path_config.prefix;
1140-
}
1141-
1142-
1143-
wchar_t *
1144-
Py_GetExecPrefix(void)
1145-
{
1146-
pathconfig_global_init();
1147-
return _Py_path_config.exec_prefix;
1148-
}
1149-
1150-
1151-
wchar_t *
1152-
Py_GetProgramFullPath(void)
1153-
{
1154-
pathconfig_global_init();
1155-
return _Py_path_config.program_full_path;
1156-
}
1157-
11581046
#ifdef __cplusplus
11591047
}
11601048
#endif

Modules/main.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,12 +1665,12 @@ pymain_impl(_PyMain *pymain)
16651665
pymain->status = 120;
16661666
}
16671667

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

16751675
return 0;
16761676
}

PC/getpathp.c

Lines changed: 1 addition & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,6 @@ typedef struct {
130130
} PyCalculatePath;
131131

132132

133-
static _PyPathConfig _Py_path_config = _PyPathConfig_INIT;
134-
135-
136133
/* determine if "ch" is a separator character */
137134
static int
138135
is_sep(wchar_t ch)
@@ -1061,23 +1058,6 @@ calculate_free(PyCalculatePath *calculate)
10611058
}
10621059

10631060

1064-
static void
1065-
pathconfig_clear(_PyPathConfig *config)
1066-
{
1067-
#define CLEAR(ATTR) \
1068-
do { \
1069-
PyMem_RawFree(ATTR); \
1070-
ATTR = NULL; \
1071-
} while (0)
1072-
1073-
CLEAR(config->prefix);
1074-
CLEAR(config->program_full_path);
1075-
CLEAR(config->dll_path);
1076-
CLEAR(config->module_search_path);
1077-
#undef CLEAR
1078-
}
1079-
1080-
10811061
/* Initialize paths for Py_GetPath(), Py_GetPrefix(), Py_GetExecPrefix()
10821062
and Py_GetProgramFullPath() */
10831063
_PyInitError
@@ -1108,110 +1088,13 @@ _PyPathConfig_Init(const _PyMainInterpreterConfig *main_config)
11081088

11091089
done:
11101090
if (_Py_INIT_FAILED(err)) {
1111-
pathconfig_clear(&new_path_config);
1091+
_PyPathConfig_Clear(&new_path_config);
11121092
}
11131093
calculate_free(&calculate);
11141094
return err;
11151095
}
11161096

11171097

1118-
static void
1119-
pathconfig_global_init(void)
1120-
{
1121-
if (_Py_path_config.module_search_path) {
1122-
/* Already initialized */
1123-
return;
1124-
}
1125-
1126-
_PyInitError err;
1127-
_PyMainInterpreterConfig config = _PyMainInterpreterConfig_INIT;
1128-
1129-
err = _PyMainInterpreterConfig_ReadEnv(&config);
1130-
if (_Py_INIT_FAILED(err)) {
1131-
goto error;
1132-
}
1133-
1134-
err = _PyMainInterpreterConfig_Read(&config);
1135-
if (_Py_INIT_FAILED(err)) {
1136-
goto error;
1137-
}
1138-
1139-
err = _PyPathConfig_Init(&config);
1140-
if (_Py_INIT_FAILED(err)) {
1141-
goto error;
1142-
}
1143-
1144-
_PyMainInterpreterConfig_Clear(&config);
1145-
return;
1146-
1147-
error:
1148-
_PyMainInterpreterConfig_Clear(&config);
1149-
_Py_FatalInitError(err);
1150-
}
1151-
1152-
1153-
void
1154-
_PyPathConfig_Fini(void)
1155-
{
1156-
pathconfig_clear(&_Py_path_config);
1157-
}
1158-
1159-
1160-
/* External interface */
1161-
1162-
void
1163-
Py_SetPath(const wchar_t *path)
1164-
{
1165-
if (_Py_path_config.module_search_path != NULL) {
1166-
pathconfig_clear(&_Py_path_config);
1167-
}
1168-
1169-
if (path == NULL) {
1170-
return;
1171-
}
1172-
1173-
_PyPathConfig new_config;
1174-
new_config.program_full_path = _PyMem_RawWcsdup(Py_GetProgramName());
1175-
new_config.prefix = _PyMem_RawWcsdup(L"");
1176-
new_config.dll_path = _PyMem_RawWcsdup(L"");
1177-
new_config.module_search_path = _PyMem_RawWcsdup(path);
1178-
1179-
pathconfig_clear(&_Py_path_config);
1180-
_Py_path_config = new_config;
1181-
}
1182-
1183-
1184-
wchar_t *
1185-
Py_GetPath(void)
1186-
{
1187-
pathconfig_global_init();
1188-
return _Py_path_config.module_search_path;
1189-
}
1190-
1191-
1192-
wchar_t *
1193-
Py_GetPrefix(void)
1194-
{
1195-
pathconfig_global_init();
1196-
return _Py_path_config.prefix;
1197-
}
1198-
1199-
1200-
wchar_t *
1201-
Py_GetExecPrefix(void)
1202-
{
1203-
return Py_GetPrefix();
1204-
}
1205-
1206-
1207-
wchar_t *
1208-
Py_GetProgramFullPath(void)
1209-
{
1210-
pathconfig_global_init();
1211-
return _Py_path_config.program_full_path;
1212-
}
1213-
1214-
12151098
/* Load python3.dll before loading any extension module that might refer
12161099
to it. That way, we can be sure that always the python3.dll corresponding
12171100
to this python DLL is loaded, not a python3.dll that might be on the path

PCbuild/pythoncore.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@
381381
<ClCompile Include="..\Python\modsupport.c" />
382382
<ClCompile Include="..\Python\mysnprintf.c" />
383383
<ClCompile Include="..\Python\mystrtoul.c" />
384+
<ClCompile Include="..\Python\pathconfig.c" />
384385
<ClCompile Include="..\Python\peephole.c" />
385386
<ClCompile Include="..\Python\pyarena.c" />
386387
<ClCompile Include="..\Python\pyctype.c" />

PCbuild/pythoncore.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,9 @@
896896
<ClCompile Include="..\Python\mystrtoul.c">
897897
<Filter>Python</Filter>
898898
</ClCompile>
899+
<ClCompile Include="..\Python\pathconfig.c">
900+
<Filter>Python</Filter>
901+
</ClCompile>
899902
<ClCompile Include="..\Python\peephole.c">
900903
<Filter>Python</Filter>
901904
</ClCompile>

0 commit comments

Comments
 (0)