-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_ast.py
56 lines (49 loc) · 1.49 KB
/
test_ast.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import re
import difflib
import pytest
import ast as py_ast
import rustpython_ast as rust_ast
from glob import glob
files = {}
for path in glob("../../cpython/Lib/**/*.py"):
try:
txt = open(path, "r").read()
except UnicodeDecodeError:
continue
# try:
# if py_ast.dump(py_ast.parse(txt)) != py_ast.dump(rust_ast.parse(txt)):
# continue
# except SyntaxError:
# continue
files[path] = txt
@pytest.mark.parametrize("path", files.keys())
def test_roundtrip(path):
txt = files[path]
module_p = py_ast.parse(txt)
dump_p = py_ast.dump(module_p, indent=True)
module_r = rust_ast.parse(txt)
dump_r = py_ast.dump(module_r, indent=True)
p = re.compile("object at 0x[0-9a-f]+")
dump_p2 = re.sub(p, "object at 0x????????", dump_p)
dump_r2 = re.sub(p, "object at 0x????????", dump_r)
try:
assert dump_p2 == dump_r2
except AssertionError:
last_sign = ' '
for s in difflib.ndiff(dump_p2, dump_r2):
if s[0]==' ': continue
if s[0] == last_sign:
print(s[2:], end='')
else:
print()
print(s, end='')
last_sign = s[0]
# with open("dump_code.py", "w") as f:
# f.write(path)
# f.write('\n')
# f.write(txt)
# with open("dump_p.txt", "w") as f:
# f.write(dump_p2)
# with open("dump_r.txt", "w") as f:
# f.write(dump_r2)
raise