summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_cmd_line.py
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2017-11-21 01:32:40 (GMT)
committerGitHub <noreply@github.com>2017-11-21 01:32:40 (GMT)
commit09f3a8a1249308a104a89041d82fe99e6c087043 (patch)
tree2d494c186b4aadfb6fe630f8ac9fc7e66e1906f5 /Lib/test/test_cmd_line.py
parentf39b674876d2bd47ec7fc106d673b60ff24092ca (diff)
downloadcpython-09f3a8a1249308a104a89041d82fe99e6c087043.zip
cpython-09f3a8a1249308a104a89041d82fe99e6c087043.tar.gz
cpython-09f3a8a1249308a104a89041d82fe99e6c087043.tar.bz2
bpo-32089: Fix warnings filters in dev mode (#4482)
The developer mode (-X dev) now creates all default warnings filters to order filters in the correct order to always show ResourceWarning and make BytesWarning depend on the -b option. Write a functional test to make sure that ResourceWarning is logged twice at the same location in the developer mode. Add a new 'dev_mode' field to _PyCoreConfig.
Diffstat (limited to 'Lib/test/test_cmd_line.py')
-rw-r--r--Lib/test/test_cmd_line.py52
1 files changed, 46 insertions, 6 deletions
diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py
index 3dbe75f..75f7d00 100644
--- a/Lib/test/test_cmd_line.py
+++ b/Lib/test/test_cmd_line.py
@@ -507,14 +507,14 @@ class CmdLineTest(unittest.TestCase):
with self.subTest(envar_value=value):
assert_python_ok('-c', code, **env_vars)
- def run_xdev(self, code, check_exitcode=True):
+ def run_xdev(self, *args, check_exitcode=True):
env = dict(os.environ)
env.pop('PYTHONWARNINGS', 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', '-c', code)
+ args = (sys.executable, '-X', 'dev', *args)
proc = subprocess.run(args,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
@@ -525,8 +525,34 @@ class CmdLineTest(unittest.TestCase):
return proc.stdout.rstrip()
def test_xdev(self):
- out = self.run_xdev("import sys; print(sys.warnoptions)")
- self.assertEqual(out, "['default']")
+ code = ("import sys, warnings; "
+ "print(' '.join('%s::%s' % (f[0], f[2].__name__) "
+ "for f in warnings.filters))")
+
+ out = self.run_xdev("-c", code)
+ self.assertEqual(out,
+ "ignore::BytesWarning "
+ "always::ResourceWarning "
+ "default::Warning")
+
+ out = self.run_xdev("-b", "-c", code)
+ self.assertEqual(out,
+ "default::BytesWarning "
+ "always::ResourceWarning "
+ "default::Warning")
+
+ out = self.run_xdev("-bb", "-c", code)
+ self.assertEqual(out,
+ "error::BytesWarning "
+ "always::ResourceWarning "
+ "default::Warning")
+
+ out = self.run_xdev("-Werror", "-c", code)
+ self.assertEqual(out,
+ "error::Warning "
+ "ignore::BytesWarning "
+ "always::ResourceWarning "
+ "default::Warning")
try:
import _testcapi
@@ -535,7 +561,7 @@ class CmdLineTest(unittest.TestCase):
else:
code = "import _testcapi; _testcapi.pymem_api_misuse()"
with support.SuppressCrashReport():
- out = self.run_xdev(code, check_exitcode=False)
+ out = self.run_xdev("-c", code, check_exitcode=False)
self.assertIn("Debug memory block at address p=", out)
try:
@@ -544,9 +570,23 @@ class CmdLineTest(unittest.TestCase):
pass
else:
code = "import faulthandler; print(faulthandler.is_enabled())"
- out = self.run_xdev(code)
+ out = self.run_xdev("-c", code)
self.assertEqual(out, "True")
+ # Make sure that ResourceWarning emitted twice at the same line number
+ # is logged twice
+ filename = support.TESTFN
+ self.addCleanup(support.unlink, filename)
+ with open(filename, "w", encoding="utf8") as fp:
+ print("def func(): open(__file__)", file=fp)
+ print("func()", file=fp)
+ print("func()", file=fp)
+ fp.flush()
+
+ out = self.run_xdev(filename)
+ self.assertEqual(out.count(':1: ResourceWarning: '), 2, out)
+
+
class IgnoreEnvironmentTest(unittest.TestCase):
def run_ignoring_vars(self, predicate, **env_vars):