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: faulthandler should handle sys.stderr being None gracefully
Type: behavior Stage:
Components: Extension Modules Versions: Python 3.3, Python 3.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: The Compiler, python-dev, vstinner
Priority: normal Keywords: patch

Created on 2014-05-13 14:09 by The Compiler, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
faulthandler.patch vstinner, 2014-05-13 15:42 review
Messages (8)
msg218461 - (view) Author: Florian Bruhin (The Compiler) * Date: 2014-05-13 14:09
When faulthandler is used while sys.stderr is None (e.g. when using pythonw.exe), a (IMHO) confusing exception is raised:

    Traceback (most recent call last):
      File "test.py", line 7, in <module>
         faulthandler.enable()
    AttributeError: 'NoneType' object has no attribute 'fileno'

Example script which demonstrates the issue without using pythonw.exe:

    import faulthandler
    import sys

    sys.stderr = None

    try:
        faulthandler.enable()
    except:
        sys.stderr = sys.__stderr__
        raise

Looking at the code it seems the file passed gets correctly checked against NULL and Py_None, but stderr (as fallback) then only gets checked against NULL:

http://hg.python.org/cpython/file/8885fc2e92b3/Modules/faulthandler.c#l141
msg218467 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2014-05-13 15:42
Attached patch modifies faulthandler to raises a RuntimeError("sys.stderr is None") with your use case. Is it what you expected?
msg218468 - (view) Author: Florian Bruhin (The Compiler) * Date: 2014-05-13 15:48
I didn't test the patch (I don't have the toolchain set up to do so), but it looks like this is indeed an exception which makes more sense to the developer.

When I saw the exception as it is now, I only discovered it's related to stderr being None by finding people having a similiar issue with curses, and with the new exception it'd be clear to me what happened.

I personally would prefer failing silently though so the application still runs (if there's no stderr, there just is no fault log), but that's debatable of course.
msg218469 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2014-05-13 15:51
> I personally would prefer failing silently though so the application still runs (if there's no stderr, there just is no fault log), but that's debatable of course.

Nope, there is no debate: there is the Zen of Python :-)

"Errors should never pass silently."

What's the purpose of enabling faulthandler if sys.stderr is None? If you don't want faulthandler output, don't enable faulthandler!?
msg218470 - (view) Author: Florian Bruhin (The Compiler) * Date: 2014-05-13 15:58
The thing is the developer is not necessarely the one controlling if sys.stderr is None, sometimes the user is.

For example, see https://docs.python.org/3.4/using/windows.html#executing-scripts-without-the-python-launcher - an user might have decided to use pythonw by default.

This means there's an unexpected gotcha - if I want my application to still run under Windows when the user has changed the default, I first have to check sys.stderr before enabling faulthandler.

At the very least I think this should be documented in the faulthandler documentation. I noticed the issue when starting my application under Windows after using cx_Freeze on it, and as a developer wouldn't have expected (or thought about this) at all.

I don't feel like I'm the one to decide this properly though, and I'm uncertain what's the right choice, so what you say is probably right ;)
msg218471 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2014-05-13 16:09
> This means there's an unexpected gotcha

I prefer to see an exception before sys.stderr is None, instead of not seeing the traceback when the application does crash. It is usually a pain to reproduce a crash in the exact same conditions.

> if I want my application to still run under Windows when the user has changed the default, I first have to check sys.stderr before enabling faulthandler.

Exactly. It's not really specific to Windows, you may also have sys.stderr on UNIX in some cases.
msg218545 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-05-14 15:17
New changeset ddd7db7cf036 by Victor Stinner in branch '3.4':
Issue #21497: faulthandler functions now raise a better error if sys.stderr is
http://hg.python.org/cpython/rev/ddd7db7cf036

New changeset cb26887f9140 by Victor Stinner in branch 'default':
(Merge 3.4) Issue #21497: faulthandler functions now raise a better error if
http://hg.python.org/cpython/rev/cb26887f9140
msg218546 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2014-05-14 15:22
I applied my patch, thanks for the report.
History
Date User Action Args
2022-04-11 14:58:03adminsetgithub: 65696
2014-05-14 15:22:32vstinnersetstatus: open -> closed
resolution: fixed
messages: + msg218546
2014-05-14 15:17:15python-devsetnosy: + python-dev
messages: + msg218545
2014-05-13 16:09:47vstinnersetmessages: + msg218471
2014-05-13 15:58:22The Compilersetmessages: + msg218470
2014-05-13 15:51:06vstinnersetmessages: + msg218469
2014-05-13 15:48:22The Compilersetmessages: + msg218468
2014-05-13 15:42:25vstinnersetfiles: + faulthandler.patch
keywords: + patch
messages: + msg218467
2014-05-13 14:09:29The Compilercreate