GitHub - 15r10nk/pysource-codegen: generate random python code to test linter/formatter/and other tools (original) (raw)

pypi version Python Versions PyPI - Downloads GitHub Sponsors

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:

pip install pysource-codegen

Usage:

The tool can be used over the CLI:

pysource-codegen --seed 42

or as a library:

from pysource_codegen import generate

seed = 42 print(generate(seed))

You might find 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.

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

cpython

3.12

3.13

Todo: