Skip to content

Commit de36c05

Browse files
committed
started implementing #pythontutor_inline_type: option
1 parent d6ae301 commit de36c05

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

v5-unity/pg_encoder.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,42 @@ def __init__(self, parent):
199199
def should_hide_var(self, var):
200200
return self.parent.should_hide_var(var)
201201

202+
# searches through self.parents.types_to_inline and tries
203+
# to match the type returned by type(obj) and also 'class'/'instance'
204+
# for classes and instances, respectively
205+
def should_inline_object_by_type(self, obj):
206+
# fast-pass optimization -- common case
207+
if not self.parent.types_to_inline:
208+
return False
209+
210+
# copy-pasted from the end of self.encode()
211+
typ = type(obj)
212+
typeStr = str(typ)
213+
m = typeRE.match(typeStr)
214+
if not m:
215+
m = classRE.match(typeStr)
216+
if not m:
217+
return False
218+
219+
assert m
220+
typename = m.group(1)
221+
if not typename:
222+
return False
223+
224+
alt_typename = None
225+
if is_class(obj):
226+
alt_typename = 'class'
227+
elif is_instance(obj):
228+
alt_typename = 'instance'
229+
230+
for re_match in self.parent.types_to_inline:
231+
if re_match(typename):
232+
return True
233+
if alt_typename and re_match(alt_typename):
234+
return True
235+
return False
236+
237+
202238
def get_heap(self):
203239
return self.encoded_heap_objects
204240

v5-unity/pg_logger.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@
6969
# like '#pythontutor_hide: _*' to hide all private instance variables
7070
# - also now filters class and instance fields in addition to top-level vars
7171
PYTUTOR_HIDE_STR = '#pythontutor_hide:'
72+
# 2018-06-17: a comma-separated list of types that should be displayed *inline*
73+
# like primitives. for details of what types are legal to specify, see:
74+
# pg_encoder.py:should_inline_object_by_type()
75+
# - also accepts shell globs, just like PYTUTOR_HIDE_STR
76+
PYTUTOR_INLINE_TYPE_STR = '#pythontutor_inline_type:'
7277

7378
CLASS_RE = re.compile('class\s+')
7479

@@ -605,6 +610,8 @@ def __init__(self, cumulative_mode, heap_primitives, show_only_outputs, finalize
605610
# created by compileGlobMatch() from
606611
# the contents of PYTUTOR_HIDE_STR
607612

613+
self.types_to_inline = set() # a set of regex match objects derived from PYTUTOR_INLINE_TYPE_STR
614+
608615
self.prev_lineno = -1 # keep track of previous line just executed
609616

610617

0 commit comments

Comments
 (0)