"""vast vs CPython ast 全量差分测试(扫描 lib 下所有 .py)""" import sys import os import ast sys.path.insert(0, r'd:\Users\TermiNexus\Desktop\TransPyC') import lib.vast as vast from lib.core.SymbolUtils import ExpandSoftKeywords def main(): root = r'd:\Users\TermiNexus\Desktop\TransPyC\lib' files = [] for dp, dn, fn in os.walk(root): for f in fn: if f.endswith('.py') and '__pycache__' not in dp: files.append(os.path.join(dp, f)) total = 0 fail = 0 fail_list = [] for fp in files: try: src = open(fp, encoding='utf-8').read() except Exception: continue expanded = ExpandSoftKeywords(src) try: ref = ast.dump(ast.parse(expanded)) except SyntaxError as e: continue # CPython 解析不了,跳过 total += 1 try: got = vast.dump(vast.parse(src)) except Exception as e: fail += 1 fail_list.append((fp, f'EXC {type(e).__name__}: {e}')) continue if ref != got: fail += 1 fail_list.append((fp, 'DIFF')) if len(fail_list) <= 15: # 找第一个差异位置 i = 0 while i < min(len(ref), len(got)) and ref[i] == got[i]: i += 1 print(f'=== {os.path.basename(fp)} DIFF @ {i}') print(' ref:', ref[max(0, i-80):i+120]) print(' got:', got[max(0, i-80):i+120]) print('=' * 50) print(f'files={total} FAIL={fail}') for fp, why in fail_list: print(f' {why}: {os.path.relpath(fp, root)}') if __name__ == '__main__': main()