diff --git a/tests/test_literals.py b/tests/test_literals.py index 31a0134..1e1b823 100644 --- a/tests/test_literals.py +++ b/tests/test_literals.py @@ -9,7 +9,6 @@ def test_constant(): asserteq_or_print(target(), ''' def target(): - 1 ''') diff --git a/trace_commentor/commentor.py b/trace_commentor/commentor.py index fe63b94..067410d 100644 --- a/trace_commentor/commentor.py +++ b/trace_commentor/commentor.py @@ -36,8 +36,8 @@ class Commentor(object): with open(inspect.getfile(func)) as f: all_lines = f.readlines() other_lines = dict( - before="".join(all_lines[:start_lineno-1]), - after="".join(all_lines[start_lineno+len(raw_lines):]) + before="".join(all_lines[:start_lineno-1]) + "\n", + after="\n" + "".join(all_lines[start_lineno+len(raw_lines):]) ) self.indent = len(raw_lines[0]) - len(raw_lines[0].lstrip()) unindented_source = ''.join([l[self.indent:] for l in raw_lines]) diff --git a/trace_commentor/flags.py b/trace_commentor/flags.py index d1fc98b..e36fd25 100644 --- a/trace_commentor/flags.py +++ b/trace_commentor/flags.py @@ -14,11 +14,12 @@ NORMAL = 0 BREAK = 8 CONTINUE = 16 RAISE = 32 +RETURN = 64 REG = lambda i: f"__REG{i}" APPEND_SOURCE_BY_THEMSELVES = [ - ast.If, ast.For, ast.With + ast.If, ast.For, ast.With, ast.Expr ] ASSIGN_SILENT = [ diff --git a/trace_commentor/handlers/control_flow.py b/trace_commentor/handlers/control_flow.py index 2e702cb..f0ba73d 100644 --- a/trace_commentor/handlers/control_flow.py +++ b/trace_commentor/handlers/control_flow.py @@ -11,7 +11,7 @@ def If(self, cmtor, state=0): test = False test_comment = "skipped" else: - test = cmtor.eval(self.test, format=False) + test = bool(cmtor.eval(self.test, format=False)) test_comment = test if test: state = state | PASS diff --git a/trace_commentor/handlers/definitions.py b/trace_commentor/handlers/definitions.py index 554c6e6..0e3acb1 100644 --- a/trace_commentor/handlers/definitions.py +++ b/trace_commentor/handlers/definitions.py @@ -25,6 +25,7 @@ def FunctionDef(self, cmtor): def Return(self, cmtor): cmtor.process(self.value) cmtor._return = cmtor.eval(self.value, format=False) + cmtor._stack_event = flags.RETURN def Lambda(self, cmtor): diff --git a/trace_commentor/handlers/expressions.py b/trace_commentor/handlers/expressions.py index a4dcd0d..76f2fb2 100644 --- a/trace_commentor/handlers/expressions.py +++ b/trace_commentor/handlers/expressions.py @@ -2,7 +2,9 @@ from ..utils import * def Expr(self, cmtor): - cmtor.process(self.value) + if type(self.value) != ast.Constant: + cmtor.append_source(cmtor.to_source(self)) + cmtor.process(self.value) def UnaryOp(self, cmtor):