summaryrefslogtreecommitdiff
path: root/Python/pylifecycle.c
diff options
context:
space:
mode:
authorNick Coghlan <ncoghlan@gmail.com>2017-10-25 12:11:26 +1000
committerGitHub <noreply@github.com>2017-10-25 12:11:26 +1000
commitd7ac06126db86f76ba92cbca4cb702852a321f78 (patch)
tree0742294efd6ef24d4f7644099d421c97346fee93 /Python/pylifecycle.c
parent850a18e03e8f8309bc8c39adc6e7d51a4568cd9a (diff)
downloadcpython-git-d7ac06126db86f76ba92cbca4cb702852a321f78.tar.gz
bpo-31845: Fix reading flags from environment (GH-4105)
The startup refactoring means command line settings are now applied after settings are read from the environment. This updates the way command line settings are applied to account for that, ensures more settings are first read from the environment in _PyInitializeCore, and adds a simple test case covering the flags that are easy to check.
Diffstat (limited to 'Python/pylifecycle.c')
-rw-r--r--Python/pylifecycle.c37
1 files changed, 23 insertions, 14 deletions
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
index 5b13bc4582..4e8bbb662e 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -217,15 +217,18 @@ Py_SetStandardStreamEncoding(const char *encoding, const char *errors)
*/
-static int
-add_flag(int flag, const char *envs)
+static void
+set_flag(int *flag, const char *envs)
{
+ /* Helper to set flag variables from environment variables:
+ * - uses the higher of the two values if they're both set
+ * - otherwise sets the flag to 1
+ */
int env = atoi(envs);
- if (flag < env)
- flag = env;
- if (flag < 1)
- flag = 1;
- return flag;
+ if (*flag < env)
+ *flag = env;
+ if (*flag < 1)
+ *flag = 1;
}
static char*
@@ -612,22 +615,28 @@ void _Py_InitializeCore(const _PyCoreConfig *config)
#endif
if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
- Py_DebugFlag = add_flag(Py_DebugFlag, p);
+ set_flag(&Py_DebugFlag, p);
if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
- Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
+ set_flag(&Py_VerboseFlag, p);
if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
- Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
+ set_flag(&Py_OptimizeFlag, p);
+ if ((p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
+ set_flag(&Py_InspectFlag, p);
if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
- Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
+ set_flag(&Py_DontWriteBytecodeFlag, p);
+ if ((p = Py_GETENV("PYTHONNOUSERSITE")) && *p != '\0')
+ set_flag(&Py_NoUserSiteDirectory, p);
+ if ((p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
+ set_flag(&Py_UnbufferedStdioFlag, p);
/* The variable is only tested for existence here;
_Py_HashRandomization_Init will check its value further. */
if ((p = Py_GETENV("PYTHONHASHSEED")) && *p != '\0')
- Py_HashRandomizationFlag = add_flag(Py_HashRandomizationFlag, p);
+ set_flag(&Py_HashRandomizationFlag, p);
#ifdef MS_WINDOWS
if ((p = Py_GETENV("PYTHONLEGACYWINDOWSFSENCODING")) && *p != '\0')
- Py_LegacyWindowsFSEncodingFlag = add_flag(Py_LegacyWindowsFSEncodingFlag, p);
+ set_flag(&Py_LegacyWindowsFSEncodingFlag, p);
if ((p = Py_GETENV("PYTHONLEGACYWINDOWSSTDIO")) && *p != '\0')
- Py_LegacyWindowsStdioFlag = add_flag(Py_LegacyWindowsStdioFlag, p);
+ set_flag(&Py_LegacyWindowsStdioFlag, p);
#endif
_Py_HashRandomization_Init(&core_config);