summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@redhat.com>2015-04-20 11:20:02 +0200
committerSergey Shepelev <temotor@gmail.com>2020-10-19 03:09:45 +0300
commit879f23a04269a5a6c115818680f9aace11574261 (patch)
treeb323cb1c8324a75325d0ed2c2f6956bb9e37b0ed
parentf2ebbb87590d0fa4f173599806dfec88caeb9fb9 (diff)
downloadeventlet-230-patcher-original.tar.gz
Issue #230: Fix patcher.original()230-patcher-original
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 a578637..517dd8c 100644
--- a/eventlet/patcher.py
+++ b/eventlet/patcher.py
@@ -182,6 +182,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 f1ef1f9..2053f7b 100644
--- a/tests/patcher_test.py
+++ b/tests/patcher_test.py
@@ -493,6 +493,11 @@ 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')