def test_unwinding_exceptions(self): # SETUP code = """\ def foo(n): return n/0 x = foo(5) """ expected_report_python2 = """\ n = 5 ZeroDivisionError: integer division or modulo by zero ZeroDivisionError: integer division or modulo by zero """ expected_report_python3 = """\ n = 5 ZeroDivisionError: division by zero ZeroDivisionError: division by zero """ expected_report = (expected_report_python3 if version_info.major >= 3 else expected_report_python2) tracer = CodeTracer() # EXEC report = tracer.trace_code(code) # VERIFY self.assertEqual(expected_report.splitlines(), report.splitlines())
def test_yield(self): # SETUP code = """\ def foo(x): a = x yield a a += 2 yield a n = foo(10) s = sum(n) """ expected_report = """\ x = 10 a = 10 yield 10 a = 12 yield 12 s = 22 """ tracer = CodeTracer() # EXEC report = tracer.trace_code(code) # VERIFY self.assertReportEqual(expected_report, report)
def test_recursion(self): # SETUP code = """\ def f(n): r = 1 for i in range(n): r += f(i) return r r = f(2) """ expected_report = """\ n = 2 | n = 0 | n = 1 | n = 0 r = 1 | r = 1 | r = 1 | r = 1 i = 0 | i = 1 | | i = 0 | r = 2 | r = 4 | | r = 2 | return 4 | return 1 | return 2 | return 1 r = 4 """ tracer = CodeTracer() # EXEC report = tracer.trace_code(code) # VERIFY self.assertReportEqual(expected_report, report)
def test_recursion_exception(self): # SETUP code = """\ def f(n): m = n - 1 if m == 0: raise RuntimeError('Invalid n.') return f(m) r = f(2) """ expected_report = """\ n = 2 | n = 1 m = 1 | m = 0 | | RuntimeError: Invalid n. RuntimeError: Invalid n. | RuntimeError: Invalid n. """ tracer = CodeTracer() # EXEC report = tracer.trace_code(code) # VERIFY self.assertReportEqual(expected_report, report)
def test_incomplete_iterator(self): # SETUP code = """\ def f(): yield 1 yield 2 r = f() x = next(r) y = next(r) #z = next(r) #Don't complete the iterator, so the function never exits. n = x """ expected_report = """\ yield 1 yield 2 x = 1 y = 2 n = 1 """ tracer = CodeTracer() # EXEC report = tracer.trace_code(code) # VERIFY self.assertEqual(expected_report.splitlines(), report.splitlines())
def test_repr(self): # SETUP code = """\ class Dog(object): def __init__(self, name): self.name = name def __repr__(self): return 'Dog(%r)' % self.name dog = Dog('Spot') animal = dog """ expected_report = """\ name = 'Spot' self.name = 'Spot' dog = Dog('Spot') animal = Dog('Spot') """ tracer = CodeTracer() # EXEC report = tracer.trace_code(code) # VERIFY self.maxDiff = None self.assertReportEqual(expected_report, report)
def test_repr_return(): code = """\ class Foo: def __init__(self, x): self.x = x def __repr__(self): return 'Foo({!r})'.format(self.x) def bar(x): return Foo(x) y = bar(23) """ expected_report = """\ x = 23 self.x = 23 x = 23 return Foo(23) y = Foo(23) """ tracer = CodeTracer() report = tracer.trace_code(code) assert trim_report(expected_report) == trim_report(report)
def test_repr(): code = """\ class Dog(object): def __init__(self, name): self.name = name def __repr__(self): return 'Dog(%r)' % self.name dog = Dog('Spot') animal = dog """ expected_report = """\ name = 'Spot' self.name = 'Spot' dog = Dog('Spot') animal = Dog('Spot') """ tracer = CodeTracer() report = tracer.trace_code(code) assert trim_report(expected_report) == trim_report(report)
def test_infinite_loop_in_repr(self): # SETUP # __repr__() doesn't display messages, so message counter doesn't change. code = """\ class Foo(object): def __repr__(self): while True: x = 42 s = Foo() s2 = 'x' """ # The infinite loop fails silently inside __repr__(). expected_report = """\ s2 = 'x' """ tracer = CodeTracer() # EXEC report = tracer.trace_code(code) # VERIFY self.assertReportEqual(expected_report, report)
def test_function(self): # SETUP code = """\ def foo(x): a = x b = x + 1 return b m = 2 n = foo(m) """ expected_report = """\ x = 2 a = 2 b = 3 return 3 m = 2 n = 3 """ tracer = CodeTracer() # EXEC report = tracer.trace_code(code) # VERIFY self.assertReportEqual(expected_report, report)
def test_repr_yield_tuple(): code = """\ class Foo: def __init__(self, x): self.x = x def __repr__(self): return 'Foo({!r})'.format(self.x) def bar(x): yield x, Foo(x) y = list(bar(23)) """ expected_report = """\ x = 23 self.x = 23 x = 23 yield (23, Foo(23)) y = [(23, Foo(23))] """ tracer = CodeTracer() report = tracer.trace_code(code) assert trim_report(expected_report) == trim_report(report)
def test_string_io(self): # SETUP code = """\ from __future__ import unicode_literals from io import StringIO f = StringIO() f.write('x') """ expected_report_python2 = """\ f.write(u'x') """ expected_report_python3 = """\ f.write('x') """ expected_report = (expected_report_python3 if version_info.major >= 3 else expected_report_python2) tracer = CodeTracer() # EXEC report = tracer.trace_code(code) # VERIFY self.assertReportEqual(expected_report, report)
def test_repr_lambda(self): # SETUP code = """\ class Foo: def __init__(self, x): self.x = x def __repr__(self): return 'Foo({!r})'.format(self.x) y = list(map(lambda n: Foo(n), range(2))) """ expected_report = """\ x = 0 | x = 1 self.x = 0 | self.x = 1 (0 => Foo(0)) | (1 => Foo(1)) | y = [Foo(0), Foo(1)] """ tracer = CodeTracer() # EXEC report = tracer.trace_code(code) # VERIFY self.assertReportEqual(expected_report, report)
def test_repr_yield_tuple(self): # SETUP code = """\ class Foo: def __init__(self, x): self.x = x def __repr__(self): return 'Foo({!r})'.format(self.x) def bar(x): yield x, Foo(x) y = list(bar(23)) """ expected_report = """\ x = 23 self.x = 23 x = 23 yield (23, Foo(23)) y = [(23, Foo(23))] """ tracer = CodeTracer() # EXEC report = tracer.trace_code(code) # VERIFY self.assertReportEqual(expected_report, report)
def test_repr_return(self): # SETUP code = """\ class Foo: def __init__(self, x): self.x = x def __repr__(self): return 'Foo({!r})'.format(self.x) def bar(x): return Foo(x) y = bar(23) """ expected_report = """\ x = 23 self.x = 23 x = 23 return Foo(23) y = Foo(23) """ tracer = CodeTracer() # EXEC report = tracer.trace_code(code) # VERIFY self.assertReportEqual(expected_report, report)
def test_repr_lambda(): code = """\ class Foo: def __init__(self, x): self.x = x def __repr__(self): return 'Foo({!r})'.format(self.x) y = list(map(lambda n: Foo(n), range(2))) """ expected_report = """\ x = 0 | x = 1 self.x = 0 | self.x = 1 (0 => Foo(0)) | (1 => Foo(1)) | y = [Foo(0), Foo(1)] """ tracer = CodeTracer() report = tracer.trace_code(code) assert trim_report(expected_report) == trim_report(report)
def wrapper(*args, **kwargs): # create a global variable to store the result within. Make sure it is unlikely to already exist exec_code, var_name = append_fn_call(source, fn) tracer = CodeTracer() tracer.max_width = max_width local_context_copy = dict(locals()) trace = tracer.trace_code(exec_code, global_context=fn.__globals__, local_context=local_context_copy, dump=dump) trace = remove_fn_call(trace) # add back in the preamble if dumping code also if dump: trace = preamble + "\n" + trace print("*** Trace Output *** ".ljust(80, "-")) print(trace) print("*** END Trace Output *** ".ljust(80, "-")) # get return val ret_val = None if var_name in local_context_copy: ret_val = local_context_copy[var_name] return ret_val
def test_error(): code = """\ from __future__ import print_function class Foo(object): def __repr__(self): raise RuntimeError('Bad representation.') def create_foo(): return Foo() print('Start.') foo = create_foo() print('End.') """ expected_report = """\ return <Foo object> print('Start.') print('End.') """ tracer = CodeTracer() report = tracer.trace_code(code) assert trim_report(expected_report) == trim_report(report)
def test_chained_function(self): # SETUP code = """\ def foo(x): return x + 10 def bar(y): return foo(y) - 2 n = bar(3) """ expected_report = """\ x = 3 return 13 y = 3 return 11 n = 11 """ tracer = CodeTracer() # EXEC report = tracer.trace_code(code) # VERIFY self.assertReportEqual(expected_report, report)
def test_repr_call(): code = """\ class Dog(object): def __init__(self, name): self.name = name def __repr__(self): return 'Dog(%r)' % self.name dog1 = Dog('Spot') dog2 = Dog('Fido') s = repr(dog2) """ expected_report = """\ name = 'Spot' | name = 'Fido' self.name = 'Spot' | self.name = 'Fido' return "Dog('Fido')" dog1 = Dog('Spot') dog2 = Dog('Fido') s = "Dog('Fido')" """ tracer = CodeTracer() report = tracer.trace_code(code) assert trim_report(expected_report) == trim_report(report)
def test_repr(self): # SETUP code = """\ class Dog(object): def __init__(self, name): self.name = name def __repr__(self): return 'Dog(%r)' % self.name dog = Dog('Spot') animal = dog """ expected_report = """\ name = 'Spot' self.name = 'Spot' dog = Dog('Spot') animal = Dog('Spot') """ tracer = CodeTracer() # EXEC report = tracer.trace_code(code) # VERIFY self.maxDiff = None self.assertEqual(expected_report.splitlines(), report.splitlines())
def test_multiline_error(self): # SETUP code = """\ quality = '' for c in ['a', None]: quality += c """ expected_report = """\ quality = '' c = 'a' | c = None | quality = 'a' | TypeError:""" if version_info.major >= 3: expected_report += ( " Can't convert 'NoneType' object to str implicitly") else: expected_report += ( " cannot concatenate 'str' and 'NoneType' objects") tracer = CodeTracer() # EXEC report = tracer.trace_code(code) # VERIFY self.assertReportEqual(expected_report, report)
def test_string_io(): code = """\ from __future__ import unicode_literals from io import StringIO f = StringIO() f.write('x') """ expected_report_python2 = """\ f.write(u'x') """ expected_report_python3 = """\ f.write('x') """ expected_report = (expected_report_python3 if version_info.major >= 3 else expected_report_python2) tracer = CodeTracer() report = tracer.trace_code(code) assert expected_report == trim_report(report)
def test_nested_reraise(): code = """\ x = 2 try: try: raise RuntimeError('Bad stuff happened.') except: raise except: raise """ expected_report = """\ x = 2 RuntimeError: Bad stuff happened. RuntimeError: Bad stuff happened. RuntimeError: Bad stuff happened. """ tracer = CodeTracer() report = tracer.trace_code(code) assert expected_report == trim_report(report)
def test_repr_call(self): # SETUP code = """\ class Dog(object): def __init__(self, name): self.name = name def __repr__(self): return 'Dog(%r)' % self.name dog1 = Dog('Spot') dog2 = Dog('Fido') s = repr(dog2) """ expected_report = """\ name = 'Spot' | name = 'Fido' self.name = 'Spot' | self.name = 'Fido' return "Dog('Fido')" dog1 = Dog('Spot') dog2 = Dog('Fido') s = "Dog('Fido')" """ tracer = CodeTracer() # EXEC report = tracer.trace_code(code) # VERIFY self.assertReportEqual(expected_report, report)
def test_docstring(): code = """\ '' """ expected_report = """\ """ tracer = CodeTracer() report = tracer.trace_code(code) assert expected_report == trim_report(report)
def test_assign_generator_unpacked(): code = """\ a, b = (3*i for i in range(2)) """ expected_report = """\ (a, b) = (0, 3) """ tracer = CodeTracer() report = tracer.trace_code(code) assert expected_report == trim_report(report)
def test_assign_tuple_list(): code = """\ a, [b, c] = (1, (2, 3)) """ expected_report = """\ (a, (b, c)) = (1, (2, 3)) """ tracer = CodeTracer() report = tracer.trace_code(code) assert expected_report == trim_report(report)
def test_assign_assignment(): code = """\ a = b = 2 """ expected_report = """\ a = b = 2 """ tracer = CodeTracer() report = tracer.trace_code(code) assert expected_report == trim_report(report)
def test_encode_percent(self): # SETUP original = "80% of everything" expected = "80%25 of everything" tracer = CodeTracer() # EXEC encoded = tracer.encode(original) decoded = tracer.decode(encoded) # VERIFY self.assertEqual(expected, encoded) self.assertEqual(original, decoded)
def test_encode_both(self): # SETUP original = "80% of everything\nis crap" expected = "80%25 of everything%0ais crap" tracer = CodeTracer() # EXEC encoded = tracer.encode(original) decoded = tracer.decode(encoded) # VERIFY self.assertEqual(expected, encoded) self.assertEqual(original, decoded)
def test_numpy_random(): code = """\ import numpy as np x = np.random.normal(size=3) """ tracer = CodeTracer() original_report = tracer.trace_code(code) for _ in range(5): report = tracer.trace_code(code) assert original_report == report
def test_encode_windows_multiline(self): # SETUP original = "first\r\nsecond" expected = "first%0d%0asecond" tracer = CodeTracer() # EXEC encoded = tracer.encode(original) decoded = tracer.decode(encoded) # VERIFY self.assertEqual(expected, encoded) self.assertEqual(original, decoded)
def test_encode_single(self): # SETUP original = "single line" expected = original tracer = CodeTracer() # EXEC encoded = tracer.encode(original) decoded = tracer.decode(encoded) # VERIFY self.assertEqual(expected, encoded) self.assertEqual(original, decoded)
def test_assign_tuple_list(self): # SETUP code = """\ a, [b, c] = (1, (2, 3)) """ expected_report = """\ (a, (b, c)) = (1, (2, 3)) """ tracer = CodeTracer() # EXEC report = tracer.trace_code(code) # VERIFY self.assertReportEqual(expected_report, report)
def test_assign_assignment(self): # SETUP code = """\ a = b = 2 """ expected_report = """\ a = b = 2 """ tracer = CodeTracer() # EXEC report = tracer.trace_code(code) # VERIFY self.assertReportEqual(expected_report, report)
def test_assign_tuple(self): # SETUP code = """\ b, c = 3, 42 """ expected_report = """\ (b, c) = (3, 42) """ tracer = CodeTracer() # EXEC report = tracer.trace_code(code) # VERIFY self.assertReportEqual(expected_report, report)
def test_assign_starred(self): # SETUP code = """\ *a, b = (2, 3) """ expected_report = """\ (*a, b) = (2, 3) """ tracer = CodeTracer() # EXEC report = tracer.trace_code(code) # VERIFY self.assertReportEqual(expected_report, report)
def test_assign_generator_unpacked(self): # SETUP code = """\ a, b = (3*i for i in range(2)) """ expected_report = """\ (a, b) = (0, 3) """ tracer = CodeTracer() # EXEC report = tracer.trace_code(code) # VERIFY self.assertReportEqual(expected_report, report)
def test_increment(self): # SETUP code = """\ i = 1 i += 1 """ expected_report = """\ i = 1 i = 2 """ # EXEC report = CodeTracer().trace_code(code) # VERIFY self.assertEqual(expected_report.splitlines(), report.splitlines())
def test_call_on_literal(self): # SETUP code = """\ s = 'abc'.replace('a', 'A') """ expected_report = """\ s = 'Abc' """ tracer = CodeTracer() # EXEC report = tracer.trace_code(code) # VERIFY self.assertEqual(expected_report.splitlines(), report.splitlines())
def test_assign_starred(): code = """\ a, *b = (1, 2, 3) print(b) """ expected_report = """\ (a, *b) = (1, 2, 3) print('[2, 3]') """ tracer = CodeTracer() report = tracer.trace_code(code) assert expected_report == trim_report(report)
def test_runtime_error(): code = """\ x = 2 raise RuntimeError('Bad stuff happened.') """ expected_report = """\ x = 2 RuntimeError: Bad stuff happened. """ tracer = CodeTracer() report = tracer.trace_code(code) assert expected_report == trim_report(report)
def test_call_on_literal(self): # SETUP code = """\ s = 'abc'.replace('a', 'A') """ expected_report = """\ s = 'Abc' """ tracer = CodeTracer() # EXEC report = tracer.trace_code(code) # VERIFY self.assertReportEqual(expected_report, report)
def test_lambda(self): # SETUP code = """\ f = lambda n: n + 1 x = f(10) """ expected_report = """\ n = 10 x = 11 """ tracer = CodeTracer() # EXEC report = tracer.trace_code(code) # VERIFY self.assertReportEqual(expected_report, report)
def test_assign_tuple_list(self): # SETUP code = """\ a, [b, c] = (1, (2, 3)) """ expected_report = """\ a = 1 b = 2 c = 3 """ tracer = CodeTracer() # EXEC report = tracer.trace_code(code) # VERIFY self.assertEqual(expected_report.splitlines(), report.splitlines())
def test_method_call(self): # SETUP code = """\ a = [2, 1] a.sort() a.sort() # second call makes no change, nothing printed """ expected_report = """\ a = [2, 1] a = [1, 2] """ # EXEC report = CodeTracer().trace_code(code) # VERIFY self.assertEqual(expected_report.splitlines(), report.splitlines())