Skip to content

Commit 89f57a9

Browse files
pgbovinemmmicedcoffee
authored andcommitted
prettier printing of list/set/dict comprehensions
1 parent f2fc5fa commit 89f57a9

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

v3/pg_logger.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,19 @@ def get_user_stdout(frame):
284284
# at_global_scope should be true only if 'frame' represents the global scope
285285
def get_user_globals(frame, at_global_scope=False):
286286
d = filter_var_dict(frame.f_globals)
287-
# only present in crazy_mode ...
287+
288+
# don't blurt out all of f_valuestack for now ...
289+
'''
288290
if at_global_scope and hasattr(frame, 'f_valuestack'):
289291
for (i, e) in enumerate(frame.f_valuestack):
290292
d['_tmp' + str(i+1)] = e
293+
'''
294+
295+
# print out list objects being built up in Python 2.x list comprehensions
296+
# (which don't have its own special <listcomp> frame, sadly)
297+
if hasattr(frame, 'f_valuestack'):
298+
for (i, e) in enumerate([e for e in frame.f_valuestack if type(e) is list]):
299+
ret['_tmp' + str(i+1)] = e
291300

292301
# also filter out __return__ for globals only, but NOT for locals
293302
if '__return__' in d:
@@ -296,10 +305,27 @@ def get_user_globals(frame, at_global_scope=False):
296305

297306
def get_user_locals(frame):
298307
ret = filter_var_dict(frame.f_locals)
299-
# only present in crazy_mode ...
308+
# don't blurt out all of f_valuestack for now ...
309+
'''
300310
if hasattr(frame, 'f_valuestack'):
301311
for (i, e) in enumerate(frame.f_valuestack):
302312
ret['_tmp' + str(i+1)] = e
313+
'''
314+
315+
# special printing of list/set/dict comprehension objects as they are
316+
# being built up incrementally ...
317+
f_name = frame.f_code.co_name
318+
if hasattr(frame, 'f_valuestack'):
319+
# print out list objects being built up in Python 2.x list comprehensions
320+
# (which don't have its own special <listcomp> frame, sadly)
321+
for (i, e) in enumerate([e for e in frame.f_valuestack if type(e) is list]):
322+
ret['_tmp' + str(i+1)] = e
323+
324+
# for dict and set comprehensions, which have their own frames:
325+
if f_name.endswith('comp>'):
326+
for (i, e) in enumerate([e for e in frame.f_valuestack
327+
if type(e) in (set, dict)]):
328+
ret['_tmp' + str(i+1)] = e
303329

304330
return ret
305331

0 commit comments

Comments
 (0)