1
0
python-delegator.py/merged_pr_62.patch

158 lines
5.4 KiB
Diff

From 9bb7790d6ad54a238f57cce8ad93a811ef302268 Mon Sep 17 00:00:00 2001
From: Dan Ryan <dan@danryan.co>
Date: Mon, 22 Oct 2018 09:42:40 -0400
Subject: [PATCH 1/3] Explicitly close file handles on block
- Fixes #61
Signed-off-by: Dan Ryan <dan@danryan.co>
---
delegator.py | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/delegator.py b/delegator.py
index d15aeb9..9eb430d 100644
--- a/delegator.py
+++ b/delegator.py
@@ -233,6 +233,8 @@ def kill(self):
def block(self):
"""Blocks until process is complete."""
if self._uses_subprocess:
+ # Close open file handles to prevent leaking them
+ self.subprocess.stdout.close()
# consume stdout and stderr
try:
stdout, stderr = self.subprocess.communicate()
@@ -241,6 +243,8 @@ def block(self):
except ValueError:
pass # Don't read from finished subprocesses.
else:
+ self.subprocess.sendeof()
+ self.subprocess.proc.stdout.close()
self.subprocess.wait()
def pipe(self, command, timeout=None, cwd=None):
From eddd9d14e50f7945faf99cac92ef33ab18b1342b Mon Sep 17 00:00:00 2001
From: Dan Ryan <dan@danryan.co>
Date: Mon, 22 Oct 2018 10:04:15 -0400
Subject: [PATCH 2/3] Don't pass `subprocess.PIPE` to blocking Popen calls
Signed-off-by: Dan Ryan <dan@danryan.co>
---
delegator.py | 23 ++++++++++++++---------
1 file changed, 14 insertions(+), 9 deletions(-)
diff --git a/delegator.py b/delegator.py
index 9eb430d..3ffb2e3 100644
--- a/delegator.py
+++ b/delegator.py
@@ -178,6 +178,7 @@ def run(self, block=True, binary=False, cwd=None, env=None):
# Use subprocess.
if self.blocking:
popen_kwargs = self._default_popen_kwargs.copy()
+ del popen_kwargs["stdin"]
popen_kwargs["universal_newlines"] = not binary
if cwd:
popen_kwargs["cwd"] = cwd
@@ -233,19 +234,23 @@ def kill(self):
def block(self):
"""Blocks until process is complete."""
if self._uses_subprocess:
- # Close open file handles to prevent leaking them
- self.subprocess.stdout.close()
# consume stdout and stderr
- try:
- stdout, stderr = self.subprocess.communicate()
- self.__out = stdout
- self.__err = stderr
- except ValueError:
- pass # Don't read from finished subprocesses.
+ if self.blocking:
+ try:
+ stdout, stderr = self.subprocess.communicate()
+ self.__out = stdout
+ self.__err = stderr
+ except ValueError:
+ pass # Don't read from finished subprocesses.
+ else:
+ self.subprocess.stdin.close()
+ self.std_out.close()
+ self.std_err.close()
+ self.subprocess.wait()
else:
self.subprocess.sendeof()
- self.subprocess.proc.stdout.close()
self.subprocess.wait()
+ self.subprocess.proc.stdout.close()
def pipe(self, command, timeout=None, cwd=None):
"""Runs the current command and passes its output to the next
From d07d065ef3ae5f7b637214a9fe193a4c642126cb Mon Sep 17 00:00:00 2001
From: Dan Ryan <dan@danryan.co>
Date: Tue, 23 Oct 2018 18:53:27 -0400
Subject: [PATCH 3/3] Don't pass `EOF` to the caller
Signed-off-by: Dan Ryan <dan@danryan.co>
---
delegator.py | 17 ++++++++++++-----
1 file changed, 12 insertions(+), 5 deletions(-)
diff --git a/delegator.py b/delegator.py
index 3ffb2e3..56d1245 100644
--- a/delegator.py
+++ b/delegator.py
@@ -7,6 +7,8 @@
import errno
from pexpect.popen_spawn import PopenSpawn
+import pexpect
+pexpect.EOF.__module__ = "pexpect.exceptions"
# Include `unicode` in STR_TYPES for Python 2.X
try:
@@ -110,7 +112,7 @@ def _pexpect_out(self):
if self.subprocess.before:
result += self.subprocess.before
- if self.subprocess.after:
+ if self.subprocess.after and self.subprocess.after is not pexpect.EOF:
result += self.subprocess.after
result += self.subprocess.read()
@@ -206,7 +208,10 @@ def expect(self, pattern, timeout=-1):
if self.blocking:
raise RuntimeError("expect can only be used on non-blocking commands.")
- self.subprocess.expect(pattern=pattern, timeout=timeout)
+ try:
+ self.subprocess.expect(pattern=pattern, timeout=timeout)
+ except pexpect.EOF:
+ pass
def send(self, s, end=os.linesep, signal=False):
"""Sends the given string or signal to std_in."""
@@ -249,8 +254,11 @@ def block(self):
self.subprocess.wait()
else:
self.subprocess.sendeof()
- self.subprocess.wait()
- self.subprocess.proc.stdout.close()
+ try:
+ self.subprocess.wait()
+ finally:
+ if self.subprocess.proc.stdout:
+ self.subprocess.proc.stdout.close()
def pipe(self, command, timeout=None, cwd=None):
"""Runs the current command and passes its output to the next
@@ -272,7 +280,6 @@ def pipe(self, command, timeout=None, cwd=None):
c.run(block=False, cwd=cwd)
if data:
c.send(data)
- c.subprocess.sendeof()
c.block()
return c