Skip to main content

No project description provided

Project description

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 no 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

Todo:

  • refactor the existing code
  • use probabilities for the ast-nodes from existing python code (use markov chains)
  • support older python versions
  • allow to customize the probabilities to generate code to test specific language features
  • hypothesis support

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pysource_codegen-0.4.2.tar.gz (13.2 kB view details)

Uploaded Source

Built Distribution

pysource_codegen-0.4.2-py3-none-any.whl (15.9 kB view details)

Uploaded Python 3

File details

Details for the file pysource_codegen-0.4.2.tar.gz.

File metadata

  • Download URL: pysource_codegen-0.4.2.tar.gz
  • Upload date:
  • Size: 13.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.0 Linux/5.10.0-25-amd64

File hashes

Hashes for pysource_codegen-0.4.2.tar.gz
Algorithm Hash digest
SHA256 623942215cf912461ed9dafb4c807d588ceaa21e4aafe7aa1652155b9434d2da
MD5 83bc8678384b7ef4d10dbb05e5342bb7
BLAKE2b-256 4f02f75b7ea54c013a8b782e2959e1e449dbb84b57fd640fbf9f9f02d42a038b

See more details on using hashes here.

File details

Details for the file pysource_codegen-0.4.2-py3-none-any.whl.

File metadata

  • Download URL: pysource_codegen-0.4.2-py3-none-any.whl
  • Upload date:
  • Size: 15.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.0 Linux/5.10.0-25-amd64

File hashes

Hashes for pysource_codegen-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ec533e73330e71463564442918838909f087ea5749602447b950f4800751fa49
MD5 18f13fd44443450ab208469bed13c570
BLAKE2b-256 46da296f81e34c1c5517b97d70cfff63bc5fa9a86d368e0ec89055b23c6811d9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page