Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Lib/test/test_warnings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ def test_stacklevel(self):

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.
Expand Down Expand Up @@ -1388,7 +1388,7 @@ def test_late_resource_warning(self):
# 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)
Expand Down
4 changes: 2 additions & 2 deletions Lib/warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Change the unknown filename of :mod:`warnings` from ``sys`` to ``<sys>`` to clarify that it's not a real filename.
4 changes: 2 additions & 2 deletions Python/_warnings.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down