]> granicus.if.org Git - python/commitdiff
Issue #12467: warnings: fix a race condition if a warning is emitted at
authorVictor Stinner <victor.stinner@haypocalc.com>
Mon, 4 Jul 2011 01:05:37 +0000 (03:05 +0200)
committerVictor Stinner <victor.stinner@haypocalc.com>
Mon, 4 Jul 2011 01:05:37 +0000 (03:05 +0200)
shutdown, if globals()['__file__'] is None.

Lib/test/test_warnings.py
Misc/NEWS
Python/_warnings.c

index 5c4fa598619c7e8a1cfd4b229636a9ad51512d01..e502ed8f222d454bbdc22942d46dede518c4574f 100644 (file)
@@ -530,6 +530,18 @@ class _WarningsTests(BaseTest):
         assert expected_line
         self.assertEqual(second_line, expected_line)
 
+    def test_filename_none(self):
+        # issue #12467: race condition if a warning is emitted at shutdown
+        globals_dict = globals()
+        oldfile = globals_dict['__file__']
+        try:
+            with original_warnings.catch_warnings(module=self.module) as w:
+                self.module.filterwarnings("always", category=UserWarning)
+                globals_dict['__file__'] = None
+                self.module.warn('test', UserWarning)
+        finally:
+            globals_dict['__file__'] = oldfile
+
 
 class WarningsDisplayTests(unittest.TestCase):
 
index 16651edd78b0722d44453eaf1abc041c355cdc68..bbd12510169d266a5f784be74b141b750f345a4b 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -18,6 +18,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #12467: warnings: fix a race condition if a warning is emitted at
+  shutdown, if globals()['__file__'] is None.
+
 - Issue #12352: Fix a deadlock in multiprocessing.Heap when a block is freed by
   the garbage collector while the Heap lock is held.
 
index 88be7db68f6c45b0b2b29caff80bc89044c00a67..845679613212b226e955325d58b9d59e89cf220a 100644 (file)
@@ -491,7 +491,7 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
 
     /* Setup filename. */
     *filename = PyDict_GetItemString(globals, "__file__");
-    if (*filename != NULL) {
+    if (*filename != NULL && PyString_Check(*filename)) {
             Py_ssize_t len = PyString_Size(*filename);
         const char *file_str = PyString_AsString(*filename);
             if (file_str == NULL || (len < 0 && PyErr_Occurred()))