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

Skip to content

Commit 8b0508e

Browse files
author
Victor Stinner
committed
Issue #12467: warnings: fix a race condition if a warning is emitted at
shutdown, if globals()['__file__'] is None.
1 parent aa90e7c commit 8b0508e

3 files changed

Lines changed: 16 additions & 1 deletion

File tree

Lib/test/test_warnings.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,18 @@ def test_show_warning_output(self):
542542
assert expected_line
543543
self.assertEqual(second_line, expected_line)
544544

545+
def test_filename_none(self):
546+
# issue #12467: race condition if a warning is emitted at shutdown
547+
globals_dict = globals()
548+
oldfile = globals_dict['__file__']
549+
try:
550+
with original_warnings.catch_warnings(module=self.module) as w:
551+
self.module.filterwarnings("always", category=UserWarning)
552+
globals_dict['__file__'] = None
553+
original_warnings.warn('test', UserWarning)
554+
finally:
555+
globals_dict['__file__'] = oldfile
556+
545557

546558
class WarningsDisplayTests(unittest.TestCase):
547559

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ Core and Builtins
1919
Library
2020
-------
2121

22+
- Issue #12467: warnings: fix a race condition if a warning is emitted at
23+
shutdown, if globals()['__file__'] is None.
24+
2225
- Issue #12451: pydoc: importfile() now opens the Python script in binary mode,
2326
instead of text mode using the locale encoding, to avoid encoding issues.
2427

Python/_warnings.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
496496

497497
/* Setup filename. */
498498
*filename = PyDict_GetItemString(globals, "__file__");
499-
if (*filename != NULL) {
499+
if (*filename != NULL && PyUnicode_Check(*filename)) {
500500
Py_ssize_t len = PyUnicode_GetSize(*filename);
501501
Py_UNICODE *unicode = PyUnicode_AS_UNICODE(*filename);
502502

0 commit comments

Comments
 (0)