# or not completely loaded (warnings imports indirectly encodings by
# importing linecache) yet
with support.temp_cwd() as cwd, support.temp_cwd('encodings'):
- env = os.environ.copy()
- env['PYTHONPATH'] = cwd
-
# encodings loaded by initfsencoding()
- retcode = subprocess.call([sys.executable, '-c', 'pass'], env=env)
- self.assertEqual(retcode, 0)
+ assert_python_ok('-c', 'pass', PYTHONPATH=cwd)
# Use -W to load warnings module at startup
- retcode = subprocess.call(
- [sys.executable, '-c', 'pass', '-W', 'always'],
- env=env)
- self.assertEqual(retcode, 0)
+ assert_python_ok('-c', 'pass', '-W', 'always', PYTHONPATH=cwd)
+class FinalizationTest(unittest.TestCase):
+ def test_finalization(self):
+ # Issue #19421: warnings.warn() should not crash
+ # during Python finalization
+ code = """
+import warnings
+warn = warnings.warn
+
+class A:
+ def __del__(self):
+ warn("test")
+
+a=A()
+ """
+ rc, out, err = assert_python_ok("-c", code)
+ # note: "__main__" filename is not correct, it should be the name
+ # of the script
+ self.assertEqual(err, b'__main__:7: UserWarning: test')
+
def setUpModule():
py_warnings.onceregistry.clear()