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

Skip to content

Commit b1b25f0

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 7d10a2d commit b1b25f0

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
@@ -13,6 +13,9 @@ Core and Builtins
1313
Library
1414
-------
1515

16+
- Issue #12467: warnings: fix a race condition if a warning is emitted at
17+
shutdown, if globals()['__file__'] is None.
18+
1619

1720
What's New in Python 3.2.1 release candidate 2?
1821
===============================================

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)