[Python-checkins] cpython (3.4): Issue #23353, asyncio: Workaround CPython bug #23353

victor.stinner python-checkins at python.org
Mon Feb 2 18:38:13 CET 2015


https://hg.python.org/cpython/rev/2cd6621a9fbc
changeset:   94464:2cd6621a9fbc
branch:      3.4
parent:      94461:7d1c32ddc432
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Mon Feb 02 18:36:31 2015 +0100
summary:
  Issue #23353, asyncio: Workaround CPython bug #23353

Don't use yield/yield-from in an except block of a generator. Store the
exception and handle it outside the except block.

files:
  Lib/asyncio/test_utils.py     |   4 ++++
  Lib/asyncio/unix_events.py    |  12 ++++++++++--
  Lib/asyncio/windows_events.py |  11 +++++++++--
  3 files changed, 23 insertions(+), 4 deletions(-)


diff --git a/Lib/asyncio/test_utils.py b/Lib/asyncio/test_utils.py
--- a/Lib/asyncio/test_utils.py
+++ b/Lib/asyncio/test_utils.py
@@ -416,6 +416,10 @@
     def tearDown(self):
         events.set_event_loop(None)
 
+        # Detect CPython bug #23353: ensure that yield/yield-from is not used
+        # in an except block of a generator
+        self.assertEqual(sys.exc_info(), (None, None, None))
+
 
 @contextlib.contextmanager
 def disable_logger():
diff --git a/Lib/asyncio/unix_events.py b/Lib/asyncio/unix_events.py
--- a/Lib/asyncio/unix_events.py
+++ b/Lib/asyncio/unix_events.py
@@ -186,10 +186,18 @@
                                       self._child_watcher_callback, transp)
             try:
                 yield from waiter
-            except:
+            except Exception as exc:
+                # Workaround CPython bug #23353: using yield/yield-from in an
+                # except block of a generator doesn't clear properly
+                # sys.exc_info()
+                err = exc
+            else:
+                err = None
+
+            if err is not None:
                 transp.close()
                 yield from transp._wait()
-                raise
+                raise err
 
         return transp
 
diff --git a/Lib/asyncio/windows_events.py b/Lib/asyncio/windows_events.py
--- a/Lib/asyncio/windows_events.py
+++ b/Lib/asyncio/windows_events.py
@@ -373,10 +373,17 @@
                                              **kwargs)
         try:
             yield from waiter
-        except:
+        except Exception as exc:
+            # Workaround CPython bug #23353: using yield/yield-from in an
+            # except block of a generator doesn't clear properly sys.exc_info()
+            err = exc
+        else:
+            err = None
+
+        if err is not None:
             transp.close()
             yield from transp._wait()
-            raise
+            raise err
 
         return transp
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list