Skip to content

Commit

Permalink
Merge pull request #11 from 15r10nk/small_fixes
Browse files Browse the repository at this point in the history
Small fixes
  • Loading branch information
15r10nk authored Oct 7, 2023
2 parents f57b0a8 + 241794c commit 7d23df5
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,64 @@ You might find [pysource-minimize](https://github.com/15r10nk/pysource-minimize)
to reduce the generated code which triggers your bug down to a minimal code snipped,
which can be used to fix the issue.

``` python
from pysource_codegen import generate
from pysource_minimize import minimize


def contains_bug(code):
"""
returns True if the code triggers a bug and False otherwise
"""
try:
test_something_with(code) # might throw

if "bug" in code: # maybe other checks
return True
except:
return True
return False


def find_issue():
for seed in range(0, 10000):
code = generate(seed)

if contains_bug(code):
new_code = minimize(code, contains_bug)

print("the following code triggers a bug")
print(new_code)

return


find_issue()
```


## Bugs found in other projects:

### black

* https://github.com/psf/black/issues/3676
* https://github.com/psf/black/issues/3678
* https://github.com/psf/black/issues/3677

### cpython

* https://github.com/python/cpython/issues/109219
* https://github.com/python/cpython/issues/109823
* https://github.com/python/cpython/issues/109719
* https://github.com/python/cpython/issues/109627
* https://github.com/python/cpython/issues/109219
* https://github.com/python/cpython/issues/109118
* https://github.com/python/cpython/issues/109114

## Todo:

* [ ] refactor the existing code
* [ ] use probabilities for the ast-nodes from existing python code (use markov chains)
* [x] support older python versions
* [ ] allow to customize the probabilities to generate code to test specific language features
* [ ] [hypothesis](https://hypothesis.readthedocs.io/en/latest/) support
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pysource-codegen"
version = "0.4.1"
version = "0.4.2"
description = ""
authors = ["Your Name <[email protected]>"]
readme = "README.md"
Expand Down
9 changes: 8 additions & 1 deletion pysource_codegen/_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,9 @@ def child_node(n, t, q, parents):
assert False


import warnings


def generate(
seed: int,
*,
Expand All @@ -927,7 +930,11 @@ def generate(
root_node: str = "Module",
) -> str:
generator = AstGenerator(seed, depth_limit=depth_limit, node_limit=node_limit)
tree = generator.generate(root_node)

with warnings.catch_warnings():
warnings.simplefilter("ignore", SyntaxWarning)
tree = generator.generate(root_node)

ast.fix_missing_locations(tree)
return unparse(tree)

Expand Down

0 comments on commit 7d23df5

Please sign in to comment.