Restore cell vars support in the ordered printing.

This commit is contained in:
Alexander Bersenev 2019-05-11 13:32:34 +05:00 committed by Ram Rachum
parent e4cd9974b2
commit 17d8a41b3b
2 changed files with 65 additions and 3 deletions

View file

@ -21,13 +21,14 @@ ipython_filename_pattern = re.compile('^<ipython-input-([0-9]+)-.*>$')
def get_local_reprs(frame, watch=()):
var_names = frame.f_code.co_varnames + frame.f_code.co_cellvars
result = collections.OrderedDict(
(key, utils.get_shortish_repr(frame.f_locals[key]))
for key in frame.f_code.co_varnames if key in frame.f_locals
for key in var_names if key in frame.f_locals
)
result.update(sorted((key, utils.get_shortish_repr(frame.f_locals[key]))
for key in set(frame.f_locals) - set(frame.f_code.co_varnames)))
result.update((key, utils.get_shortish_repr(frame.f_locals[key]))
for key in frame.f_code.co_freevars)
for variable in watch:
result.update(sorted(variable.items(frame)))

View file

@ -921,6 +921,67 @@ def test_with_block_depth():
)
)
def test_cellvars():
string_io = io.StringIO()
def f2(a):
def f3(a):
x = 0
x += 1
def f4(a):
y = x
return 42
return f4(a)
return f3(a)
def f1(a):
with pysnooper.snoop(string_io, depth=4):
result1 = f2(a)
return result1
result = f1(42)
assert result == 42
output = string_io.getvalue()
assert_output(
output,
(
VariableEntry(),
VariableEntry(),
VariableEntry(),
LineEntry('result1 = f2(a)'),
VariableEntry(),
CallEntry('def f2(a):'),
LineEntry(),
VariableEntry(),
LineEntry(),
VariableEntry("a"),
CallEntry('def f3(a):'),
LineEntry(),
VariableEntry("x"),
LineEntry(),
VariableEntry("x"),
LineEntry(),
VariableEntry(),
LineEntry(),
VariableEntry(),
VariableEntry("x"),
CallEntry('def f4(a):'),
LineEntry(),
VariableEntry(),
LineEntry(),
ReturnEntry(),
ReturnValueEntry(),
ReturnEntry(),
ReturnValueEntry(),
ReturnEntry(),
ReturnValueEntry(),
)
)
def test_truncate():
max_length = 20