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

Skip to content

Commit 927131e

Browse files
Issue #23016: A warning no longer produces an AttributeError when the program
is run with pythonw.exe.
2 parents 649e1f1 + 6059952 commit 927131e

3 files changed

Lines changed: 15 additions & 0 deletions

File tree

Lib/test/test_warnings.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,15 @@ def test_filename_none(self):
666666
finally:
667667
globals_dict['__file__'] = oldfile
668668

669+
def test_stderr_none(self):
670+
rc, stdout, stderr = assert_python_ok("-c",
671+
"import sys; sys.stderr = None; "
672+
"import warnings; warnings.simplefilter('always'); "
673+
"warnings.warn('Warning!')")
674+
self.assertEqual(stdout, b'')
675+
self.assertNotIn(b'Warning!', stderr)
676+
self.assertNotIn(b'Error', stderr)
677+
669678

670679
class WarningsDisplayTests(BaseTest):
671680

Lib/warnings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ def showwarning(message, category, filename, lineno, file=None, line=None):
1111
"""Hook to write a warning to a file; replace if you like."""
1212
if file is None:
1313
file = sys.stderr
14+
if file is None:
15+
# sys.stderr is None when ran with pythonw.exe - warnings get lost
16+
return
1417
try:
1518
file.write(formatwarning(message, category, filename, lineno, line))
1619
except OSError:

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ Core and Builtins
194194
Library
195195
-------
196196

197+
- Issue #23016: A warning no longer produces an AttributeError when the program
198+
is run with pythonw.exe.
199+
197200
- Issue #21775: shutil.copytree(): fix crash when copying to VFAT. An exception
198201
handler assumed that that OSError objects always have a 'winerror' attribute.
199202
That is not the case, so the exception handler itself raised AttributeError

0 commit comments

Comments
 (0)