Preserve the local variables order in the output.

The variables are outputed in the declare order, instead of alphabetical.

This causes function arguments to be printed in the declare order.
All watched variables are outputted after declared ones.
This commit is contained in:
Alexander Bersenev 2019-05-11 03:12:33 +05:00 committed by Ram Rachum
parent 90a1946529
commit f4117c1565
1 changed files with 11 additions and 8 deletions

View File

@ -21,8 +21,11 @@ ipython_filename_pattern = re.compile('^<ipython-input-([0-9]+)-.*>$')
def get_local_reprs(frame, watch=()): def get_local_reprs(frame, watch=()):
result = {key: utils.get_shortish_repr(value) for key, value result = collections.OrderedDict(
in frame.f_locals.items()} (key, utils.get_shortish_repr(frame.f_locals[key]))
for key in frame.f_code.co_varnames if key in frame.f_locals
)
for variable in watch: for variable in watch:
result.update(variable.items(frame)) result.update(variable.items(frame))
return result return result
@ -187,7 +190,7 @@ class Tracer:
v if isinstance(v, BaseVariable) else Exploding(v) v if isinstance(v, BaseVariable) else Exploding(v)
for v in utils.ensure_tuple(watch_explode) for v in utils.ensure_tuple(watch_explode)
] ]
self.frame_to_local_reprs = collections.defaultdict(lambda: {}) self.frame_to_local_reprs = collections.OrderedDict()
self.depth = depth self.depth = depth
self.prefix = prefix self.prefix = prefix
self.overwrite = overwrite self.overwrite = overwrite
@ -280,12 +283,12 @@ class Tracer:
### Reporting newish and modified variables: ########################## ### Reporting newish and modified variables: ##########################
# # # #
old_local_reprs = self.frame_to_local_reprs[frame] old_local_reprs = self.frame_to_local_reprs.get(frame, {})
self.frame_to_local_reprs[frame] = local_reprs = \ self.frame_to_local_reprs[frame] = local_reprs = \
get_local_reprs(frame, watch=self.watch) get_local_reprs(frame, watch=self.watch)
modified_local_reprs = {} modified_local_reprs = collections.OrderedDict()
newish_local_reprs = {} newish_local_reprs = collections.OrderedDict()
for key, value in local_reprs.items(): for key, value in local_reprs.items():
if key not in old_local_reprs: if key not in old_local_reprs:
@ -295,10 +298,10 @@ class Tracer:
newish_string = ('Starting var:.. ' if event == 'call' else newish_string = ('Starting var:.. ' if event == 'call' else
'New var:....... ') 'New var:....... ')
for name, value_repr in sorted(newish_local_reprs.items()): for name, value_repr in newish_local_reprs.items():
self.write('{indent}{newish_string}{name} = {value_repr}'.format( self.write('{indent}{newish_string}{name} = {value_repr}'.format(
**locals())) **locals()))
for name, value_repr in sorted(modified_local_reprs.items()): for name, value_repr in modified_local_reprs.items():
self.write('{indent}Modified var:.. {name} = {value_repr}'.format( self.write('{indent}Modified var:.. {name} = {value_repr}'.format(
**locals())) **locals()))
# # # #