-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, "<string>", "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() |