Skip to content

Commit 423fae3

Browse files
author
heliang
committed
Add Unit Test
1 parent 4b05629 commit 423fae3

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

Lib/test/test_warnings/__init__.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -771,15 +771,32 @@ def test_improper_input(self):
771771
self.assertRaises(UserWarning, self.module.warn, 'convert to error')
772772

773773
def test_import_from_module(self):
774-
with self.module.catch_warnings():
775-
self.module._setoption('ignore::Warning')
776-
with self.assertRaises(self.module._OptionError):
777-
self.module._setoption('ignore::TestWarning')
778-
with self.assertRaises(self.module._OptionError):
779-
self.module._setoption('ignore::test.test_warnings.bogus')
780-
self.module._setoption('error::test.test_warnings.TestWarning')
781-
with self.assertRaises(TestWarning):
782-
self.module.warn('test warning', TestWarning)
774+
script = support.script_helper.make_script('test_warnings_importer',
775+
'import test.test_warnings.data.import_warning')
776+
rc, out, err = assert_python_ok(script)
777+
self.assertNotIn(b'UserWarning', err)
778+
779+
def test_syntax_warning_for_compiler(self):
780+
# Test that SyntaxWarning from the compiler has a proper module name,
781+
# not a guessed one like 'sys'. gh-135801
782+
code = textwrap.dedent("""\
783+
class A:
784+
def func(self):
785+
return self.var is 2
786+
""")
787+
# The name of the script is 'test_sw'
788+
with os_helper.temp_dir() as script_dir:
789+
script_name = support.script_helper.make_script(script_dir, 'test_sw', code)
790+
# We want to check that the warning filter for 'test_sw' module works.
791+
rc, out, err = assert_python_failure("-W", "error::SyntaxWarning:test_sw",
792+
script_name)
793+
self.assertEqual(rc, 1)
794+
self.assertEqual(out, b'')
795+
# Check that we got a SyntaxError.
796+
err = err.decode()
797+
self.assertIn("""SyntaxError: "is" with 'int' literal. Did you mean "=="?""", err)
798+
# Check that the filename in the traceback is correct.
799+
self.assertIn(os.path.basename(script_name), err)
783800

784801

785802
class CWCmdLineTests(WCmdLineTests, unittest.TestCase):

0 commit comments

Comments
 (0)