|
| 1 | + |
| 2 | + |
| 3 | +import ast |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +from pysource_codegen import generate |
| 7 | +from pysource_minimize import minimize |
| 8 | + |
| 9 | +import black |
| 10 | +from blib2to3.pgen2.literals import test |
| 11 | + |
| 12 | +base_path=Path(__file__).parent |
| 13 | + |
| 14 | +def bug_in_code(src_contents,mode): |
| 15 | + |
| 16 | + try: |
| 17 | + dst_contents = black.format_str(src_contents, mode=mode) |
| 18 | + |
| 19 | + black.assert_equivalent(src_contents, dst_contents) |
| 20 | + black.assert_stable(src_contents, dst_contents, mode=mode) |
| 21 | + except Exception as e: |
| 22 | + print("error:",e) |
| 23 | + return True |
| 24 | + return False |
| 25 | + |
| 26 | + |
| 27 | +def find_issue() -> None: |
| 28 | + |
| 29 | + for seed in range(38,100000): |
| 30 | + #for seed in [4414]: |
| 31 | + src_contents=generate(seed) |
| 32 | + print("seed:",seed) |
| 33 | + |
| 34 | + compile(src_contents, "<string>", "exec") |
| 35 | + |
| 36 | + mode=black.FileMode( |
| 37 | + line_length=80, |
| 38 | + string_normalization=True, |
| 39 | + is_pyi=False, |
| 40 | + magic_trailing_comma=False, |
| 41 | + ) |
| 42 | + |
| 43 | + if bug_in_code(src_contents,mode): |
| 44 | + new_code=ast.unparse(ast.parse(src_contents,type_comments=True)) |
| 45 | + (base_path/"new_code.py").write_text(new_code) |
| 46 | + (base_path/"src_code.py").write_text(src_contents) |
| 47 | + for a,b in zip(new_code.splitlines(),src_contents.splitlines()): |
| 48 | + if a!=b: |
| 49 | + print("mismatch") |
| 50 | + print(a) |
| 51 | + print(b) |
| 52 | + |
| 53 | + assert bug_in_code(src_contents,mode) |
| 54 | + assert bug_in_code(new_code,mode) |
| 55 | + |
| 56 | + minimized=minimize(src_contents,lambda code:bug_in_code(code,mode)) |
| 57 | + |
| 58 | + print("seed:",seed) |
| 59 | + print("minimized code:") |
| 60 | + print(minimized) |
| 61 | + assert bug_in_code(minimized,mode) |
| 62 | + (base_path/"min_code.py").write_text(minimized) |
| 63 | + |
| 64 | + if "match" not in minimized: |
| 65 | + return |
| 66 | + |
| 67 | + #if "with" not in minimized: |
| 68 | + # return |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +if __name__ == "__main__": |
| 74 | + find_issue() |
0 commit comments