summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_cmd_line.py
diff options
context:
space:
mode:
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 383302b..2aff51b 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 = [