Skip to content

ifs are unparsed inconsistently to ast.unparse, which breaks tests of the libs using astunparse.unparse as a fallback impl. #66

@KOLANICH

Description

@KOLANICH
import ast

import astunparse
import sh  # allows calling binaries as functions
from MempipedPath import MempipedPathRead  # maps byte arrays to anonymous memory maps and exposes them as paths in file system

n = ast.If(
	test=ast.Compare(left=ast.Name(id="a", ctx=ast.Load()), ops=[ast.Gt()], comparators=[ast.Constant(value=0)]),
	body=[ast.Expr(value=ast.Name(id="b", ctx=ast.Load()))],
	orelse=[ast.Expr(value=ast.Name(id="c", ctx=ast.Load()))],
)
ast.fix_missing_locations(n)


if __name__ == "__main__":
	with MempipedPathRead(ast.unparse(n).strip()) as f1:
		with MempipedPathRead(astunparse.unparse(n).strip()) as f2:
			print(sh.diff("-u", f1, "--label", "ast", f2, "--label", "astunparse", _ok_code=1, _env={"LANG": "C"}))

results in

--- ast
+++ astunparse
@@ -1,4 +1,4 @@
-if a > 0:
+if (a > 0):
     b
 else:
     c
\ No newline at end of file

astor.to_source works fine.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions