Improved patcher so as to not require make_original.
This commit is contained in:
@@ -98,21 +98,19 @@ def patch_function(func, *additional_modules):
|
|||||||
return patched
|
return patched
|
||||||
|
|
||||||
_originals = {}
|
_originals = {}
|
||||||
class DummyModule(object):
|
|
||||||
pass
|
|
||||||
def make_original(modname):
|
|
||||||
orig_mod = __import__(modname)
|
|
||||||
dummy_mod = DummyModule()
|
|
||||||
for attr in dir(orig_mod):
|
|
||||||
setattr(dummy_mod, attr, getattr(orig_mod, attr))
|
|
||||||
_originals[modname] = dummy_mod
|
|
||||||
|
|
||||||
def original(modname):
|
def original(modname):
|
||||||
mod = _originals.get(modname)
|
mod = _originals.get(modname)
|
||||||
if mod is None:
|
if mod is None:
|
||||||
make_original(modname)
|
# re-import the "pure" module and store it in the global _originals
|
||||||
mod = _originals.get(modname)
|
# dict; be sure to restore whatever module had that name already
|
||||||
return mod
|
current_mod = sys.modules.pop(modname, None)
|
||||||
|
try:
|
||||||
|
real_mod = __import__(modname, {}, {}, modname.split('.')[:-1])
|
||||||
|
_originals[modname] = real_mod
|
||||||
|
finally:
|
||||||
|
if current_mod is not None:
|
||||||
|
sys.modules[modname] = current_mod
|
||||||
|
return _originals.get(modname)
|
||||||
|
|
||||||
already_patched = {}
|
already_patched = {}
|
||||||
def monkey_patch(all=True, os=False, select=False,
|
def monkey_patch(all=True, os=False, select=False,
|
||||||
@@ -134,14 +132,12 @@ def monkey_patch(all=True, os=False, select=False,
|
|||||||
modules_to_patch += _green_os_modules()
|
modules_to_patch += _green_os_modules()
|
||||||
already_patched['os'] = True
|
already_patched['os'] = True
|
||||||
if all or select and not already_patched.get('select'):
|
if all or select and not already_patched.get('select'):
|
||||||
make_original('select')
|
|
||||||
modules_to_patch += _green_select_modules()
|
modules_to_patch += _green_select_modules()
|
||||||
already_patched['select'] = True
|
already_patched['select'] = True
|
||||||
if all or socket and not already_patched.get('socket'):
|
if all or socket and not already_patched.get('socket'):
|
||||||
modules_to_patch += _green_socket_modules()
|
modules_to_patch += _green_socket_modules()
|
||||||
already_patched['socket'] = True
|
already_patched['socket'] = True
|
||||||
if all or thread and not already_patched.get('thread'):
|
if all or thread and not already_patched.get('thread'):
|
||||||
make_original('threading')
|
|
||||||
# hacks ahead
|
# hacks ahead
|
||||||
threading = original('threading')
|
threading = original('threading')
|
||||||
import eventlet.green.threading as greenthreading
|
import eventlet.green.threading as greenthreading
|
||||||
@@ -149,7 +145,6 @@ def monkey_patch(all=True, os=False, select=False,
|
|||||||
modules_to_patch += _green_thread_modules()
|
modules_to_patch += _green_thread_modules()
|
||||||
already_patched['thread'] = True
|
already_patched['thread'] = True
|
||||||
if all or time and not already_patched.get('time'):
|
if all or time and not already_patched.get('time'):
|
||||||
make_original('time')
|
|
||||||
modules_to_patch += _green_time_modules()
|
modules_to_patch += _green_time_modules()
|
||||||
already_patched['time'] = True
|
already_patched['time'] = True
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user