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

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.3.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: pysource_codegen-0.4.3.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.0 Linux/6.1.0-13-amd64

File hashes

Hashes for pysource_codegen-0.4.3.tar.gz
Algorithm Hash digest
SHA256 76ee4c9e0ec6421d8112ae314a621a7b5ddafa4e030bc0900fedbe043b89971a
MD5 d00f997c3367ad61e2f6f97e4de7ad0a
BLAKE2b-256 5529f80cc35bcae152616217a891db4a12f54b6208125fd36c563586b826ea95

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pysource_codegen-0.4.3-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/6.1.0-13-amd64

File hashes

Hashes for pysource_codegen-0.4.3-py3-none-any.whl
Algorithm Hash digest
SHA256 549c530fb1efdf9669eb155eb6ba8560b3cfe31e2f2cb70c118ab07fccf1a3b4
MD5 ba16489a01c3f4e5ed777bc3d255c8af
BLAKE2b-256 3e876e7d5dd259e5d777b031790ed5c87dac2bf08266f6299a2fc016b75933b2

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