From eb2097772d7f4596f36943dab87e7ca967b2fb49 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Mon, 3 Mar 2014 21:22:20 +0100 Subject: [PATCH] Add utils/debug.py --- qutebrowser/utils/debug.py | 63 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 qutebrowser/utils/debug.py diff --git a/qutebrowser/utils/debug.py b/qutebrowser/utils/debug.py new file mode 100644 index 000000000..3adf4661a --- /dev/null +++ b/qutebrowser/utils/debug.py @@ -0,0 +1,63 @@ +# Copyright 2014 Florian Bruhin (The Compiler) +# +# 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 . + +"""Utilities used for debugging.""" + +import sys + +from PyQt5.QtCore import pyqtRemoveInputHook + +try: + # pylint: disable=import-error + from ipdb import set_trace as pdb_set_trace +except ImportError: + from pdb import set_trace as pdb_set_trace + +import qutebrowser.commands.utils as cmdutils + + +@cmdutils.register(name='settrace', hide=True) +def set_trace(): + """Set a tracepoint in the Python debugger that works with Qt. + + Based on http://stackoverflow.com/a/1745965/2085149 + + """ + print() + print("When done debugging, remember to execute:") + print(" from PyQt5 import QtCore; QtCore.pyqtRestoreInputHook()") + print("before executing c(ontinue).") + pyqtRemoveInputHook() + return pdb_set_trace() + + +def trace_lines(do_trace): + """Turn on/off printing each executed line. + + Args: + do_trace: Whether to start tracing (True) or stop it (False). + + """ + def trace(frame, event, _): + """Trace function passed to sys.settrace.""" + print("{}, {}:{}".format(event, frame.f_code.co_filename, + frame.f_lineno)) + return trace + if do_trace: + sys.settrace(trace) + else: + sys.settrace(None)