Use sys.__stderr__ for faulthandler.

This commit is contained in:
Florian Bruhin 2014-08-01 00:55:18 +02:00
parent ad1ba4c1f7
commit b6cd0dd600
2 changed files with 11 additions and 5 deletions

View File

@ -502,8 +502,11 @@ class Application(QApplication):
"""Clean up the crash log file and delete it.""" """Clean up the crash log file and delete it."""
if self._crashlogfile is None: if self._crashlogfile is None:
return return
if sys.stderr is not None: # We use sys.__stderr__ instead of sys.stderr here so this will still
faulthandler.enable() # work when sys.stderr got replaced, e.g. by "Python Tools for Visual
# Studio".
if sys.__stderr__ is not None:
faulthandler.enable(sys.__stderr__)
else: else:
faulthandler.disable() faulthandler.disable()
self._crashlogfile.close() self._crashlogfile.close()

View File

@ -95,9 +95,12 @@ def init_faulthandler():
"""Enable faulthandler module if available. """Enable faulthandler module if available.
This print a nice traceback on segfauls. This print a nice traceback on segfauls.
We use sys.__stderr__ instead of sys.stderr here so this will still work
when sys.stderr got replaced, e.g. by "Python Tools for Visual Studio".
""" """
if sys.stderr is None: if sys.__stderr__ is None:
# When run with pythonw.exe, sys.stderr can be None: # When run with pythonw.exe, sys.__stderr__ can be None:
# https://docs.python.org/3/library/sys.html#sys.__stderr__ # https://docs.python.org/3/library/sys.html#sys.__stderr__
# If we'd enable faulthandler in that case, we just get a weird # If we'd enable faulthandler in that case, we just get a weird
# exception, so we don't enable faulthandler if we have no stdout. # exception, so we don't enable faulthandler if we have no stdout.
@ -106,7 +109,7 @@ def init_faulthandler():
# to write to a file so we can display a crash to the user at the next # to write to a file so we can display a crash to the user at the next
# start. # start.
return return
faulthandler.enable() faulthandler.enable(sys.__stderr__)
if hasattr(faulthandler, 'register') and hasattr(signal, 'SIGUSR1'): if hasattr(faulthandler, 'register') and hasattr(signal, 'SIGUSR1'):
# If available, we also want a traceback on SIGUSR1. # If available, we also want a traceback on SIGUSR1.
faulthandler.register(signal.SIGUSR1) # pylint: disable=no-member faulthandler.register(signal.SIGUSR1) # pylint: disable=no-member