summaryrefslogtreecommitdiff
path: root/Lib/test/test_cmd_line.py
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2017-12-12 22:59:48 +0100
committerGitHub <noreply@github.com>2017-12-12 22:59:48 +0100
commit747f48e2e92390c44c72f52a1239959601cde157 (patch)
tree502e53b129aee7a393ca6d05e4f93751919a5e1b /Lib/test/test_cmd_line.py
parentb748e3b2586e44bfc7011b601bce9cc6d16d89f1 (diff)
downloadcpython-git-747f48e2e92390c44c72f52a1239959601cde157.tar.gz
bpo-32230: Set sys.warnoptions with -X dev (#4820)
Rather than supporting dev mode directly in the warnings module, this instead adjusts the initialisation code to add an extra 'default' entry to sys.warnoptions when dev mode is enabled. This ensures that dev mode behaves *exactly* as if `-Wdefault` had been passed on the command line, including in the way it interacts with `sys.warnoptions`, and with other command line flags like `-bb`. Fix also bpo-20361: have -b & -bb options take precedence over any other warnings options. Patch written by Nick Coghlan, with minor modifications of Victor Stinner.
Diffstat (limited to 'Lib/test/test_cmd_line.py')
-rw-r--r--Lib/test/test_cmd_line.py81
1 files changed, 60 insertions, 21 deletions
diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py
index 383302bad8..2aff51bdc1 100644
--- a/Lib/test/test_cmd_line.py
+++ b/Lib/test/test_cmd_line.py
@@ -14,6 +14,11 @@ from test.support.script_helper import (
interpreter_requires_environment
)
+
+# Debug build?
+Py_DEBUG = hasattr(sys, "gettotalrefcount")
+
+
# XXX (ncoghlan): Move to script_helper and make consistent with run_python
def _kill_python_and_exit_code(p):
data = kill_python(p)
@@ -97,7 +102,7 @@ class CmdLineTest(unittest.TestCase):
# "-X showrefcount" shows the refcount, but only in debug builds
rc, out, err = run_python('-X', 'showrefcount', '-c', code)
self.assertEqual(out.rstrip(), b"{'showrefcount': True}")
- if hasattr(sys, 'gettotalrefcount'): # debug build
+ if Py_DEBUG:
self.assertRegex(err, br'^\[\d+ refs, \d+ blocks\]')
else:
self.assertEqual(err, b'')
@@ -541,31 +546,26 @@ class CmdLineTest(unittest.TestCase):
code = ("import sys, warnings; "
"print(' '.join('%s::%s' % (f[0], f[2].__name__) "
"for f in warnings.filters))")
+ if Py_DEBUG:
+ expected_filters = "default::Warning"
+ else:
+ expected_filters = ("default::Warning "
+ "ignore::DeprecationWarning "
+ "ignore::PendingDeprecationWarning "
+ "ignore::ImportWarning "
+ "ignore::ResourceWarning")
out = self.run_xdev("-c", code)
- self.assertEqual(out,
- "ignore::BytesWarning "
- "default::ResourceWarning "
- "default::Warning")
+ self.assertEqual(out, expected_filters)
out = self.run_xdev("-b", "-c", code)
- self.assertEqual(out,
- "default::BytesWarning "
- "default::ResourceWarning "
- "default::Warning")
+ self.assertEqual(out, f"default::BytesWarning {expected_filters}")
out = self.run_xdev("-bb", "-c", code)
- self.assertEqual(out,
- "error::BytesWarning "
- "default::ResourceWarning "
- "default::Warning")
+ self.assertEqual(out, f"error::BytesWarning {expected_filters}")
out = self.run_xdev("-Werror", "-c", code)
- self.assertEqual(out,
- "error::Warning "
- "ignore::BytesWarning "
- "default::ResourceWarning "
- "default::Warning")
+ self.assertEqual(out, f"error::Warning {expected_filters}")
# Memory allocator debug hooks
try:
@@ -592,6 +592,46 @@ class CmdLineTest(unittest.TestCase):
out = self.run_xdev("-c", code)
self.assertEqual(out, "True")
+ def check_warnings_filters(self, cmdline_option, envvar, use_pywarning=False):
+ if use_pywarning:
+ code = ("import sys; from test.support import import_fresh_module; "
+ "warnings = import_fresh_module('warnings', blocked=['_warnings']); ")
+ else:
+ code = "import sys, warnings; "
+ code += ("print(' '.join('%s::%s' % (f[0], f[2].__name__) "
+ "for f in warnings.filters))")
+ args = (sys.executable, '-W', cmdline_option, '-bb', '-c', code)
+ env = dict(os.environ)
+ env.pop('PYTHONDEVMODE', None)
+ env["PYTHONWARNINGS"] = envvar
+ proc = subprocess.run(args,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.STDOUT,
+ universal_newlines=True,
+ env=env)
+ self.assertEqual(proc.returncode, 0, proc)
+ return proc.stdout.rstrip()
+
+ def test_warnings_filter_precedence(self):
+ expected_filters = ("error::BytesWarning "
+ "once::UserWarning "
+ "always::UserWarning")
+ if not Py_DEBUG:
+ expected_filters += (" "
+ "ignore::DeprecationWarning "
+ "ignore::PendingDeprecationWarning "
+ "ignore::ImportWarning "
+ "ignore::ResourceWarning")
+
+ out = self.check_warnings_filters("once::UserWarning",
+ "always::UserWarning")
+ self.assertEqual(out, expected_filters)
+
+ out = self.check_warnings_filters("once::UserWarning",
+ "always::UserWarning",
+ use_pywarning=True)
+ self.assertEqual(out, expected_filters)
+
def check_pythonmalloc(self, env_var, name):
code = 'import _testcapi; print(_testcapi.pymem_getallocatorsname())'
env = dict(os.environ)
@@ -611,13 +651,12 @@ class CmdLineTest(unittest.TestCase):
def test_pythonmalloc(self):
# Test the PYTHONMALLOC environment variable
- pydebug = hasattr(sys, "gettotalrefcount")
pymalloc = support.with_pymalloc()
if pymalloc:
- default_name = 'pymalloc_debug' if pydebug else 'pymalloc'
+ default_name = 'pymalloc_debug' if Py_DEBUG else 'pymalloc'
default_name_debug = 'pymalloc_debug'
else:
- default_name = 'malloc_debug' if pydebug else 'malloc'
+ default_name = 'malloc_debug' if Py_DEBUG else 'malloc'
default_name_debug = 'malloc_debug'
tests = [