Skip to main content

Build Status Coverage Status

pymonkey

A tool for applying monkeypatches to python executables.

Installation

pip install pymonkey

Registering a patch

Make a module:

## mymod/pymonkey.py

# Be sure to not import anything at the module scope
# This ensures that import-hook patching will work better later


# This is your chance to do argument parsing before running the target
# executable.
# This will be called after all of the patch hooks have been registered.
def pymonkey_argparse(argv):
    # You'll be passed the arguments as a tuple.  Parse your specific arguments
    # and return your parsed state and the remaining arguments.
    # If you wish to forgo argparsing, simply `return None, argv`
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('--foo')
    return parser.parse_known_args(argv)


# This is your entry point.  It will be passed module objects after they have
# been imported.  Check the module name and then apply your patch (if
# applicable).
# This will be called as a post-import hook so you'll have a chance to modify
# the module before another module would import from it.
# The parsed state computed above will be passed as `args`
def pymonkey_patch(mod, args):
    # This callback will be called with *every* module that gets imported
    # Guard against the specific module you'd like to patch
    if mod.__name__ != 'module_to_patch':
        return

    # Apply your patches here to module_to_patch
    mod.foo = args.foo

And add the entrypoints to setup.py:

setup(
    ...,
    entry_points={
        'pymonkey': ['mymod = mymod.pymonkey:pymonkey_patch'],
        'pymonkey.argparse': ['mymod = mymod.pymonkey:pymonkey_argparse'],
    },
    ...
)

Commandline usage

Applying a single patch:

$ pymonkey mymod -- pip install simplejson

Applying all the patches available:

$ pymonkey --all -- pip install simplejson

Viewing the help

$ pymonkey --help

Making entry points with pymonkey

In a module:

## mymod_main.py
from pymonkey import make_entry_point

# The first argument is a list of pymonkey patches to apply
# The second argument is the entry point to run
main = make_entry_point(('mymod',), 'pip')

if __name__ == '__main__':
    exit(main())

In setup.py

setup(
    ...,
    entry_points={
        'console_scripts': ['pip-patched = mymod_main:main'],
        'pymonkey': ['mymod = mymod.pymonkey:pymonkey_patch'],
        'pymonkey.argparse': ['mymod = mymod.pymonkey:pymonkey_argparse'],
    },
    ...
)

Then instead of

$ pymonkey mymod -- pip ...

You can now do

$ pip-patched ...

Things using pymonkey

Release files for pymonkey 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pymonkey 0.3.1
File Size Uploaded
pymonkey-0.3.1.tar.gz 6.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pymonkey 0.3.1
File Interpreter ABI Platform
pymonkey-0.3.1-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 12.6 kB

Release files / pymonkey-0.3.1.tar.gz

Download URL pymonkey-0.3.1.tar.gz
Size 6.1 kB
Tags Source
SHA-256 checksum
How to use checksums
23dfc5a7e5c72c1735349691f3a13e69614e80c12de05b236ca5ab361007d0f1
BLAKE2b-256 checksum
How to use checksums
8181a893cd17c5196c7d42718f98a0cd5c009bdd7897e4766b363384b2ffc912
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.8

Release files / pymonkey-0.3.1-py2.py3-none-any.whl

Download URL pymonkey-0.3.1-py2.py3-none-any.whl
Size 6.5 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
59a4d58785cd6a5734cfcba6dba5eb11595e493be559a141441caaa0e209048d
BLAKE2b-256 checksum
How to use checksums
5122276152db3ee5852de9c127e25c9acd9afe3544e4affa9eea6f31babbe6ab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.8

Release history Release notifications | RSS feed

This release

0.3.1 This release

2 release files

0.3.0

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page