This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: uncaught exception in lib/warnings.py when executed with pythonw
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.4, Python 3.5, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: Arfrever, python-dev, serhiy.storchaka, stockbsd, vstinner
Priority: normal Keywords: patch

Created on 2014-12-09 01:27 by stockbsd, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
warnings_stderr_none.patch serhiy.storchaka, 2014-12-09 11:49 review
Messages (10)
msg232342 - (view) Author: stockbsd Li (stockbsd) Date: 2014-12-09 01:27
in py3k, the following simple code will throw an uncatched exception when executed with pythonw:

import warnings
warnings.warn('test')

the problem occurs in showarning function: in py3k's pythonw , stderr/stdout is set to None, so the file.write(...) statement will thorw AttributeError uncatched. I think a catch-all except(delete 'OSError') can solve this.

def showwarning(message, category, filename, lineno, file=None, line=None):
    """Hook to write a warning to a file; replace if you like."""
    if file is None:
        file = sys.stderr
    try:
        file.write(formatwarning(message, category, filename, lineno, line))
    except OSError:
        pass # the file (probably stderr) is invalid - this warning gets lost.
msg232369 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-12-09 11:49
Here is a patch. 2.7 is affected too.
msg232371 - (view) Author: stockbsd Li (stockbsd) Date: 2014-12-09 12:21
1. py2 is unaffected, because stderr/stdout is not None in py2's pythonw and the catch works correctly.

2. as to py3, I prefer this patch:
-  execpt OSError:
+  execpt:
msg232374 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2014-12-09 13:10
+  execpt:

Please never use that, but "except Exception:" instead to not catch SystemExit or KeyboardInterrupt.
msg232391 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-12-09 16:41
I afraid this will silence unexpected errors.
msg232424 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2014-12-10 13:09
warnings_stderr_none.patch looks good to me. It can be applied on Python 2.7, 3.4 and 3.5.
msg232443 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-12-10 21:12
New changeset d04dab84388f by Serhiy Storchaka in branch '3.4':
Issue #23016: A warning no longer produces AttributeError when the program
https://hg.python.org/cpython/rev/d04dab84388f

New changeset 0050e770b34c by Serhiy Storchaka in branch 'default':
Issue #23016: A warning no longer produces an AttributeError when the program
https://hg.python.org/cpython/rev/0050e770b34c

New changeset aeeec8a4b9b8 by Serhiy Storchaka in branch '2.7':
Issue #23016: A warning no longer produces an AttributeError when sys.stderr
https://hg.python.org/cpython/rev/aeeec8a4b9b8
msg232631 - (view) Author: Arfrever Frehtes Taifersar Arahesis (Arfrever) * (Python triager) Date: 2014-12-14 08:15
> +            # sys.stderr is None when ran with pythonw.exe - warnings get lost

s/ran/run/
msg232632 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-12-14 08:58
New changeset 70b6fe58c425 by Serhiy Storchaka in branch '3.4':
Fixed a typo in a comment (issue #23016).
https://hg.python.org/cpython/rev/70b6fe58c425

New changeset da1ec8e0e068 by Serhiy Storchaka in branch 'default':
Fixed a typo in a comment (issue #23016).
https://hg.python.org/cpython/rev/da1ec8e0e068
msg232633 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-12-14 08:58
Thanks Arfrever.
History
Date User Action Args
2022-04-11 14:58:10adminsetgithub: 67205
2014-12-14 08:58:48serhiy.storchakasetmessages: + msg232633
2014-12-14 08:58:06python-devsetmessages: + msg232632
2014-12-14 08:15:09Arfreversetnosy: + Arfrever
messages: + msg232631
2014-12-10 22:08:57serhiy.storchakasetstatus: open -> closed
assignee: serhiy.storchaka
resolution: fixed
stage: patch review -> resolved
2014-12-10 21:12:49python-devsetnosy: + python-dev
messages: + msg232443
2014-12-10 14:58:24r.david.murraysettitle: uncatched exception in lib/warnings.py when executed with pythonw -> uncaught exception in lib/warnings.py when executed with pythonw
2014-12-10 13:09:22vstinnersetmessages: + msg232424
2014-12-09 16:41:40serhiy.storchakasetmessages: + msg232391
2014-12-09 13:10:45vstinnersetnosy: + vstinner
messages: + msg232374
2014-12-09 12:21:16stockbsdsetmessages: + msg232371
2014-12-09 11:49:40serhiy.storchakasetfiles: + warnings_stderr_none.patch

type: crash -> behavior
versions: + Python 2.7, Python 3.5, - Python 3.2, Python 3.3
keywords: + patch
nosy: + serhiy.storchaka

messages: + msg232369
stage: patch review
2014-12-09 01:27:03stockbsdcreate