-
Notifications
You must be signed in to change notification settings - Fork 0
/
trepanmagic.py
121 lines (89 loc) · 3.22 KB
/
trepanmagic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# -*- coding: utf-8 -*-
"""
===========
trepanmagic
===========
IPython Magics for trepan debugging.
.. note::
The ``trepan`` module needs to be installed first
.. sourcecode:: ipython
In [1]: %load_ext trepanmagic
In [2]: import os
In [3]: %trepan_eval os.path.join("a", "b")
In [4]: %trepan --highlight -- pygmentize -f terminal /tmp/example.py 1 3
Usage
=====
The following magic commands are provided:
``%trepan_eval``
Use ``%trepan_eval`` to evaluate an expression under the trepan debugger
Evaluates an expression under the trepan debugger
``%trepan``
Use ``%trepan`` to invoke a top-level Python program with arguments.
You can use ``--`` to separate the debugger arguments from the
program's arguments
%trepan --highlight -- pygmentize -f terminal /tmp/script.py
"""
import IPython
if IPython.version_info[0] < 3:
raise ImportError("You need IPython 3.x, got %d.%d%d" %
IPython.version_info[:3])
from IPython.core.magic import (line_magic, Magics, magics_class
# , cell_magic
# , register_cell_magic
# , register_line_cell_magic
)
try:
import trepan.api
except ImportError:
raise ImportError("You need trepan installed: pip install trepan3k")
from trepan.__main__ import main as trepan_main
import shlex
from trepan import exception as Mexcept
@magics_class
class TrepanMagics(Magics):
"""Magics related to trepan debugging"""
def __init__(self, shell):
super(TrepanMagics, self).__init__(shell)
self.shell = shell
@line_magic
def trepan_eval(self, python_statement):
"""%trepan_eval *python-expression*"""
tb_fn = lambda etype, value, tb: \
self.shell.showtraceback((etype, value, tb), tb_offset=0)
try:
return trepan.api.run_eval(python_statement,
start_opts = {'force': True,
'tb_fn': tb_fn},
debug_opts = {
'from_ipython': self.shell},
globals_ = self.shell.user_ns)
except Mexcept.DebuggerQuit:
pass
@line_magic
def trepan(self, line):
"""%trepan *trepan-options [--] *python-script* [*args*...]"""
sys_argv = shlex.split('trepan --from_ipython ' + line)
try:
return trepan_main(sys_argv=sys_argv)
except Mexcept.DebuggerQuit:
pass
@line_magic
def trepan_pm(self, line):
"""%trepan_pm
Run post-mortem debugger on last traceback
"""
try:
return trepan.post_mortem.pm()
except Mexcept.DebuggerQuit:
pass
def load_ipython_extension(ip):
# The `ipython` argument is the currently active `InteractiveShell`
# instance, which can be used in any way. This allows you to register
# new magics or aliases, for example.
magicsTrepan = TrepanMagics(ip)
ip.register_magics(magicsTrepan)
print("\ntrepanmagic.py loaded")
if __name__ == '__main__':
ip = get_ipython() # NOQA
tm = TrepanMagics(ip)
load_ipython_extension(ip)