summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorNick Coghlan <ncoghlan@gmail.com>2017-10-25 02:11:26 (GMT)
committerGitHub <noreply@github.com>2017-10-25 02:11:26 (GMT)
commitd7ac06126db86f76ba92cbca4cb702852a321f78 (patch)
tree0742294efd6ef24d4f7644099d421c97346fee93 /Lib
parent850a18e03e8f8309bc8c39adc6e7d51a4568cd9a (diff)
downloadcpython-d7ac06126db86f76ba92cbca4cb702852a321f78.zip
cpython-d7ac06126db86f76ba92cbca4cb702852a321f78.tar.gz
cpython-d7ac06126db86f76ba92cbca4cb702852a321f78.tar.bz2
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 'Lib')
-rw-r--r--Lib/test/test_cmd_line.py35
1 files changed, 34 insertions, 1 deletions
diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py
index 28ddb2b..1b584eb 100644
--- a/Lib/test/test_cmd_line.py
+++ b/Lib/test/test_cmd_line.py
@@ -12,7 +12,6 @@ from test.support.script_helper import (
spawn_python, kill_python, assert_python_ok, assert_python_failure
)
-
# XXX (ncoghlan): Move to script_helper and make consistent with run_python
def _kill_python_and_exit_code(p):
data = kill_python(p)
@@ -486,6 +485,26 @@ class CmdLineTest(unittest.TestCase):
cwd=tmpdir)
self.assertEqual(out.strip(), b"ok")
+ def test_sys_flags_set(self):
+ # Issue 31845: a startup refactoring broke reading flags from env vars
+ for value, expected in (("", 0), ("1", 1), ("text", 1), ("2", 2)):
+ env_vars = dict(
+ PYTHONDEBUG=value,
+ PYTHONOPTIMIZE=value,
+ PYTHONDONTWRITEBYTECODE=value,
+ PYTHONVERBOSE=value,
+ )
+ code = (
+ "import sys; "
+ "sys.stderr.write(str(sys.flags)); "
+ f"""sys.exit(not (
+ sys.flags.debug == sys.flags.optimize ==
+ sys.flags.dont_write_bytecode == sys.flags.verbose ==
+ {expected}
+ ))"""
+ )
+ with self.subTest(envar_value=value):
+ assert_python_ok('-c', code, **env_vars)
class IgnoreEnvironmentTest(unittest.TestCase):
@@ -506,6 +525,20 @@ class IgnoreEnvironmentTest(unittest.TestCase):
self.run_ignoring_vars("sys.flags.hash_randomization == 1",
PYTHONHASHSEED="0")
+ def test_sys_flags_not_set(self):
+ # Issue 31845: a startup refactoring broke reading flags from env vars
+ expected_outcome = """
+ (sys.flags.debug == sys.flags.optimize ==
+ sys.flags.dont_write_bytecode == sys.flags.verbose == 0)
+ """
+ self.run_ignoring_vars(
+ expected_outcome,
+ PYTHONDEBUG="1",
+ PYTHONOPTIMIZE="1",
+ PYTHONDONTWRITEBYTECODE="1",
+ PYTHONVERBOSE="1",
+ )
+
def test_main():
test.support.run_unittest(CmdLineTest, IgnoreEnvironmentTest)