Skip to content

Commit

Permalink
BUG: Avoid error when printing warning in pythonw (#486)
Browse files Browse the repository at this point in the history
The default value of sys.stderr will be None in this case, so care
should be taken to avoid causing an AttributeError when trying to show
an otherwise nonfatal warning.
  • Loading branch information
colatkinson committed Apr 6, 2022
1 parent dacc6b7 commit c3e6d4a
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion PyPDF2/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,11 @@ def _showwarning(message, category, filename, lineno, file=warndest, line=None):
if file is None:
file = sys.stderr
try:
file.write(formatWarning(message, category, filename, lineno, line))
# It is possible for sys.stderr to be defined as None, most commonly in the case that the script
# is being run vida pythonw.exe on Windows. In this case, just swallow the warning.
# See also https://docs.python.org/3/library/sys.html#sys.__stderr__
if file is not None:
file.write(formatWarning(message, category, filename, lineno, line))
except IOError:
pass
warnings.showwarning = _showwarning
Expand Down

0 comments on commit c3e6d4a

Please sign in to comment.