gh-117535: Change unknown filename of warnings from `sys` to `<sys>` (#118018)

This commit is contained in:
Tian Gao 2024-04-18 20:50:09 -07:00 committed by GitHub
parent 398abdd6fa
commit a09e472992
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 7 additions and 6 deletions

View File

@ -489,7 +489,7 @@ class WarnTests(BaseTest):
warning_tests.inner("spam7", stacklevel=9999)
self.assertEqual(os.path.basename(w[-1].filename),
"sys")
"<sys>")
def test_stacklevel_import(self):
# Issue #24305: With stacklevel=2, module-level warnings should work.
@ -1388,7 +1388,7 @@ a=A()
# Issue #21925: Emitting a ResourceWarning late during the Python
# shutdown must be logged.
expected = b"sys:1: ResourceWarning: unclosed file "
expected = b"<sys>:0: ResourceWarning: unclosed file "
# don't import the warnings module
# (_warnings will try to import it)

View File

@ -332,8 +332,8 @@ def warn(message, category=None, stacklevel=1, source=None,
raise ValueError
except ValueError:
globals = sys.__dict__
filename = "sys"
lineno = 1
filename = "<sys>"
lineno = 0
else:
globals = frame.f_globals
filename = frame.f_code.co_filename

View File

@ -0,0 +1 @@
Change the unknown filename of :mod:`warnings` from ``sys`` to ``<sys>`` to clarify that it's not a real filename.

View File

@ -894,8 +894,8 @@ setup_context(Py_ssize_t stack_level,
if (f == NULL) {
globals = interp->sysdict;
*filename = PyUnicode_FromString("sys");
*lineno = 1;
*filename = PyUnicode_FromString("<sys>");
*lineno = 0;
}
else {
globals = f->f_frame->f_globals;