From 585947920806f9d6714ec09dbdcd4c433a24ed02 Mon Sep 17 00:00:00 2001 From: Sergey Shepelev Date: Sun, 14 May 2017 13:31:47 +0300 Subject: [PATCH] green.subprocess: keep CalledProcessError identity; Thanks to Linbing@github https://github.com/eventlet/eventlet/issues/413 --- AUTHORS | 1 + eventlet/green/subprocess.py | 8 ++++++++ tests/isolated/subprocess_exception_identity.py | 14 ++++++++++++++ tests/subprocess_test.py | 6 ++++++ 4 files changed, 29 insertions(+) create mode 100644 tests/isolated/subprocess_exception_identity.py diff --git a/AUTHORS b/AUTHORS index c5b94a7..790998b 100644 --- a/AUTHORS +++ b/AUTHORS @@ -150,3 +150,4 @@ Thanks To * Yuichi Bando * Feng * Aayush Kasurde +* Linbing diff --git a/eventlet/green/subprocess.py b/eventlet/green/subprocess.py index d639cd5..66d1d91 100644 --- a/eventlet/green/subprocess.py +++ b/eventlet/green/subprocess.py @@ -18,6 +18,7 @@ if sys.version_info > (3, 4): patcher.inject('subprocess', globals(), *to_patch) subprocess_orig = patcher.original("subprocess") +subprocess_imported = sys.modules['subprocess'] mswindows = sys.platform == "win32" @@ -37,6 +38,8 @@ if getattr(subprocess_orig, 'TimeoutExpired', None) is None: def __str__(self): return ("Command '%s' timed out after %s seconds" % (self.cmd, self.timeout)) +else: + TimeoutExpired = subprocess_imported.TimeoutExpired # This is the meat of this module, the green version of Popen. @@ -133,3 +136,8 @@ if hasattr(subprocess_orig, 'check_output'): __patched__.append('check_output') check_output = patched_function(subprocess_orig.check_output) del patched_function + +# Keep exceptions identity. +# https://github.com/eventlet/eventlet/issues/413 +CalledProcessError = subprocess_imported.CalledProcessError +del subprocess_imported diff --git a/tests/isolated/subprocess_exception_identity.py b/tests/isolated/subprocess_exception_identity.py new file mode 100644 index 0000000..24fdadd --- /dev/null +++ b/tests/isolated/subprocess_exception_identity.py @@ -0,0 +1,14 @@ +__test__ = False + +if __name__ == '__main__': + import subprocess as original + from eventlet.green import subprocess as green + + cases = ( + 'CalledProcessError', + 'TimeoutExpired', + ) + for c in cases: + if hasattr(original, c): + assert getattr(green, c) is getattr(original, c), c + print('pass') diff --git a/tests/subprocess_test.py b/tests/subprocess_test.py index d18c623..9040919 100644 --- a/tests/subprocess_test.py +++ b/tests/subprocess_test.py @@ -93,3 +93,9 @@ def test_check_call_without_timeout_works(): stdout=subprocess.PIPE, stderr=subprocess.PIPE, ) + + +def test_exception_identity(): + # https://github.com/eventlet/eventlet/issues/413 + # green module must keep exceptions classes as stdlib version + tests.run_isolated('subprocess_exception_identity.py')