diff --git a/eventlet/patcher.py b/eventlet/patcher.py index 68e21bb..d7f911e 100644 --- a/eventlet/patcher.py +++ b/eventlet/patcher.py @@ -5,6 +5,14 @@ __exclude = set(('__builtins__', '__file__', '__name__')) def inject(module_name, new_globals, *additional_modules): + if not additional_modules: + # supply some defaults + additional_modules = ( + _green_os_modules() + + _green_select_modules() + + _green_socket_modules() + + _green_thread_modules()) + ## Put the specified modules in sys.modules for the duration of the import saved = {} for name, mod in additional_modules: @@ -47,6 +55,7 @@ def import_patched(module_name, *additional_modules, **kw_additional_modules): None, *additional_modules + tuple(kw_additional_modules.items())) + def patch_function(func, *additional_modules): """Huge hack here -- patches the specified modules for the duration of the function call.""" @@ -66,3 +75,38 @@ def patch_function(func, *additional_modules): del sys.modules[name] return patched + +def monkey_patch(os=True, select=True, socket=True, thread=True): + modules_to_patch = [] + if os: + modules_to_patch += _green_os_modules() + if select: + modules_to_patch += _green_select_modules() + if socket: + modules_to_patch += _green_socket_modules() + if thread: + modules_to_patch += _green_thread_modules() + for name, mod in modules_to_patch: + sys.modules[name] = mod + +def _green_os_modules(): + from eventlet.green import os + return [('os', os)] + +def _green_select_modules(): + from eventlet.green import select + return [('select', select)] + +def _green_socket_modules(): + from eventlet.green import socket + try: + from eventlet.green import ssl + return [('socket', socket), ('ssl', ssl)] + except ImportError: + return [('socket', socket)] + +def _green_thread_modules(): + from eventlet.green import Queue + from eventlet.green import thread + return [('Queue', Queue), ('thread', thread)] + \ No newline at end of file diff --git a/tests/patcher_test.py b/tests/patcher_test.py index aef13e0..060cf49 100644 --- a/tests/patcher_test.py +++ b/tests/patcher_test.py @@ -63,4 +63,46 @@ class Patcher(LimitedTestCase): self.assert_('eventlet.green.urllib' in lines[1]) self.assert_('eventlet.green.socket' in lines[2]) self.assert_('eventlet.green.urllib' in lines[2]) - self.assert_('eventlet.green.httplib' not in lines[2]) \ No newline at end of file + self.assert_('eventlet.green.httplib' not in lines[2]) + + def test_import_patched_defaults(self): + self.write_to_tempfile("base", base_module_contents) + new_mod = """ +from eventlet import patcher +base = patcher.import_patched('base') +print "newmod", base, base.socket, base.urllib.socket.socket +""" + self.write_to_tempfile("newmod", new_mod) + python_path = os.pathsep.join(sys.path + [self.tempdir]) + new_env = os.environ.copy() + new_env['PYTHONPATH'] = python_path + p = subprocess.Popen([sys.executable, + os.path.join(self.tempdir, "newmod.py")], + stdout=subprocess.PIPE, env=new_env) + output = p.communicate() + lines = output[0].split("\n") + self.assert_(lines[0].startswith('base')) + self.assert_(lines[1].startswith('newmod')) + self.assert_('eventlet.green.socket' in lines[1]) + self.assert_('GreenSocket' in lines[1]) + + def test_monkey_patch(self): + new_mod = """ +from eventlet import patcher +patcher.monkey_patch() +import socket +import urllib +print "newmod", socket, urllib.socket.socket +""" + self.write_to_tempfile("newmod", new_mod) + python_path = os.pathsep.join(sys.path + [self.tempdir]) + new_env = os.environ.copy() + new_env['PYTHONPATH'] = python_path + p = subprocess.Popen([sys.executable, + os.path.join(self.tempdir, "newmod.py")], + stdout=subprocess.PIPE, env=new_env) + output = p.communicate() + lines = output[0].split("\n") + self.assert_(lines[0].startswith('newmod')) + self.assert_('eventlet.green.socket' in lines[0]) + self.assert_('GreenSocket' in lines[0])