Move early init code to own file

This commit is contained in:
Florian Bruhin 2014-05-13 18:01:10 +02:00
parent ee02b44117
commit 8fddd328f6
3 changed files with 63 additions and 77 deletions

View File

@ -18,38 +18,15 @@
"""Initialization of qutebrowser and application-wide things.""" """Initialization of qutebrowser and application-wide things."""
### Things we want to do before normal imports ### ### Things we want to do before normal imports ###
import qutebrowser.utils.earlyinit as earlyinit
earlyinit.check_python_version()
earlyinit.init_faulthandler()
earlyinit.fix_harfbuzz()
earlyinit.check_pyqt_core()
earlyinit.check_pyqt_webkit()
import sys
# Print a nice traceback on segfault -- only available on Python 3.3+, but if
# it's unavailable, it doesn't matter much.
try:
import faulthandler # pylint: disable=import-error
except ImportError:
pass
else:
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
# We need to do this before importing PyQt
import qutebrowser.utils.harfbuzz as harfbuzz
harfbuzz.fix()
# Check if everything we should be able to import is there
import qutebrowser.utils.dependencies as dependencies
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

@ -1,5 +1,3 @@
# Copyright 2014 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
#
# This file is part of qutebrowser. # This file is part of qutebrowser.
# #
# qutebrowser is free software: you can redistribute it and/or modify # qutebrowser is free software: you can redistribute it and/or modify
@ -15,21 +13,66 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>. # along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
"""Check if everything needed for qutebrowser is there. """Things which need to be done really early (e.g. before importing Qt)."""
In a separate file because this needs to be done early before any imports.
"""
import os
import sys import sys
def check(): def check_python_version():
"""Check if all dependencies are met.""" """Check if correct python version is run."""
if sys.hexversion < 0x03030000: if sys.hexversion < 0x03030000:
print("Fatal error: At least Python 3.3 is required to run " print("Fatal error: At least Python 3.3 is required to run "
"qutebrowser, but {} is installed!".format( "qutebrowser, but {} is installed!".format(
'.'.join(map(str, sys.version_info[:3])))) '.'.join(map(str, sys.version_info[:3]))))
sys.exit(1) sys.exit(1)
def init_faulthandler():
"""Enable faulthandler module if available.
This print a nice traceback on segfauls. It's only available on Python
3.3+, but if it's unavailable, it doesn't matter much (we just ignore
that).
"""
try:
import faulthandler # pylint: disable=import-error
except ImportError:
return
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.
return
faulthandler.enable()
if hasattr(faulthandler, 'register'):
# If available, we also want a traceback on SIGUSR1.
from signal import SIGUSR1
faulthandler.register(SIGUSR1)
def fix_harfbuzz():
"""Fix harfbuzz issues.
This switches to an older (but more stable) harfbuzz font rendering engine
instead of using the system wide one.
This fixes crashes on various sites.
See https://bugreports.qt-project.org/browse/QTBUG-36099
"""
if sys.platform.startswith('linux'):
# Switch to old but stable font rendering engine
os.environ['QT_HARFBUZZ'] = 'old'
def check_pyqt_core():
"""Check if PyQt core is installed."""
import textwrap import textwrap
try: try:
import PyQt5.QtCore # pylint: disable=unused-variable import PyQt5.QtCore # pylint: disable=unused-variable
@ -59,12 +102,17 @@ def check():
print() print()
traceback.print_exc() traceback.print_exc()
sys.exit(1) sys.exit(1)
def check_pyqt_webkit():
"""Check if PyQt WebKit is installed."""
# At this point we can rely on QtCore being available, so we can display an # At this point we can rely on QtCore being available, so we can display an
# error dialog # error dialog
try: try:
import PyQt5.QtWebKit import PyQt5.QtWebKit
except ImportError: except ImportError:
from PyQt5.QtWidgets import QApplication, QMessageBox from PyQt5.QtWidgets import QApplication, QMessageBox
import textwrap
app = QApplication(sys.argv) app = QApplication(sys.argv)
msgbox = QMessageBox(QMessageBox.Critical, "qutebrowser: Fatal error!", msgbox = QMessageBox(QMessageBox.Critical, "qutebrowser: Fatal error!",
textwrap.dedent(""" textwrap.dedent("""

View File

@ -1,39 +0,0 @@
# Copyright 2014 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
#
# This file is part of qutebrowser.
#
# qutebrowser is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# qutebrowser is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
"""Fixer to set QT_HARFBUZZ variable.
In its own file so it doesn't include any Qt stuff, because if it did, it
wouldn't work.
"""
import os
import sys
def fix():
"""Fix harfbuzz issues.
This switches to an older (but more stable) harfbuzz font rendering engine
instead of using the system wide one.
This fixes crashes on various sites.
See https://bugreports.qt-project.org/browse/QTBUG-36099
"""
if sys.platform.startswith('linux'):
# Switch to old but stable font rendering engine
os.environ['QT_HARFBUZZ'] = 'old'