| 1078 | self.module.showwarning = orig |
| 1079 | |
| 1080 | def test_show_warning_output(self): |
| 1081 | # With showwarning() missing, make sure that output is okay. |
| 1082 | orig = self.module.showwarning |
| 1083 | try: |
| 1084 | text = 'test show_warning' |
| 1085 | with self.module.catch_warnings(): |
| 1086 | self.module.filterwarnings("always", category=UserWarning) |
| 1087 | del self.module.showwarning |
| 1088 | with support.captured_output('stderr') as stream: |
| 1089 | warning_tests.inner(text) |
| 1090 | result = stream.getvalue() |
| 1091 | self.assertEqual(result.count('\n'), 2, |
| 1092 | "Too many newlines in %r" % result) |
| 1093 | first_line, second_line = result.split('\n', 1) |
| 1094 | expected_file = os.path.splitext(warning_tests.__file__)[0] + '.py' |
| 1095 | first_line_parts = first_line.rsplit(':', 3) |
| 1096 | path, line, warning_class, message = first_line_parts |
| 1097 | line = int(line) |
| 1098 | self.assertEqual(expected_file, path) |
| 1099 | self.assertEqual(warning_class, ' ' + UserWarning.__name__) |
| 1100 | self.assertEqual(message, ' ' + text) |
| 1101 | expected_line = ' ' + linecache.getline(path, line).strip() + '\n' |
| 1102 | assert expected_line |
| 1103 | self.assertEqual(second_line, expected_line) |
| 1104 | finally: |
| 1105 | self.module.showwarning = orig |
| 1106 | |
| 1107 | def test_filename_none(self): |
| 1108 | # issue #12467: race condition if a warning is emitted at shutdown |