diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2017-11-30 10:40:24 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-30 10:40:24 (GMT) |
commit | 5e3806f8cfd84722fc55d4299dc018ad9b0f8401 (patch) | |
tree | 436f0a963001f590a1193dba5c84627ba59513c2 /Lib | |
parent | 706e10b186992e086e661a62d2c8ec9588525b31 (diff) | |
download | cpython-5e3806f8cfd84722fc55d4299dc018ad9b0f8401.zip cpython-5e3806f8cfd84722fc55d4299dc018ad9b0f8401.tar.gz cpython-5e3806f8cfd84722fc55d4299dc018ad9b0f8401.tar.bz2 |
bpo-32101: Add PYTHONDEVMODE environment variable (#4624)
* bpo-32101: Add sys.flags.dev_mode flag
Rename also the "Developer mode" to the "Development mode".
* bpo-32101: Add PYTHONDEVMODE environment variable
Mention it in the development chapiter.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/asyncio/coroutines.py | 8 | ||||
-rw-r--r-- | Lib/test/test_cmd_line.py | 37 | ||||
-rw-r--r-- | Lib/test/test_sys.py | 6 |
3 files changed, 42 insertions, 9 deletions
diff --git a/Lib/asyncio/coroutines.py b/Lib/asyncio/coroutines.py index b6f81a4..7d2ca05 100644 --- a/Lib/asyncio/coroutines.py +++ b/Lib/asyncio/coroutines.py @@ -27,11 +27,9 @@ def _is_debug_mode(): # before you define your coroutines. A downside of using this feature # is that tracebacks show entries for the CoroWrapper.__next__ method # when _DEBUG is true. - debug = (not sys.flags.ignore_environment and - bool(os.environ.get('PYTHONASYNCIODEBUG'))) - if hasattr(sys, '_xoptions') and 'dev' in sys._xoptions: - debug = True - return debug + return (sys.flags.dev_mode + or (not sys.flags.ignore_environment + and bool(os.environ.get('PYTHONASYNCIODEBUG')))) _DEBUG = _is_debug_mode() diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py index 96405e7..383302b 100644 --- a/Lib/test/test_cmd_line.py +++ b/Lib/test/test_cmd_line.py @@ -508,14 +508,18 @@ class CmdLineTest(unittest.TestCase): with self.subTest(envar_value=value): assert_python_ok('-c', code, **env_vars) - def run_xdev(self, *args, check_exitcode=True): + def run_xdev(self, *args, check_exitcode=True, xdev=True): env = dict(os.environ) env.pop('PYTHONWARNINGS', None) + env.pop('PYTHONDEVMODE', None) # Force malloc() to disable the debug hooks which are enabled # by default for Python compiled in debug mode env['PYTHONMALLOC'] = 'malloc' - args = (sys.executable, '-X', 'dev', *args) + if xdev: + args = (sys.executable, '-X', 'dev', *args) + else: + args = (sys.executable, *args) proc = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, @@ -526,6 +530,14 @@ class CmdLineTest(unittest.TestCase): return proc.stdout.rstrip() def test_xdev(self): + # sys.flags.dev_mode + code = "import sys; print(sys.flags.dev_mode)" + out = self.run_xdev("-c", code, xdev=False) + self.assertEqual(out, "False") + out = self.run_xdev("-c", code) + self.assertEqual(out, "True") + + # Warnings code = ("import sys, warnings; " "print(' '.join('%s::%s' % (f[0], f[2].__name__) " "for f in warnings.filters))") @@ -555,6 +567,7 @@ class CmdLineTest(unittest.TestCase): "default::ResourceWarning " "default::Warning") + # Memory allocator debug hooks try: import _testcapi except ImportError: @@ -569,6 +582,7 @@ class CmdLineTest(unittest.TestCase): alloc_name = "malloc_debug" self.assertEqual(out, alloc_name) + # Faulthandler try: import faulthandler except ImportError: @@ -581,6 +595,7 @@ class CmdLineTest(unittest.TestCase): def check_pythonmalloc(self, env_var, name): code = 'import _testcapi; print(_testcapi.pymem_getallocatorsname())' env = dict(os.environ) + env.pop('PYTHONDEVMODE', None) if env_var is not None: env['PYTHONMALLOC'] = env_var else: @@ -621,6 +636,24 @@ class CmdLineTest(unittest.TestCase): with self.subTest(env_var=env_var, name=name): self.check_pythonmalloc(env_var, name) + def test_pythondevmode_env(self): + # Test the PYTHONDEVMODE environment variable + code = "import sys; print(sys.flags.dev_mode)" + env = dict(os.environ) + env.pop('PYTHONDEVMODE', None) + args = (sys.executable, '-c', code) + + proc = subprocess.run(args, stdout=subprocess.PIPE, + universal_newlines=True, env=env) + self.assertEqual(proc.stdout.rstrip(), 'False') + self.assertEqual(proc.returncode, 0, proc) + + env['PYTHONDEVMODE'] = '1' + proc = subprocess.run(args, stdout=subprocess.PIPE, + universal_newlines=True, env=env) + self.assertEqual(proc.stdout.rstrip(), 'True') + self.assertEqual(proc.returncode, 0, proc) + class IgnoreEnvironmentTest(unittest.TestCase): diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py index 4b8fcb9..6346094 100644 --- a/Lib/test/test_sys.py +++ b/Lib/test/test_sys.py @@ -526,10 +526,12 @@ class SysModuleTest(unittest.TestCase): attrs = ("debug", "inspect", "interactive", "optimize", "dont_write_bytecode", "no_user_site", "no_site", "ignore_environment", "verbose", - "bytes_warning", "quiet", "hash_randomization", "isolated") + "bytes_warning", "quiet", "hash_randomization", "isolated", + "dev_mode") for attr in attrs: self.assertTrue(hasattr(sys.flags, attr), attr) - self.assertEqual(type(getattr(sys.flags, attr)), int, attr) + attr_type = bool if attr == "dev_mode" else int + self.assertEqual(type(getattr(sys.flags, attr)), attr_type, attr) self.assertTrue(repr(sys.flags)) self.assertEqual(len(sys.flags), len(attrs)) |