Skip to content

Commit f78a5e9

Browse files
authored
bpo-36301: Add _Py_GetEnv() function (GH-12542)
* Make _PyPreConfig_GetEnv(), _PyCoreConfig_GetEnv() and _PyCoreConfig_GetEnvDup() private * _Py_get_env_flag() first parameter becomes "int use_environment"
1 parent 548cb60 commit f78a5e9

File tree

4 files changed

+34
-32
lines changed

4 files changed

+34
-32
lines changed

‎Include/internal/pycore_coreconfig.h‎

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,17 @@ PyAPI_FUNC(int) _Py_str_to_int(
7676
PyAPI_FUNC(const wchar_t*) _Py_get_xoption(
7777
const _PyWstrList *xoptions,
7878
const wchar_t *name);
79+
PyAPI_FUNC(const char*) _Py_GetEnv(
80+
int use_environment,
81+
const char *name);
7982

8083
PyAPI_FUNC(void) _PyPreConfig_Clear(_PyPreConfig *config);
8184
PyAPI_FUNC(int) _PyPreConfig_Copy(_PyPreConfig *config,
8285
const _PyPreConfig *config2);
8386
PyAPI_FUNC(void) _PyPreConfig_GetGlobalConfig(_PyPreConfig *config);
8487
PyAPI_FUNC(void) _PyPreConfig_SetGlobalConfig(const _PyPreConfig *config);
85-
PyAPI_FUNC(const char*) _PyPreConfig_GetEnv(const _PyPreConfig *config,
86-
const char *name);
87-
PyAPI_FUNC(void) _Py_get_env_flag(_PyPreConfig *config,
88+
PyAPI_FUNC(void) _Py_get_env_flag(
89+
int use_environment,
8890
int *flag,
8991
const char *name);
9092
PyAPI_FUNC(_PyInitError) _PyPreConfig_Read(_PyPreConfig *config,
@@ -107,14 +109,6 @@ PyAPI_FUNC(_PyInitError) _PyCoreConfig_SetPathConfig(
107109
const _PyCoreConfig *config);
108110
PyAPI_FUNC(void) _PyCoreConfig_GetGlobalConfig(_PyCoreConfig *config);
109111
PyAPI_FUNC(void) _PyCoreConfig_SetGlobalConfig(const _PyCoreConfig *config);
110-
PyAPI_FUNC(const char*) _PyCoreConfig_GetEnv(
111-
const _PyCoreConfig *config,
112-
const char *name);
113-
PyAPI_FUNC(int) _PyCoreConfig_GetEnvDup(
114-
const _PyCoreConfig *config,
115-
wchar_t **dest,
116-
wchar_t *wname,
117-
char *name);
118112
PyAPI_FUNC(_PyInitError) _PyCoreConfig_Read(_PyCoreConfig *config);
119113
PyAPI_FUNC(_PyInitError) _PyCoreConfig_ReadFromArgv(_PyCoreConfig *config,
120114
const _PyArgv *args);

‎Modules/main.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ pymain_run_file(_PyCoreConfig *config, PyCompilerFlags *cf)
655655
static void
656656
pymain_run_startup(_PyCoreConfig *config, PyCompilerFlags *cf)
657657
{
658-
const char *startup = _PyCoreConfig_GetEnv(config, "PYTHONSTARTUP");
658+
const char *startup = _Py_GetEnv(config->preconfig.use_environment, "PYTHONSTARTUP");
659659
if (startup == NULL) {
660660
return;
661661
}
@@ -735,7 +735,7 @@ pymain_repl(_PyCoreConfig *config, PyCompilerFlags *cf, int *exitcode)
735735
{
736736
/* Check this environment variable at the end, to give programs the
737737
opportunity to set it from Python. */
738-
if (!Py_InspectFlag && _PyCoreConfig_GetEnv(config, "PYTHONINSPECT")) {
738+
if (!Py_InspectFlag && _Py_GetEnv(config->preconfig.use_environment, "PYTHONINSPECT")) {
739739
Py_InspectFlag = 1;
740740
config->inspect = 1;
741741
}

‎Python/coreconfig.c‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -610,14 +610,14 @@ _PyCoreConfig_Copy(_PyCoreConfig *config, const _PyCoreConfig *config2)
610610
}
611611

612612

613-
const char*
613+
static const char*
614614
_PyCoreConfig_GetEnv(const _PyCoreConfig *config, const char *name)
615615
{
616-
return _PyPreConfig_GetEnv(&config->preconfig, name);
616+
return _Py_GetEnv(config->preconfig.use_environment, name);
617617
}
618618

619619

620-
int
620+
static int
621621
_PyCoreConfig_GetEnvDup(const _PyCoreConfig *config,
622622
wchar_t **dest,
623623
wchar_t *wname, char *name)
@@ -924,34 +924,34 @@ config_wstr_to_int(const wchar_t *wstr, int *result)
924924
static _PyInitError
925925
config_read_env_vars(_PyCoreConfig *config)
926926
{
927-
_PyPreConfig *preconfig = &config->preconfig;
927+
int use_env = config->preconfig.use_environment;
928928

929929
/* Get environment variables */
930-
_Py_get_env_flag(preconfig, &config->parser_debug, "PYTHONDEBUG");
931-
_Py_get_env_flag(preconfig, &config->verbose, "PYTHONVERBOSE");
932-
_Py_get_env_flag(preconfig, &config->optimization_level, "PYTHONOPTIMIZE");
933-
_Py_get_env_flag(preconfig, &config->inspect, "PYTHONINSPECT");
930+
_Py_get_env_flag(use_env, &config->parser_debug, "PYTHONDEBUG");
931+
_Py_get_env_flag(use_env, &config->verbose, "PYTHONVERBOSE");
932+
_Py_get_env_flag(use_env, &config->optimization_level, "PYTHONOPTIMIZE");
933+
_Py_get_env_flag(use_env, &config->inspect, "PYTHONINSPECT");
934934

935935
int dont_write_bytecode = 0;
936-
_Py_get_env_flag(preconfig, &dont_write_bytecode, "PYTHONDONTWRITEBYTECODE");
936+
_Py_get_env_flag(use_env, &dont_write_bytecode, "PYTHONDONTWRITEBYTECODE");
937937
if (dont_write_bytecode) {
938938
config->write_bytecode = 0;
939939
}
940940

941941
int no_user_site_directory = 0;
942-
_Py_get_env_flag(preconfig, &no_user_site_directory, "PYTHONNOUSERSITE");
942+
_Py_get_env_flag(use_env, &no_user_site_directory, "PYTHONNOUSERSITE");
943943
if (no_user_site_directory) {
944944
config->user_site_directory = 0;
945945
}
946946

947947
int unbuffered_stdio = 0;
948-
_Py_get_env_flag(preconfig, &unbuffered_stdio, "PYTHONUNBUFFERED");
948+
_Py_get_env_flag(use_env, &unbuffered_stdio, "PYTHONUNBUFFERED");
949949
if (unbuffered_stdio) {
950950
config->buffered_stdio = 0;
951951
}
952952

953953
#ifdef MS_WINDOWS
954-
_Py_get_env_flag(preconfig, &config->legacy_windows_stdio,
954+
_Py_get_env_flag(use_env, &config->legacy_windows_stdio,
955955
"PYTHONLEGACYWINDOWSSTDIO");
956956
#endif
957957

‎Python/preconfig.c‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -270,11 +270,11 @@ _PyPreConfig_SetGlobalConfig(const _PyPreConfig *config)
270270

271271

272272
const char*
273-
_PyPreConfig_GetEnv(const _PyPreConfig *config, const char *name)
273+
_Py_GetEnv(int use_environment, const char *name)
274274
{
275-
assert(config->use_environment >= 0);
275+
assert(use_environment >= 0);
276276

277-
if (!config->use_environment) {
277+
if (!use_environment) {
278278
return NULL;
279279
}
280280

@@ -288,6 +288,13 @@ _PyPreConfig_GetEnv(const _PyPreConfig *config, const char *name)
288288
}
289289

290290

291+
static const char*
292+
_PyPreConfig_GetEnv(const _PyPreConfig *config, const char *name)
293+
{
294+
return _Py_GetEnv(config->use_environment, name);
295+
}
296+
297+
291298
int
292299
_Py_str_to_int(const char *str, int *result)
293300
{
@@ -307,9 +314,9 @@ _Py_str_to_int(const char *str, int *result)
307314

308315

309316
void
310-
_Py_get_env_flag(_PyPreConfig *config, int *flag, const char *name)
317+
_Py_get_env_flag(int use_environment, int *flag, const char *name)
311318
{
312-
const char *var = _PyPreConfig_GetEnv(config, name);
319+
const char *var = _Py_GetEnv(use_environment, name);
313320
if (!var) {
314321
return;
315322
}
@@ -434,8 +441,9 @@ preconfig_read(_PyPreConfig *config, _PyPreCmdline *cmdline)
434441
/* legacy_windows_fs_encoding, utf8_mode, coerce_c_locale */
435442
if (config->use_environment) {
436443
#ifdef MS_WINDOWS
437-
_Py_get_env_flag(config, &config->legacy_windows_fs_encoding,
438-
"PYTHONLEGACYWINDOWSFSENCODING");
444+
_Py_get_env_flag(config->use_environment,
445+
&config->legacy_windows_fs_encoding,
446+
"PYTHONLEGACYWINDOWSFSENCODING");
439447
#endif
440448

441449
const char *env = _PyPreConfig_GetEnv(config, "PYTHONCOERCECLOCALE");

0 commit comments

Comments
 (0)