summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Shepelev <temotor@gmail.com>2017-10-17 22:05:39 +0300
committerSergey Shepelev <temotor@gmail.com>2017-10-17 22:07:26 +0300
commit0c416976201ffd7adc48a71f105ea4dc2312f6b9 (patch)
treeb144de31937ed93874f3e3a999f7ec47eae60c51
parent5b8f5f595624bdfb5f707707959977bb56864e0d (diff)
downloadeventlet-exc-id-442.tar.gz
green: keep urllib exceptions identityexc-id-442
https://github.com/eventlet/eventlet/issues/442 General advice - always import modules, not names. Good: import urllib ; except urllib.URLError Bad: from urllib import URLError
-rw-r--r--eventlet/green/urllib/__init__.py9
-rw-r--r--eventlet/green/urllib/error.py18
-rw-r--r--eventlet/green/urllib2.py17
-rw-r--r--tests/isolated/urllib_exception_identity.py22
-rw-r--r--tests/patcher_test.py4
5 files changed, 61 insertions, 9 deletions
diff --git a/eventlet/green/urllib/__init__.py b/eventlet/green/urllib/__init__.py
index 7cb4ea6..771e5de 100644
--- a/eventlet/green/urllib/__init__.py
+++ b/eventlet/green/urllib/__init__.py
@@ -20,7 +20,6 @@ if six.PY2:
except NameError:
patcher.inject('urllib.request', globals(), *to_patch)
-
# patch a bunch of things that have imports inside the
# function body; this is lame and hacky but I don't feel
# too bad because urllib is a hacky pile of junk that no
@@ -33,8 +32,8 @@ if six.PY2:
ftpwrapper.init = patcher.patch_function(ftpwrapper.init, ('ftplib', ftplib))
ftpwrapper.retrfile = patcher.patch_function(ftpwrapper.retrfile, ('ftplib', ftplib))
- del patcher
+del patcher
- # Run test program when run as a script
- if __name__ == '__main__':
- main()
+# Run test program when run as a script
+if __name__ == '__main__':
+ main()
diff --git a/eventlet/green/urllib/error.py b/eventlet/green/urllib/error.py
index 6913813..d074388 100644
--- a/eventlet/green/urllib/error.py
+++ b/eventlet/green/urllib/error.py
@@ -1,4 +1,18 @@
+import sys
from eventlet import patcher
from eventlet.green.urllib import response
-patcher.inject('urllib.error', globals(), ('urllib.response', response))
-del patcher
+_canonical_name = 'urllib.error'
+patcher.inject(_canonical_name, globals(), ('urllib.response', response))
+_MISSING = object()
+module_imported = sys.modules.get(_canonical_name, patcher.original(_canonical_name))
+_current_module = sys.modules[__name__]
+_keep_names = (
+ 'URLError',
+ 'HTTPError',
+ 'ContentTooShortError',
+)
+for k in _keep_names:
+ v = getattr(module_imported, k, _MISSING)
+ if v is not _MISSING:
+ setattr(_current_module, k, v)
+del _canonical_name, _current_module, _keep_names, k, v, module_imported, patcher, sys
diff --git a/eventlet/green/urllib2.py b/eventlet/green/urllib2.py
index c53ecbb..d7e3f64 100644
--- a/eventlet/green/urllib2.py
+++ b/eventlet/green/urllib2.py
@@ -1,3 +1,4 @@
+import sys
from eventlet import patcher
from eventlet.green import ftplib
from eventlet.green import httplib
@@ -6,8 +7,9 @@ from eventlet.green import ssl
from eventlet.green import time
from eventlet.green import urllib
+_canonical_name = 'urllib2'
patcher.inject(
- 'urllib2',
+ _canonical_name,
globals(),
('httplib', httplib),
('socket', socket),
@@ -17,4 +19,15 @@ patcher.inject(
FTPHandler.ftp_open = patcher.patch_function(FTPHandler.ftp_open, ('ftplib', ftplib))
-del patcher
+_MISSING = object()
+module_imported = sys.modules.get(_canonical_name, patcher.original(_canonical_name))
+_current_module = sys.modules[__name__]
+_keep_names = (
+ 'URLError',
+ 'HTTPError',
+)
+for k in _keep_names:
+ v = getattr(module_imported, k, _MISSING)
+ if v is not _MISSING:
+ setattr(_current_module, k, v)
+del _canonical_name, _current_module, _keep_names, k, v, module_imported, patcher, sys
diff --git a/tests/isolated/urllib_exception_identity.py b/tests/isolated/urllib_exception_identity.py
new file mode 100644
index 0000000..6d700b9
--- /dev/null
+++ b/tests/isolated/urllib_exception_identity.py
@@ -0,0 +1,22 @@
+__test__ = False
+
+if __name__ == '__main__':
+ import sys
+ if sys.version_info[0] == 2:
+ import urllib2 as original
+ from eventlet.green import urllib2 as green
+ elif sys.version_info[0] == 3:
+ from urllib import error as original
+ from eventlet.green.urllib import error as green
+ else:
+ raise Exception
+
+ cases = (
+ 'URLError',
+ 'HTTPError',
+ 'ContentTooShortError',
+ )
+ for c in cases:
+ if hasattr(original, c):
+ assert getattr(green, c) is getattr(original, c), c
+ print('pass')
diff --git a/tests/patcher_test.py b/tests/patcher_test.py
index ff59400..bda82b3 100644
--- a/tests/patcher_test.py
+++ b/tests/patcher_test.py
@@ -520,3 +520,7 @@ def test_blocking_select_methods_are_deleted():
def test_regular_file_readall():
tests.run_isolated('regular_file_readall.py')
+
+
+def test_urllib_exception_identity():
+ tests.run_isolated('urllib_exception_identity.py')