Check if sys.stdout is not None before using it.

When using pythonw (e.g. with cx_Freeze), sys.__stdout__ and sys.stdout
will be None, so we don't need to flush it.

This also means faulthandler won't work correctly, so we don't enabled
it if we don't have an stdout.
This commit is contained in:
Florian Bruhin 2014-05-13 10:38:27 +02:00
parent 1031fc8560
commit bf639602f5
3 changed files with 17 additions and 4 deletions

View File

@ -19,6 +19,8 @@
### Things we want to do before normal imports ### ### Things we want to do before normal imports ###
import sys
# Print a nice traceback on segfault -- only available on Python 3.3+, but if # Print a nice traceback on segfault -- only available on Python 3.3+, but if
# it's unavailable, it doesn't matter much. # it's unavailable, it doesn't matter much.
try: try:
@ -26,7 +28,16 @@ try:
except ImportError: except ImportError:
pass pass
else: else:
faulthandler.enable() if sys.__stdout__ is not None:
# When run with pythonw.exe, sys.__stdout__ can be None:
# https://docs.python.org/3/library/sys.html#sys.__stdout__
# If we'd enable faulthandler in that case, we just get a weird
# exception, so we don't enable faulthandler in that case.
#
# FIXME at the point we have our config/data dirs, we probably should
# re-enable faulthandler to write to a file. Then we can also display
# crashes to the user at the next start.
faulthandler.enable()
# See https://bugreports.qt-project.org/browse/QTBUG-36099 # See https://bugreports.qt-project.org/browse/QTBUG-36099
# We need to do this before importing PyQt # We need to do this before importing PyQt
@ -37,8 +48,8 @@ harfbuzz.fix()
import qutebrowser.utils.dependencies as dependencies import qutebrowser.utils.dependencies as dependencies
dependencies.check() dependencies.check()
# Early imports done, normal imports following now.
import os import os
import sys
import logging import logging
import subprocess import subprocess
import configparser import configparser

View File

@ -56,7 +56,8 @@ def set_trace():
Based on http://stackoverflow.com/a/1745965/2085149 Based on http://stackoverflow.com/a/1745965/2085149
""" """
sys.stdout.flush() if sys.stdout is not None:
sys.stdout.flush()
print() print()
print("When done debugging, remember to execute:") print("When done debugging, remember to execute:")
print(" from PyQt5 import QtCore; QtCore.pyqtRestoreInputHook()") print(" from PyQt5 import QtCore; QtCore.pyqtRestoreInputHook()")

View File

@ -167,7 +167,8 @@ def actute_warning():
with open('/usr/share/X11/locale/en_US.UTF-8/Compose', 'r') as f: with open('/usr/share/X11/locale/en_US.UTF-8/Compose', 'r') as f:
for line in f: for line in f:
if '<dead_actute>' in line: if '<dead_actute>' in line:
sys.stdout.flush() if sys.stdout is not None:
sys.stdout.flush()
print("Note: If you got a 'dead_actute' warning above, that " print("Note: If you got a 'dead_actute' warning above, that "
"is not a bug in qutebrowser! See " "is not a bug in qutebrowser! See "
"https://bugs.freedesktop.org/show_bug.cgi?id=69476 for " "https://bugs.freedesktop.org/show_bug.cgi?id=69476 for "