summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@redhat.com>2015-04-20 11:20:02 +0200
committerSergey Shepelev <temotor@gmail.com>2015-04-20 21:02:27 +0300
commit5884072e4e752ee84f56aa968cf4bb6acc67353c (patch)
treef0b36943c5ba83f2a63a10722d6d34a1e3344179
parentf1dd17833f8eb28bcb87132d9b538ce930369550 (diff)
downloadeventlet-tm5.tar.gz
Issue #230: Fix patcher.original()tm5
Don't load a module twice when eventlet monkey-patched is not used.
-rw-r--r--eventlet/patcher.py5
-rw-r--r--tests/isolated/patcher_threading_original.py31
-rw-r--r--tests/patcher_test.py5
3 files changed, 41 insertions, 0 deletions
diff --git a/eventlet/patcher.py b/eventlet/patcher.py
index eb09f9a..8a20e2d 100644
--- a/eventlet/patcher.py
+++ b/eventlet/patcher.py
@@ -175,6 +175,11 @@ def original(modname):
if original_name in sys.modules:
return sys.modules.get(original_name)
+ if not already_patched and modname in sys.modules:
+ # https://github.com/eventlet/eventlet/issues/230 Don't load a module twice
+ # when eventlet monkey-patched is not used
+ return sys.modules[modname]
+
# re-import the "pure" module and store it in the global _originals
# dict; be sure to restore whatever module had that name already
saver = SysModulesSaver((modname,))
diff --git a/tests/isolated/patcher_threading_original.py b/tests/isolated/patcher_threading_original.py
new file mode 100644
index 0000000..42bb107
--- /dev/null
+++ b/tests/isolated/patcher_threading_original.py
@@ -0,0 +1,31 @@
+import sys
+import threading
+
+
+# no standard tests in this file, ignore
+__test__ = False
+
+
+class ImportEventlet(threading.Thread):
+ def __init__(self):
+ threading.Thread.__init__(self)
+ self.same_module = None
+
+ def run(self):
+ # https://github.com/eventlet/eventlet/issues/230
+ # Test importing eventlet for the first time in a thread:
+ # eventlet.patcher.original() must not reload threading.py twice.
+ mod1 = sys.modules['threading']
+ import eventlet.patcher
+ mod2 = eventlet.patcher.original('threading')
+ self.same_module = mod2 is mod1
+
+
+if __name__ == '__main__':
+ thread = ImportEventlet()
+ thread.start()
+ thread.join()
+ if thread.same_module:
+ print("pass")
+ else:
+ print("failed")
diff --git a/tests/patcher_test.py b/tests/patcher_test.py
index 2e458c5..1fe54fe 100644
--- a/tests/patcher_test.py
+++ b/tests/patcher_test.py
@@ -504,5 +504,10 @@ def test_threading_condition():
tests.run_isolated('patcher_threading_condition.py')
+def test_threading_original():
+ # https://github.com/eventlet/eventlet/issues/230 test for non-regression of threading+original()
+ tests.run_isolated('patcher_threading_original.py')
+
+
def test_threading_join():
tests.run_isolated('patcher_threading_join.py')