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

Skip to content

Commit 5a21e83

Browse files
author
Victor Stinner
committed
(merge 3.2) Issue #12467: warnings: fix a race condition if a warning is
emitted at shutdown, if globals()['__file__'] is None.
2 parents 10cdc63 + 8b0508e commit 5a21e83

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
@@ -209,6 +209,9 @@ Core and Builtins
209209
Library
210210
-------
211211

212+
- Issue #12467: warnings: fix a race condition if a warning is emitted at
213+
shutdown, if globals()['__file__'] is None.
214+
212215
- Issue #12451: pydoc: importfile() now opens the Python script in binary mode,
213216
instead of text mode using the locale encoding, to avoid encoding issues.
214217

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)