bpo-32089: Fix warnings filters in dev mode by vstinner · Pull Request #4482 · python/cpython
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,
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
try:
# 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):