Adding import locks around the places where we mess with sys.modules in patcher, at schmir's suggestion.
This commit is contained in:
2
AUTHORS
2
AUTHORS
@@ -59,6 +59,6 @@ Thanks To
|
|||||||
* Marcin Bachry, nice repro of a bug and good diagnosis leading to the fix
|
* Marcin Bachry, nice repro of a bug and good diagnosis leading to the fix
|
||||||
* David Ziegler, reporting issue #53
|
* David Ziegler, reporting issue #53
|
||||||
* Favo Yang, twisted hub patch
|
* Favo Yang, twisted hub patch
|
||||||
* Schmir, patch that fixes readline method with chunked encoding in wsgi.py
|
* Schmir, patch that fixes readline method with chunked encoding in wsgi.py, advice on patcher
|
||||||
* Slide, for open-sourcing gogreen
|
* Slide, for open-sourcing gogreen
|
||||||
* Holger Krekel, websocket example small fix
|
* Holger Krekel, websocket example small fix
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import sys
|
import sys
|
||||||
|
import imp
|
||||||
|
|
||||||
__all__ = ['inject', 'import_patched', 'monkey_patch', 'is_monkey_patched']
|
__all__ = ['inject', 'import_patched', 'monkey_patch', 'is_monkey_patched']
|
||||||
|
|
||||||
@@ -11,6 +11,7 @@ class SysModulesSaver(object):
|
|||||||
constructor."""
|
constructor."""
|
||||||
def __init__(self, module_names=()):
|
def __init__(self, module_names=()):
|
||||||
self._saved = {}
|
self._saved = {}
|
||||||
|
imp.acquire_lock()
|
||||||
self.save(*module_names)
|
self.save(*module_names)
|
||||||
|
|
||||||
def save(self, *module_names):
|
def save(self, *module_names):
|
||||||
@@ -22,6 +23,7 @@ class SysModulesSaver(object):
|
|||||||
"""Restores the modules that the saver knows about into
|
"""Restores the modules that the saver knows about into
|
||||||
sys.modules.
|
sys.modules.
|
||||||
"""
|
"""
|
||||||
|
try:
|
||||||
for modname, mod in self._saved.iteritems():
|
for modname, mod in self._saved.iteritems():
|
||||||
if mod is not None:
|
if mod is not None:
|
||||||
sys.modules[modname] = mod
|
sys.modules[modname] = mod
|
||||||
@@ -30,6 +32,8 @@ class SysModulesSaver(object):
|
|||||||
del sys.modules[modname]
|
del sys.modules[modname]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
|
finally:
|
||||||
|
imp.release_lock()
|
||||||
|
|
||||||
|
|
||||||
def inject(module_name, new_globals, *additional_modules):
|
def inject(module_name, new_globals, *additional_modules):
|
||||||
@@ -63,7 +67,7 @@ def inject(module_name, new_globals, *additional_modules):
|
|||||||
_green_time_modules())
|
_green_time_modules())
|
||||||
|
|
||||||
# after this we are gonna screw with sys.modules, so capture the
|
# after this we are gonna screw with sys.modules, so capture the
|
||||||
# state of all the modules we're going to mess with
|
# state of all the modules we're going to mess with, and lock
|
||||||
saver = SysModulesSaver([name for name, m in additional_modules])
|
saver = SysModulesSaver([name for name, m in additional_modules])
|
||||||
saver.save(module_name)
|
saver.save(module_name)
|
||||||
|
|
||||||
@@ -244,6 +248,8 @@ def monkey_patch(**on):
|
|||||||
# tell us whether or not we succeeded
|
# tell us whether or not we succeeded
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
imp.acquire_lock()
|
||||||
|
try:
|
||||||
for name, mod in modules_to_patch:
|
for name, mod in modules_to_patch:
|
||||||
orig_mod = sys.modules.get(name)
|
orig_mod = sys.modules.get(name)
|
||||||
if orig_mod is None:
|
if orig_mod is None:
|
||||||
@@ -256,7 +262,8 @@ def monkey_patch(**on):
|
|||||||
# hacks ahead; this is necessary to prevent a KeyError on program exit
|
# hacks ahead; this is necessary to prevent a KeyError on program exit
|
||||||
if patched_thread:
|
if patched_thread:
|
||||||
_patch_main_thread(sys.modules['threading'])
|
_patch_main_thread(sys.modules['threading'])
|
||||||
|
finally:
|
||||||
|
imp.release_lock()
|
||||||
|
|
||||||
def _patch_main_thread(mod):
|
def _patch_main_thread(mod):
|
||||||
"""This is some gnarly patching specific to the threading module;
|
"""This is some gnarly patching specific to the threading module;
|
||||||
|
|||||||
Reference in New Issue
Block a user