◐ Shell
clean mode source ↗

bpo-32089: Fix warnings filters in dev mode by vstinner · Pull Request #4482 · python/cpython

Expand Up @@ -507,14 +507,14 @@ def test_sys_flags_set(self): 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, Expand All @@ -525,8 +525,34 @@ def run_xdev(self, code, check_exitcode=True): 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 Expand All @@ -535,7 +561,7 @@ def test_xdev(self): 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: Expand All @@ -544,9 +570,23 @@ def test_xdev(self): 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): Expand Down