]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-117535: Change unknown filename of warnings from `sys` to `<sys>` (#118018)
authorTian Gao <gaogaotiantian@hotmail.com>
Fri, 19 Apr 2024 03:50:09 +0000 (20:50 -0700)
committerGitHub <noreply@github.com>
Fri, 19 Apr 2024 03:50:09 +0000 (20:50 -0700)
Lib/test/test_warnings/__init__.py
Lib/warnings.py
Misc/NEWS.d/next/Library/2024-04-18-00-35-11.gh-issue-117535.0m6SIM.rst [new file with mode: 0644]
Python/_warnings.c

index 50b0f3fff04c57bba2aa632a85393fecb64a1510..b768631846e2408a5030f3edfea359aa5cd2d9c3 100644 (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)
index 4ad6ad027192e891176530763eb3f88f7d5078fd..20a39d54bf7e6a553e0865b24e3944f5e58e26a1 100644 (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
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 (file)
index 0000000..7242350
--- /dev/null
@@ -0,0 +1 @@
+Change the unknown filename of :mod:`warnings` from ``sys`` to ``<sys>`` to clarify that it's not a real filename.
index 4c520252aa12a8996ae65ab0635fbc7b90f78c42..2ba704dcaa79b20a744eafdacb64d55734aa5a9c 100644 (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;