Skip to main content

Tools to reversibly replace functions / objects with proxy functions / objects for debug, testing, monkey-patching.

Project description

pyjack is a debug/test/monkey-patching toolset that allows you to reversibly replace all references to a function or object in memory with a proxy function or object. pyjack has two major functions:

  • function “connect” can connect a ‘proxy’ function to almost any python function/method. This proxy function is called instead of the original function. However, the original function is passed to the proxy function along with all args, kwargs so you can do things like:

    • Modify the args, kwargs first, print a debug message, then call the original function
    • Not call the function, rather just log it and print a debug message

    etc. etc. – it’s all up to you.

  • function “replace_all_refs” can be used to replace all references to a object with references to another object. This replaces all references in the _entire_ memory space. You get one final reference to the original reference, so it is possible to call this function again to restore the memory state back to it’s original state.

“connect” function

>>> import pyjack
>>>
>>> def fakeimport(orgopen, *args, **kwargs):
...     print 'Trying to import %s' % args[0]
...     return 'MODULE_%s' % args[0]
...
>>> pyjack.connect(__import__, proxyfn=fakeimport)
<..._PyjackFuncBuiltin object at 0x...>
>>>
>>> import time
Trying to import time
>>> print time
MODULE_time
>>>
>>> __import__.restore()
>>>
>>> import time
>>> print time
<module 'time' (built-in)>

“replace_all_refs” function

Setup an ‘item’ reference and put it in a few places:

>>> item = (100, 'one hundred')
>>> data = {item: True, 'itemdata': item}
>>>
>>> class Foobar(object):
...     the_item = item
...
>>> def outer(datum):
...     def inner():
...         return ("Here is the datum:", datum,)
...
...     return inner
...
>>> inner = outer(item)
>>>
>>> print item
(100, 'one hundred')
>>> print data
{'itemdata': (100, 'one hundred'), (100, 'one hundred'): True}
>>> print Foobar.the_item
(100, 'one hundred')
>>> print inner()
('Here is the datum:', (100, 'one hundred'))

Then replace them:

>>> new = (101, 'one hundred and one')
>>> org_item = pyjack.replace_all_refs(item, new)
>>>
>>> print item
(101, 'one hundred and one')
>>> print data
{'itemdata': (101, 'one hundred and one'), (101, 'one hundred and one'): True}
>>> print Foobar.the_item
(101, 'one hundred and one')
>>> print inner()
('Here is the datum:', (101, 'one hundred and one'))

But you still have one final reference to the org data:

>>> print org_item
(100, 'one hundred')

So the process is reversible:

>>> new = pyjack.replace_all_refs(new, org_item)
>>>
>>> print item
(100, 'one hundred')
>>> print data
{'itemdata': (100, 'one hundred'), (100, 'one hundred'): True}
>>> print Foobar.the_item
(100, 'one hundred')
>>> print inner()
('Here is the datum:', (100, 'one hundred'))

Other References

For full documentation and several examples please visit:

The git repo is here:

Project details


Download files

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

Filename, size & hash SHA256 hash help File type Python version Upload date
pyjack-0.3.2-py2.4.egg (9.2 kB) Copy SHA256 hash SHA256 Egg 2.4 Mar 15, 2011
pyjack-0.3.2-py2.5.egg (9.1 kB) Copy SHA256 hash SHA256 Egg 2.5 Mar 15, 2011
pyjack-0.3.2-py2.6.egg (9.1 kB) Copy SHA256 hash SHA256 Egg 2.6 Mar 15, 2011
pyjack-0.3.2-py2.7.egg (9.1 kB) Copy SHA256 hash SHA256 Egg 2.7 Mar 15, 2011
pyjack-0.3.2.tar.gz (5.3 kB) Copy SHA256 hash SHA256 Source None Mar 15, 2011
pyjack-0.3.2.win32-py2.4.exe (71.1 kB) Copy SHA256 hash SHA256 Windows Installer 2.4 Mar 15, 2011
pyjack-0.3.2.win32-py2.5.exe (71.1 kB) Copy SHA256 hash SHA256 Windows Installer 2.5 Mar 15, 2011
pyjack-0.3.2.win32-py2.6.exe (75.7 kB) Copy SHA256 hash SHA256 Windows Installer 2.6 Mar 15, 2011
pyjack-0.3.2.win32-py2.7.exe (205.8 kB) Copy SHA256 hash SHA256 Windows Installer 2.7 Mar 15, 2011
pyjack-0.3.2.win-amd64-py2.6.exe (233.4 kB) Copy SHA256 hash SHA256 Windows Installer 2.6 Mar 15, 2011
pyjack-0.3.2.win-amd64-py2.7.exe (233.4 kB) Copy SHA256 hash SHA256 Windows Installer 2.7 Mar 15, 2011
pyjack-0.3.2.zip (9.0 kB) Copy SHA256 hash SHA256 Source None Mar 15, 2011

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page