summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_capi/test_exceptions.py
diff options
context:
space:
mode:
authorKirill Podoprigora <kirill.bast9@mail.ru>2024-05-16 20:27:59 (GMT)
committerGitHub <noreply@github.com>2024-05-16 20:27:59 (GMT)
commit100c7ab00ab66a8c0d54582f35e38d8eb691743c (patch)
tree64dc41a7a3be5a41790d7c8c197eeedf294ab4fb /Lib/test/test_capi/test_exceptions.py
parent4702b7b5bdc07d046576b4126cf4e4f5f7145abb (diff)
downloadcpython-100c7ab00ab66a8c0d54582f35e38d8eb691743c.zip
cpython-100c7ab00ab66a8c0d54582f35e38d8eb691743c.tar.gz
cpython-100c7ab00ab66a8c0d54582f35e38d8eb691743c.tar.bz2
gh-119049: Fix incorrect display of warning which is constructed by C API (GH-119063)
The source line was not displayed if the warnings module had not yet been imported.
Diffstat (limited to 'Lib/test/test_capi/test_exceptions.py')
-rw-r--r--Lib/test/test_capi/test_exceptions.py44
1 files changed, 43 insertions, 1 deletions
diff --git a/Lib/test/test_capi/test_exceptions.py b/Lib/test/test_capi/test_exceptions.py
index 1d158e3..c475b6d 100644
--- a/Lib/test/test_capi/test_exceptions.py
+++ b/Lib/test/test_capi/test_exceptions.py
@@ -3,11 +3,12 @@ import os
import re
import sys
import unittest
+import textwrap
from test import support
from test.support import import_helper
from test.support.os_helper import TESTFN, TESTFN_UNDECODABLE
-from test.support.script_helper import assert_python_failure
+from test.support.script_helper import assert_python_failure, assert_python_ok
from test.support.testcase import ExceptionIsLikeMixin
from .test_misc import decode_stderr
@@ -68,6 +69,47 @@ class Test_Exceptions(unittest.TestCase):
else:
self.assertTrue(False)
+ def test_warn_with_stacklevel(self):
+ code = textwrap.dedent('''\
+ import _testcapi
+
+ def foo():
+ _testcapi.function_set_warning()
+
+ foo() # line 6
+
+
+ foo() # line 9
+ ''')
+ proc = assert_python_ok("-c", code)
+ warnings = proc.err.splitlines()
+ self.assertEqual(warnings, [
+ b'<string>:6: RuntimeWarning: Testing PyErr_WarnEx',
+ b' foo() # line 6',
+ b'<string>:9: RuntimeWarning: Testing PyErr_WarnEx',
+ b' foo() # line 9',
+ ])
+
+ def test_warn_during_finalization(self):
+ code = textwrap.dedent('''\
+ import _testcapi
+
+ class Foo:
+ def foo(self):
+ _testcapi.function_set_warning()
+ def __del__(self):
+ self.foo()
+
+ ref = Foo()
+ ''')
+ proc = assert_python_ok("-c", code)
+ warnings = proc.err.splitlines()
+ # Due to the finalization of the interpreter, the source will be ommited
+ # because the ``warnings`` module cannot be imported at this time
+ self.assertEqual(warnings, [
+ b'<string>:7: RuntimeWarning: Testing PyErr_WarnEx',
+ ])
+
class Test_FatalError(unittest.TestCase):