|
3 | 3 | import re |
4 | 4 | import sys |
5 | 5 | import unittest |
| 6 | +import textwrap |
6 | 7 |
|
7 | 8 | from test import support |
8 | 9 | from test.support import import_helper |
9 | 10 | from test.support.os_helper import TESTFN, TESTFN_UNDECODABLE |
10 | | -from test.support.script_helper import assert_python_failure |
| 11 | +from test.support.script_helper import assert_python_failure, assert_python_ok |
11 | 12 | from test.support.testcase import ExceptionIsLikeMixin |
12 | 13 |
|
13 | 14 | from .test_misc import decode_stderr |
@@ -68,6 +69,47 @@ def test_exc_info(self): |
68 | 69 | else: |
69 | 70 | self.assertTrue(False) |
70 | 71 |
|
| 72 | + def test_warn_with_stacklevel(self): |
| 73 | + code = textwrap.dedent('''\ |
| 74 | + import _testcapi |
| 75 | +
|
| 76 | + def foo(): |
| 77 | + _testcapi.function_set_warning() |
| 78 | +
|
| 79 | + foo() # line 6 |
| 80 | +
|
| 81 | +
|
| 82 | + foo() # line 9 |
| 83 | + ''') |
| 84 | + proc = assert_python_ok("-c", code) |
| 85 | + warnings = proc.err.splitlines() |
| 86 | + self.assertEqual(warnings, [ |
| 87 | + b'<string>:6: RuntimeWarning: Testing PyErr_WarnEx', |
| 88 | + b' foo() # line 6', |
| 89 | + b'<string>:9: RuntimeWarning: Testing PyErr_WarnEx', |
| 90 | + b' foo() # line 9', |
| 91 | + ]) |
| 92 | + |
| 93 | + def test_warn_during_finalization(self): |
| 94 | + code = textwrap.dedent('''\ |
| 95 | + import _testcapi |
| 96 | +
|
| 97 | + class Foo: |
| 98 | + def foo(self): |
| 99 | + _testcapi.function_set_warning() |
| 100 | + def __del__(self): |
| 101 | + self.foo() |
| 102 | +
|
| 103 | + ref = Foo() |
| 104 | + ''') |
| 105 | + proc = assert_python_ok("-c", code) |
| 106 | + warnings = proc.err.splitlines() |
| 107 | + # Due to the finalization of the interpreter, the source will be ommited |
| 108 | + # because the ``warnings`` module cannot be imported at this time |
| 109 | + self.assertEqual(warnings, [ |
| 110 | + b'<string>:7: RuntimeWarning: Testing PyErr_WarnEx', |
| 111 | + ]) |
| 112 | + |
71 | 113 |
|
72 | 114 | class Test_FatalError(unittest.TestCase): |
73 | 115 |
|
|
0 commit comments