Mailing List Archive

gh-117535: Change unknown filename of warnings from `sys` to `<sys>` (#118018)
https://github.com/python/cpython/commit/a09e47299217fe98615aec8318e13a744307d52a
commit: a09e47299217fe98615aec8318e13a744307d52a
branch: main
author: Tian Gao <gaogaotiantian@hotmail.com>
committer: JelleZijlstra <jelle.zijlstra@gmail.com>
date: 2024-04-18T20:50:09-07:00
summary:

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

files:
A Misc/NEWS.d/next/Library/2024-04-18-00-35-11.gh-issue-117535.0m6SIM.rst
M Lib/test/test_warnings/__init__.py
M Lib/warnings.py
M Python/_warnings.c

diff --git a/Lib/test/test_warnings/__init__.py b/Lib/test/test_warnings/__init__.py
index 50b0f3fff04c57..b768631846e240 100644
--- a/Lib/test/test_warnings/__init__.py
+++ b/Lib/test/test_warnings/__init__.py
@@ -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.
@@ -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)
diff --git a/Lib/warnings.py b/Lib/warnings.py
index 4ad6ad027192e8..20a39d54bf7e6a 100644
--- a/Lib/warnings.py
+++ b/Lib/warnings.py
@@ -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
diff --git a/Misc/NEWS.d/next/Library/2024-04-18-00-35-11.gh-issue-117535.0m6SIM.rst b/Misc/NEWS.d/next/Library/2024-04-18-00-35-11.gh-issue-117535.0m6SIM.rst
new file mode 100644
index 00000000000000..72423506ccc07b
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-04-18-00-35-11.gh-issue-117535.0m6SIM.rst
@@ -0,0 +1 @@
+Change the unknown filename of :mod:`warnings` from ``sys`` to ``<sys>`` to clarify that it's not a real filename.
diff --git a/Python/_warnings.c b/Python/_warnings.c
index 4c520252aa12a8..2ba704dcaa79b2 100644
--- a/Python/_warnings.c
+++ b/Python/_warnings.c
@@ -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;

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-leave@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: list-python-checkins@lists.gossamer-threads.com