[Python-checkins] cpython: Issue #26870: Close pty master in case of exception

martin.panter python-checkins at python.org
Sat May 14 23:07:52 EDT 2016


https://hg.python.org/cpython/rev/27a49daf7925
changeset:   101338:27a49daf7925
user:        Martin Panter <vadmium+py at gmail.com>
date:        Sun May 15 03:05:36 2016 +0000
summary:
  Issue #26870: Close pty master in case of exception

files:
  Lib/test/test_readline.py |  7 +++++--
  1 files changed, 5 insertions(+), 2 deletions(-)


diff --git a/Lib/test/test_readline.py b/Lib/test/test_readline.py
--- a/Lib/test/test_readline.py
+++ b/Lib/test/test_readline.py
@@ -1,6 +1,7 @@
 """
 Very minimal unittests for parts of the readline module.
 """
+from contextlib import ExitStack
 from errno import EIO
 import os
 import selectors
@@ -123,7 +124,10 @@
     args = (sys.executable, '-c', script)
     proc = subprocess.Popen(args, stdin=slave, stdout=slave, stderr=slave)
     os.close(slave)
-    with proc, selectors.DefaultSelector() as sel:
+    with ExitStack() as cleanup:
+        cleanup.enter_context(proc)
+        cleanup.callback(os.close, master)
+        sel = cleanup.enter_context(selectors.DefaultSelector())
         sel.register(master, selectors.EVENT_READ | selectors.EVENT_WRITE)
         os.set_blocking(master, False)
         while True:
@@ -137,7 +141,6 @@
                             raise
                         chunk = b""
                     if not chunk:
-                        os.close(master)
                         return output
                     output.extend(chunk)
                 if events & selectors.EVENT_WRITE:

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


More information about the Python-checkins mailing list