From bdd0846397a760b11d5d6765b24bf86e3d73e7da Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 13 May 2014 19:46:57 +0200 Subject: [PATCH] Move python check to separate file --- qutebrowser/app.py | 3 ++- qutebrowser/utils/checkpyver.py | 37 +++++++++++++++++++++++++++++++++ qutebrowser/utils/earlyinit.py | 34 ++++++++---------------------- 3 files changed, 48 insertions(+), 26 deletions(-) create mode 100644 qutebrowser/utils/checkpyver.py diff --git a/qutebrowser/app.py b/qutebrowser/app.py index 941b29c22..b06a94430 100644 --- a/qutebrowser/app.py +++ b/qutebrowser/app.py @@ -18,8 +18,9 @@ """Initialization of qutebrowser and application-wide things.""" ### Things we want to do before normal imports ### +from qutebrowser.utils.checkpyver import check_python_version +check_python_version() import qutebrowser.utils.earlyinit as earlyinit -earlyinit.check_python_version() earlyinit.init_faulthandler() earlyinit.fix_harfbuzz() earlyinit.check_pyqt_core() diff --git a/qutebrowser/utils/checkpyver.py b/qutebrowser/utils/checkpyver.py new file mode 100644 index 000000000..7d4429d23 --- /dev/null +++ b/qutebrowser/utils/checkpyver.py @@ -0,0 +1,37 @@ +# 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 . + +"""Check if qutebrowser is run with the correct python version. + +This should import and run fine with both python2 and python3. +""" + +import sys + + +# First we check the version of Python. This code should run fine with python2 +# and python3. We don't have Qt available here yet, so we just print an error +# to stderr. +def check_python_version(): + """Check if correct python version is run.""" + if sys.hexversion < 0x03030000: + # We don't use .format() and print_function here just in case someone + # still has < 2.6 installed. + version_str = '.'.join(map(str, sys.version_info[:3])) + sys.stderr.write("Fatal error: At least Python 3.3 is required to run " + "qutebrowser, but " + version_str + " is " + "installed!\n") + sys.stderr.flush() + sys.exit(1) diff --git a/qutebrowser/utils/earlyinit.py b/qutebrowser/utils/earlyinit.py index 7f9b4a805..dcd692987 100644 --- a/qutebrowser/utils/earlyinit.py +++ b/qutebrowser/utils/earlyinit.py @@ -15,28 +15,17 @@ """Things which need to be done really early (e.g. before importing Qt). -These functions are supposed to get called in the order they're in this file. -At the very start, we aren't even sure about the Python version used, so we -import more exotic modules later. +At this point we can be sure we have all python 3.3 features available. """ import os import sys +import faulthandler +import textwrap +import traceback +from signal import SIGUSR1 -# First we check the version of Python. This code should run fine with python2 -# and python3. We don't have Qt available here yet, so we just print an error -# to stdout. -def check_python_version(): - """Check if correct python version is run.""" - if sys.hexversion < 0x03030000: - print("Fatal error: At least Python 3.3 is required to run " - "qutebrowser, but {} is installed!".format( - '.'.join(map(str, sys.version_info[:3])))) - sys.exit(1) - - -# At this point we can be sure we have all python 3.3 features available. # Now we initialize the faulthandler as early as possible, so we theoretically # could catch segfaults occuring later. def init_faulthandler(): @@ -44,8 +33,6 @@ def init_faulthandler(): This print a nice traceback on segfauls. """ - import faulthandler - from signal import SIGUSR1 if sys.stderr is not None: # When run with pythonw.exe, sys.stderr can be None: # https://docs.python.org/3/library/sys.html#sys.__stderr__ @@ -83,8 +70,6 @@ def fix_harfbuzz(): # console. def check_pyqt_core(): """Check if PyQt core is installed.""" - import textwrap - import traceback try: import PyQt5.QtCore # pylint: disable=unused-variable except ImportError: @@ -107,9 +92,9 @@ def check_pyqt_core(): For other distributions: Check your package manager for similiarly named packages. - """).strip()) + """).strip(), file=sys.stderr) if '--debug' in sys.argv: - print() + print(file=sys.stderr) traceback.print_exc() sys.exit(1) @@ -119,8 +104,6 @@ def check_pyqt_core(): def check_pyqt_webkit(): """Check if PyQt WebKit is installed.""" from PyQt5.QtWidgets import QApplication, QMessageBox - import textwrap - import traceback try: import PyQt5.QtWebKit # pylint: disable=unused-variable except ImportError: @@ -140,8 +123,9 @@ def check_pyqt_webkit(): Check your package manager for similiarly named packages. """).strip()) if '--debug' in sys.argv: - print() + print(file=sys.stderr) traceback.print_exc() msgbox.resize(msgbox.sizeHint()) msgbox.exec_() app.quit() + sys.exit(1)