summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_warnings/__init__.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_warnings/__init__.py')
-rw-r--r--Lib/test/test_warnings/__init__.py84
1 files changed, 9 insertions, 75 deletions
diff --git a/Lib/test/test_warnings/__init__.py b/Lib/test/test_warnings/__init__.py
index b48debd..71f6a30 100644
--- a/Lib/test/test_warnings/__init__.py
+++ b/Lib/test/test_warnings/__init__.py
@@ -441,78 +441,14 @@ class WarnTests(BaseTest):
self.assertEqual(len(w), 1)
self.assertEqual(w[0].filename, __file__)
- def test_missing_filename_not_main(self):
- # If __file__ is not specified and __main__ is not the module name,
- # then __file__ should be set to the module name.
- filename = warning_tests.__file__
- try:
- del warning_tests.__file__
- with warnings_state(self.module):
- with original_warnings.catch_warnings(record=True,
- module=self.module) as w:
- warning_tests.inner("spam8", stacklevel=1)
- self.assertEqual(w[-1].filename, warning_tests.__name__)
- finally:
- warning_tests.__file__ = filename
-
- @unittest.skipUnless(hasattr(sys, 'argv'), 'test needs sys.argv')
- def test_missing_filename_main_with_argv(self):
- # If __file__ is not specified and the caller is __main__ and sys.argv
- # exists, then use sys.argv[0] as the file.
- filename = warning_tests.__file__
- module_name = warning_tests.__name__
- try:
- del warning_tests.__file__
- warning_tests.__name__ = '__main__'
- with warnings_state(self.module):
- with original_warnings.catch_warnings(record=True,
- module=self.module) as w:
- warning_tests.inner('spam9', stacklevel=1)
- self.assertEqual(w[-1].filename, sys.argv[0])
- finally:
- warning_tests.__file__ = filename
- warning_tests.__name__ = module_name
-
- def test_missing_filename_main_without_argv(self):
- # If __file__ is not specified, the caller is __main__, and sys.argv
- # is not set, then '__main__' is the file name.
- filename = warning_tests.__file__
- module_name = warning_tests.__name__
- argv = sys.argv
- try:
- del warning_tests.__file__
- warning_tests.__name__ = '__main__'
- del sys.argv
- with warnings_state(self.module):
- with original_warnings.catch_warnings(record=True,
- module=self.module) as w:
- warning_tests.inner('spam10', stacklevel=1)
- self.assertEqual(w[-1].filename, '__main__')
- finally:
- warning_tests.__file__ = filename
- warning_tests.__name__ = module_name
- sys.argv = argv
-
- def test_missing_filename_main_with_argv_empty_string(self):
- # If __file__ is not specified, the caller is __main__, and sys.argv[0]
- # is the empty string, then '__main__ is the file name.
- # Tests issue 2743.
- file_name = warning_tests.__file__
- module_name = warning_tests.__name__
- argv = sys.argv
- try:
- del warning_tests.__file__
- warning_tests.__name__ = '__main__'
- sys.argv = ['']
- with warnings_state(self.module):
- with original_warnings.catch_warnings(record=True,
- module=self.module) as w:
- warning_tests.inner('spam11', stacklevel=1)
- self.assertEqual(w[-1].filename, '__main__')
- finally:
- warning_tests.__file__ = file_name
- warning_tests.__name__ = module_name
- sys.argv = argv
+ def test_exec_filename(self):
+ filename = "<warnings-test>"
+ codeobj = compile(("import warnings\n"
+ "warnings.warn('hello', UserWarning)"),
+ filename, "exec")
+ with original_warnings.catch_warnings(record=True) as w:
+ exec(codeobj)
+ self.assertEqual(w[0].filename, filename)
def test_warn_explicit_non_ascii_filename(self):
with original_warnings.catch_warnings(record=True,
@@ -1245,9 +1181,7 @@ class A:
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.decode(), '__main__:7: UserWarning: test')
+ self.assertEqual(err.decode(), '<string>:7: UserWarning: test')
def test_late_resource_warning(self):
# Issue #21925: Emitting a ResourceWarning late during the Python