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.

Source Distributions

pyjack-0.3.2.zip (9.0 kB view hashes)

Uploaded Source

pyjack-0.3.2.tar.gz (5.3 kB view hashes)

Uploaded Source

Built Distributions

pyjack-0.3.2.win-amd64-py2.7.exe (233.4 kB view hashes)

Uploaded Source

pyjack-0.3.2.win-amd64-py2.6.exe (233.4 kB view hashes)

Uploaded Source

pyjack-0.3.2.win32-py2.7.exe (205.8 kB view hashes)

Uploaded Source

pyjack-0.3.2.win32-py2.6.exe (75.7 kB view hashes)

Uploaded Source

pyjack-0.3.2.win32-py2.5.exe (71.1 kB view hashes)

Uploaded Source

pyjack-0.3.2.win32-py2.4.exe (71.1 kB view hashes)

Uploaded Source

pyjack-0.3.2-py2.7.egg (9.1 kB view hashes)

Uploaded Source

pyjack-0.3.2-py2.6.egg (9.1 kB view hashes)

Uploaded Source

pyjack-0.3.2-py2.5.egg (9.1 kB view hashes)

Uploaded Source

pyjack-0.3.2-py2.4.egg (9.2 kB view hashes)

Uploaded Source

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