[BUGFIX] If(): test_comment bool convert; skip handling after return; do not echo constants.

This commit is contained in:
Yuyao Huang 2024-04-23 22:58:51 +08:00
parent 675b112a16
commit 963a65fa71
6 changed files with 9 additions and 6 deletions

View File

@ -9,7 +9,6 @@ def test_constant():
asserteq_or_print(target(), ''' asserteq_or_print(target(), '''
def target(): def target():
1
''') ''')

View File

@ -36,8 +36,8 @@ class Commentor(object):
with open(inspect.getfile(func)) as f: with open(inspect.getfile(func)) as f:
all_lines = f.readlines() all_lines = f.readlines()
other_lines = dict( other_lines = dict(
before="".join(all_lines[:start_lineno-1]), before="".join(all_lines[:start_lineno-1]) + "\n",
after="".join(all_lines[start_lineno+len(raw_lines):]) after="\n" + "".join(all_lines[start_lineno+len(raw_lines):])
) )
self.indent = len(raw_lines[0]) - len(raw_lines[0].lstrip()) self.indent = len(raw_lines[0]) - len(raw_lines[0].lstrip())
unindented_source = ''.join([l[self.indent:] for l in raw_lines]) unindented_source = ''.join([l[self.indent:] for l in raw_lines])

View File

@ -14,11 +14,12 @@ NORMAL = 0
BREAK = 8 BREAK = 8
CONTINUE = 16 CONTINUE = 16
RAISE = 32 RAISE = 32
RETURN = 64
REG = lambda i: f"__REG{i}" REG = lambda i: f"__REG{i}"
APPEND_SOURCE_BY_THEMSELVES = [ APPEND_SOURCE_BY_THEMSELVES = [
ast.If, ast.For, ast.With ast.If, ast.For, ast.With, ast.Expr
] ]
ASSIGN_SILENT = [ ASSIGN_SILENT = [

View File

@ -11,7 +11,7 @@ def If(self, cmtor, state=0):
test = False test = False
test_comment = "skipped" test_comment = "skipped"
else: else:
test = cmtor.eval(self.test, format=False) test = bool(cmtor.eval(self.test, format=False))
test_comment = test test_comment = test
if test: if test:
state = state | PASS state = state | PASS

View File

@ -25,6 +25,7 @@ def FunctionDef(self, cmtor):
def Return(self, cmtor): def Return(self, cmtor):
cmtor.process(self.value) cmtor.process(self.value)
cmtor._return = cmtor.eval(self.value, format=False) cmtor._return = cmtor.eval(self.value, format=False)
cmtor._stack_event = flags.RETURN
def Lambda(self, cmtor): def Lambda(self, cmtor):

View File

@ -2,7 +2,9 @@ from ..utils import *
def Expr(self, cmtor): 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): def UnaryOp(self, cmtor):