https://github.com/15r10nk/pysource-codegen
generate random python code to test linter/formatter/and other tools
https://github.com/15r10nk/pysource-codegen
fuzzing python testing
Last synced: about 1 year ago
JSON representation
generate random python code to test linter/formatter/and other tools
- Host: GitHub
- URL: https://github.com/15r10nk/pysource-codegen
- Owner: 15r10nk
- License: mit
- Created: 2023-04-13T21:19:18.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-10-28T23:32:09.000Z (over 1 year ago)
- Last Synced: 2024-10-29T20:55:38.468Z (over 1 year ago)
- Topics: fuzzing, python, testing
- Language: Python
- Homepage:
- Size: 228 KB
- Stars: 38
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
[](https://pypi.org/project/pysource-codegen/)


[](https://github.com/sponsors/15r10nk)
# Introduction
`pysource_codegen` is able to generate random python code which can be compiled.
The compiled code should not be executed.
This is still a very early version, but it does its job.
It is general useful to test tools like formatters, linters or anything which operates on python code.
## Install:
``` bash
pip install pysource-codegen
```
## Usage:
The tool can be used over the CLI:
``` bash
pysource-codegen --seed 42
```
or as a library:
``` python
from pysource_codegen import generate
seed = 42
print(generate(seed))
```
You might find [pysource-minimize](https://github.com/15r10nk/pysource-minimize) also useful
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
#### 3.12
* 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
* https://github.com/python/cpython/issues/109889
#### 3.13
* https://github.com/python/cpython/issues/120367
* https://github.com/python/cpython/issues/120225
* https://github.com/python/cpython/issues/124746
* https://github.com/python/cpython/issues/124871
## Todo:
* [ ] refactor the existing code
* add better context information to `probability()`
* remove `fix()` function and move code into `probability()`
* [ ] 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