From de8e160872ee9f9e6ddd4b7efe3726cdb347f8b5 Mon Sep 17 00:00:00 2001 From: Frank Hoffmann <15r10nk-git@polarbit.de> Date: Sun, 9 Jul 2023 12:24:08 +0200 Subject: [PATCH] wip --- scripts/find_issue.py | 74 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 scripts/find_issue.py diff --git a/scripts/find_issue.py b/scripts/find_issue.py new file mode 100644 index 00000000000..22812e1e9fa --- /dev/null +++ b/scripts/find_issue.py @@ -0,0 +1,74 @@ + + +import ast +from pathlib import Path + +from pysource_codegen import generate +from pysource_minimize import minimize + +import black +from blib2to3.pgen2.literals import test + +base_path=Path(__file__).parent + +def bug_in_code(src_contents,mode): + + try: + dst_contents = black.format_str(src_contents, mode=mode) + + black.assert_equivalent(src_contents, dst_contents) + black.assert_stable(src_contents, dst_contents, mode=mode) + except Exception as e: + print("error:",e) + return True + return False + + +def find_issue() -> None: + + for seed in range(38,100000): + #for seed in [4414]: + src_contents=generate(seed) + print("seed:",seed) + + compile(src_contents, "", "exec") + + mode=black.FileMode( + line_length=80, + string_normalization=True, + is_pyi=False, + magic_trailing_comma=False, + ) + + if bug_in_code(src_contents,mode): + new_code=ast.unparse(ast.parse(src_contents,type_comments=True)) + (base_path/"new_code.py").write_text(new_code) + (base_path/"src_code.py").write_text(src_contents) + for a,b in zip(new_code.splitlines(),src_contents.splitlines()): + if a!=b: + print("mismatch") + print(a) + print(b) + + assert bug_in_code(src_contents,mode) + assert bug_in_code(new_code,mode) + + minimized=minimize(src_contents,lambda code:bug_in_code(code,mode)) + + print("seed:",seed) + print("minimized code:") + print(minimized) + assert bug_in_code(minimized,mode) + (base_path/"min_code.py").write_text(minimized) + + if "match" not in minimized: + return + + #if "with" not in minimized: + # return + + + + +if __name__ == "__main__": + find_issue()