Clean up debug methods

This commit is contained in:
Florian Bruhin 2014-06-17 11:12:55 +02:00
parent 52bbabd91a
commit 61e8940ccb
2 changed files with 63 additions and 61 deletions

View File

@ -29,7 +29,7 @@ from base64 import b64encode
from PyQt5.QtWidgets import QApplication, QDialog, QMessageBox
from PyQt5.QtCore import (pyqtSlot, QTimer, QEventLoop, Qt, QStandardPaths,
qInstallMessageHandler, QObject)
qInstallMessageHandler)
import qutebrowser
import qutebrowser.commands.utils as cmdutils
@ -59,7 +59,6 @@ from qutebrowser.utils.misc import (get_standard_dir, actute_warning,
get_qt_args)
from qutebrowser.utils.readline import ReadlineBridge
from qutebrowser.utils.usertypes import Timer
from qutebrowser.utils.debug import set_trace # pylint: disable=unused-import
class Application(QApplication):
@ -516,10 +515,11 @@ class Application(QApplication):
# self.shutdown()
@cmdutils.register(instance='', split=False, debug=True)
def pyeval(self, s):
def debug_pyeval(self, s):
"""Evaluate a python string and display the results as a webpage.
:pyeval command handler.
We have this here rather in utils.debug so the context of eval makes
more sense and because we don't want to import much stuff in the utils.
Args:
s: The string to evaluate.
@ -532,25 +532,6 @@ class Application(QApplication):
qutescheme.pyeval_output = out
self.mainwindow.tabs.cmd.openurl('qute:pyeval')
@cmdutils.register(instance='', debug=True)
def all_widgets(self):
"""Print a list of all widgets to debug log."""
widgets = self.allWidgets()
log.misc.debug("{} widgets".format(len(widgets)))
widgets.sort(key=lambda e: repr(e))
for w in widgets:
log.misc.debug(repr(w))
@cmdutils.register(instance='', debug=True)
def all_objects(self, obj=None, depth=0):
"""Dump all children of an object recursively."""
if obj is None:
obj = self
for kid in obj.findChildren(QObject):
log.misc.debug(' ' * depth + repr(kid))
self.all_objects(kid, depth + 1)
@pyqtSlot()
def shutdown(self):
"""Try to shutdown everything cleanly.
@ -618,24 +599,3 @@ class Application(QApplication):
"""
log.destroy.debug("Shutdown complete, quitting.")
self.quit()
@cmdutils.register(debug=True)
def crash(typ='exception'):
"""Crash for debugging purposes.
Args:
typ: either 'exception' or 'segfault'
Raises:
raises Exception when typ is not segfault.
segfaults when typ is (you don't say...)
"""
if typ == 'segfault':
# From python's Lib/test/crashers/bogus_code_obj.py
co = types.CodeType(0, 0, 0, 0, 0, b'\x04\x71\x00\x00', (), (), (),
'', '', 1, b'')
exec(co) # pylint: disable=exec-used
raise Exception("Segfault failed (wat.)")
else:
raise Exception("Forced crash")

View File

@ -20,7 +20,8 @@
import sys
from functools import wraps
from PyQt5.QtCore import pyqtRemoveInputHook, QEvent
from PyQt5.QtCore import (pyqtRemoveInputHook, QEvent, QCoreApplication,
QObject)
from qutebrowser.utils.log import misc as logger
@ -34,6 +35,63 @@ except ImportError:
import qutebrowser.commands.utils as cmdutils
@cmdutils.register(debug=True)
def debug_set_trace():
"""Set a tracepoint in the Python debugger that works with Qt.
Based on http://stackoverflow.com/a/1745965/2085149
"""
if sys.stdout is not None:
sys.stdout.flush()
print()
print("When done debugging, remember to execute:")
print(" from PyQt5 import QtCore; QtCore.pyqtRestoreInputHook()")
print("before executing c(ontinue).")
pyqtRemoveInputHook()
pdb_set_trace()
@cmdutils.register(debug=True)
def debug_crash(typ='exception'):
"""Crash for debugging purposes.
Args:
typ: either 'exception' or 'segfault'
Raises:
raises Exception when typ is not segfault.
segfaults when typ is (you don't say...)
"""
if typ == 'segfault':
# From python's Lib/test/crashers/bogus_code_obj.py
co = types.CodeType(0, 0, 0, 0, 0, b'\x04\x71\x00\x00', (), (), (),
'', '', 1, b'')
exec(co) # pylint: disable=exec-used
raise Exception("Segfault failed (wat.)")
else:
raise Exception("Forced crash")
@cmdutils.register(debug=True)
def debug_all_widgets():
"""Print a list of all widgets to debug log."""
widgets = QCoreApplication.instance().allWidgets()
logger.debug("{} widgets".format(len(widgets)))
widgets.sort(key=lambda e: repr(e))
for w in widgets:
logger.debug(repr(w))
@cmdutils.register(debug=True)
def debug_all_objects(obj=None, depth=0):
"""Dump all children of an object recursively."""
if obj is None:
obj = QCoreApplication.instance()
for kid in obj.findChildren(QObject):
logger.debug(' ' * depth + repr(kid))
debug_all_objects(kid, depth + 1)
def log_events(klass):
"""Class decorator to log Qt events."""
old_event = klass.event
@ -49,22 +107,6 @@ def log_events(klass):
return klass
@cmdutils.register(debug=True)
def set_trace():
"""Set a tracepoint in the Python debugger that works with Qt.
Based on http://stackoverflow.com/a/1745965/2085149
"""
if sys.stdout is not None:
sys.stdout.flush()
print()
print("When done debugging, remember to execute:")
print(" from PyQt5 import QtCore; QtCore.pyqtRestoreInputHook()")
print("before executing c(ontinue).")
pyqtRemoveInputHook()
pdb_set_trace()
def trace_lines(do_trace):
"""Turn on/off printing each executed line.