1
1
# -*- coding: utf-8 -*-
2
- # Copyright (C) 2015-2018 Rocky Bernstein
2
+ # Copyright (C) 2015-2018, 2020 Rocky Bernstein
3
3
#
4
4
# This program is free software: you can redistribute it and/or modify
5
5
# it under the terms of the GNU General Public License as published by
13
13
#
14
14
# You should have received a copy of the GNU General Public License
15
15
# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
- import os
16
+
17
+ import os .path as osp
17
18
from getopt import getopt , GetoptError
18
19
from uncompyle6 .semantics .fragments import code_deparse
19
- from trepan .lib .deparse import ( deparse_and_cache , deparse_offset )
20
+ from trepan .lib .deparse import deparse_and_cache , deparse_offset
20
21
from pyficache import highlight_string , getlines
21
22
22
23
# Our local modules
23
- from trepan .processor .command import base_cmd as Mbase_cmd
24
+ from trepan .processor .command .base_cmd import DebuggerCommand
25
+
24
26
25
- class DeparseCommand (Mbase_cmd . DebuggerCommand ):
27
+ class DeparseCommand (DebuggerCommand ):
26
28
"""**deparse** [options] [ . ]
27
29
28
30
Options:
29
31
------
30
32
31
33
-p | --parent show parent node
32
- -A | --tree | --AST show abstract syntax tree (AST)
34
+ -t | --tree show parse tree
33
35
-o | --offset [num] show deparse of offset NUM
34
36
-h | --help give this help
35
37
@@ -50,97 +52,106 @@ class DeparseCommand(Mbase_cmd.DebuggerCommand):
50
52
deparse . # deparse current function or main
51
53
deparse --offset 6 # deparse starting at offset 6
52
54
deparse --offsets # show all exect deparsing offsets
53
- deparse --AST # deparse and show AST
55
+ deparse --tree # deparse and show parse tree
54
56
55
57
See also:
56
58
---------
57
59
58
60
`disassemble`, `list`, and `set highlight`
59
61
"""
60
62
61
- category = ' data'
62
- min_args = 0
63
- max_args = 10
64
- name = os . path . basename (__file__ ).split ('.' )[0 ]
65
- need_stack = True
66
- short_help = ' Deparse source via uncompyle6'
63
+ category = " data"
64
+ min_args = 0
65
+ max_args = 10
66
+ name = osp . basename (__file__ ).split ("." )[0 ]
67
+ need_stack = True
68
+ short_help = " Deparse source via uncompyle6"
67
69
68
70
def print_text (self , text ):
69
- if self .settings [' highlight' ] == ' plain' :
71
+ if self .settings [" highlight" ] == " plain" :
70
72
self .msg (text )
71
73
return
72
- opts = {'bg' : self .settings [' highlight' ]}
74
+ opts = {"bg" : self .settings [" highlight" ]}
73
75
74
- if ' style' in self .settings :
75
- opts [' style' ] = self .settings [' style' ]
76
+ if " style" in self .settings :
77
+ opts [" style" ] = self .settings [" style" ]
76
78
self .msg (highlight_string (text , opts ).strip ("\n " ))
77
79
78
80
def run (self , args ):
79
81
co = self .proc .curframe .f_code
80
82
name = co .co_name
81
83
82
84
try :
83
- opts , args = getopt (args [1 :], "hpPAto:O" ,
84
- ["help" , "parent" , "pretty" , "AST" ,
85
- 'tree' , "offset=" , "offsets" ])
85
+ opts , args = getopt (
86
+ args [1 :],
87
+ "hpPto:O" ,
88
+ ["help" , "parent" , "pretty" , "tree" , "offset=" , "offsets" ],
89
+ )
86
90
except GetoptError as err :
87
91
# print help information and exit:
88
- print (str (err )) # will print something like "option -a not recognized"
92
+ self .errmsg (
93
+ str (err )
94
+ ) # will print something like "option -a not recognized"
89
95
return
90
96
91
97
show_parent = False
92
- show_ast = False
98
+ show_tree = False
93
99
offset = None
94
100
show_offsets = False
95
101
for o , a in opts :
96
102
if o in ("-h" , "--help" ):
97
- self .proc .commands [' help' ].run ([' help' , ' deparse' ])
103
+ self .proc .commands [" help" ].run ([" help" , " deparse" ])
98
104
return
99
105
elif o in ("-O" , "--offsets" ):
100
106
show_offsets = True
101
107
elif o in ("-p" , "--parent" ):
102
108
show_parent = True
103
- elif o in ("-A " , "--tree" , '--AST' ):
104
- show_ast = True
105
- elif o in ("-o" , ' --offset' ):
109
+ elif o in ("-t " , "--tree" ,):
110
+ show_tree = True
111
+ elif o in ("-o" , " --offset" ):
106
112
offset = a
107
113
else :
108
114
self .errmsg ("unhandled option '%s'" % o )
109
115
pass
110
116
pass
111
117
nodeInfo = None
112
118
113
- if len (args ) >= 1 and args [0 ] == '.' :
119
+ if len (args ) >= 1 and args [0 ] == "." :
114
120
temp_filename , name_for_code = deparse_and_cache (co , self .errmsg )
115
121
if not temp_filename :
116
122
return
117
- self .print_text ('' .join (getlines (temp_filename )))
123
+ self .print_text ("" .join (getlines (temp_filename )))
118
124
return
119
125
elif show_offsets :
120
126
deparsed = code_deparse (co )
121
127
self .section ("Offsets known:" )
122
- m = self .columnize_commands (list (sorted (deparsed .offsets .keys (),
123
- key = lambda x : str (x [0 ]))))
128
+ m = self .columnize_commands (
129
+ list (sorted (deparsed .offsets .keys (), key = lambda x : str (x [0 ])))
130
+ )
124
131
self .msg_nocr (m )
125
132
return
126
133
elif offset is not None :
127
- mess = ("The 'deparse' command when given an argument requires an"
128
- " instruction offset. Got: '%s'" % offset )
134
+ mess = (
135
+ "The 'deparse' command when given an argument requires an"
136
+ " instruction offset. Got: '%s'" % offset
137
+ )
129
138
last_i = self .proc .get_an_int (offset , mess )
130
139
if last_i is None :
131
140
return
132
141
else :
133
142
last_i = self .proc .curframe .f_lasti
134
- if last_i == - 1 : last_i = 0
143
+ if last_i == - 1 :
144
+ last_i = 0
135
145
136
146
deparsed , nodeInfo = deparse_offset (co , name , last_i , self .errmsg )
137
147
if not deparsed :
138
148
return
149
+
139
150
if nodeInfo :
140
151
extractInfo = deparsed .extract_node_info (nodeInfo )
141
152
parentInfo = None
142
153
# print extractInfo
143
- if show_ast :
154
+ if show_tree :
144
155
p = deparsed .ast
145
156
if show_parent :
146
157
parentInfo , p = deparsed .extract_parent_info (nodeInfo .node )
@@ -168,20 +179,29 @@ def run(self, args):
168
179
else :
169
180
self .msg ("At beginning" )
170
181
else :
171
- self .errmsg ("haven't recorded info for offset %d. Offsets I know are:"
172
- % last_i )
173
- offsets = [key [1 ] for key in deparsed .offsets .keys () if isinstance (key [1 ], int )]
182
+ self .errmsg (
183
+ "haven't recorded info for offset %d. Offsets I know are:" % last_i
184
+ )
185
+ offsets = [
186
+ key [1 ] for key in deparsed .offsets .keys () if isinstance (key [1 ], int )
187
+ ]
174
188
m = self .columnize_commands (list (sorted (offsets )))
175
189
self .msg_nocr (m )
176
190
return
191
+
177
192
pass
178
193
179
- # if __name__ == '__main__':
180
- # from trepan import debugger
181
- # d = debugger.Trepan()
182
- # cp = d.core.processor
183
- # command = DeparseCommand(d.core.processor)
184
- # command.proc.frame = sys._getframe()
185
- # command.proc.setup()
186
- # command.run(['deparse'])
187
- # pass
194
+
195
+ if __name__ == "__main__" :
196
+ import sys
197
+ from trepan import debugger
198
+
199
+ d = debugger .Debugger ()
200
+ cp = d .core .processor
201
+ command = DeparseCommand (d .core .processor )
202
+ command .proc .frame = sys ._getframe ()
203
+ command .proc .setup ()
204
+ command .run (["deparse" , "--bad" ])
205
+ command .run (["deparse" ])
206
+ command .run (["deparse" , "--help" ])
207
+ pass
0 commit comments