I am facing a tricky problem in Python:
I need to import a Python module (say, module_A) developed by somebody else which imports and monkey-patches httplib.
and I also need to import selenium, which uses httplib and does not like the above patches
Since I cannot really modify either modules, I was wondering if it is possible to import module_A in a 'watertight compartment' of the memory (dunno if there's a more correct term), that is, in such a way that all modules used inside module_A are imported as completely different objects, even if they were imported somewhere else.
Thanks!
There is no such thing as a 'sandbox' for modules, no.
You can undo monkey patches to a module by reloading the module:
import httplib
import module_A
reload(httplib)
This'll re-import httplib, resetting any globals to their original definition. Additional globals set by module_A are preserved, but changed methods and classes are reverted to their original state.
The alternative would be for you to slot a mock module into sys.modules['httplib'] for module_A to patch, then removing it again from sys.modules and importing the real httplib module. But the httplib module itself can be that mock better than anything else.
Another idea could be to try and re-slot the patched httplib module under a different name in sys.modules after patching, and count on module_A holding references to the objects in that module:
import sys
import module_A
sys.modules['httplib_patched'] = sys.modules['httplib']
del sys.modules['httplib']
Now there is no 'httplib' key in sys.modules and a new import would get a fresh unpatched version.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With