diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2014-01-26 23:58:14 +0100 |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2014-01-26 23:58:14 +0100 |
commit | 4012c6a89a485d3587807373a95aca16d1e51dcd (patch) | |
tree | e4942ff2928fa8e84da2158b2c198197b7da35a6 | |
parent | 2c5d0bf69d1cadab6fa9c266f19180facae9937c (diff) | |
download | trollius-4012c6a89a485d3587807373a95aca16d1e51dcd.tar.gz |
replace read/write_pipe_protocol attributes with classic methods
-rw-r--r-- | asyncio/base_subprocess.py | 76 | ||||
-rw-r--r-- | asyncio/subprocess_stream.py | 7 | ||||
-rw-r--r-- | asyncio/transports.py | 52 |
3 files changed, 69 insertions, 66 deletions
diff --git a/asyncio/base_subprocess.py b/asyncio/base_subprocess.py index 61e2d83..2f7f565 100644 --- a/asyncio/base_subprocess.py +++ b/asyncio/base_subprocess.py @@ -1,3 +1,5 @@ +__all__ = ['SubprocessTransport'] + import collections import subprocess @@ -11,7 +13,65 @@ STDOUT = 1 STDERR = 2 -class BaseSubprocessTransport(transports.SubprocessTransport): +class SubprocessTransport(transports.BaseTransport): + + def create_read_pipe_protocol(self, transport, fd): + return ReadSubprocessPipeProto(transport, fd) + + def create_write_pipe_protocol(self, transport, fd): + return WriteSubprocessPipeProto(transport, fd) + + def get_pid(self): + """Get subprocess id.""" + raise NotImplementedError + + def get_returncode(self): + """Get subprocess returncode. + + See also + http://docs.python.org/3/library/subprocess#subprocess.Popen.returncode + """ + raise NotImplementedError + + def get_pipe_transport(self, fd): + """Get transport for pipe with number fd.""" + raise NotImplementedError + + def send_signal(self, signal): + """Send signal to subprocess. + + See also: + docs.python.org/3/library/subprocess#subprocess.Popen.send_signal + """ + raise NotImplementedError + + def terminate(self): + """Stop the subprocess. + + Alias for close() method. + + On Posix OSs the method sends SIGTERM to the subprocess. + On Windows the Win32 API function TerminateProcess() + is called to stop the subprocess. + + See also: + http://docs.python.org/3/library/subprocess#subprocess.Popen.terminate + """ + raise NotImplementedError + + def kill(self): + """Kill the subprocess. + + On Posix OSs the function sends SIGKILL to the subprocess. + On Windows kill() is an alias for terminate(). + + See also: + http://docs.python.org/3/library/subprocess#subprocess.Popen.kill + """ + raise NotImplementedError + + +class BaseSubprocessTransport(SubprocessTransport): def __init__(self, loop, protocol, args, shell, stdin, stdout, stderr, bufsize, @@ -30,14 +90,6 @@ class BaseSubprocessTransport(transports.SubprocessTransport): self._pending_calls = collections.deque() self._finished = False self._returncode = None - if protocol.read_pipe_protocol is not None: - self._read_pipe_protocol = protocol.read_pipe_protocol - else: - self._read_pipe_protocol = ReadSubprocessPipeProto - if protocol.write_pipe_protocol is not None: - self._write_pipe_protocol = protocol.write_pipe_protocol - else: - self._write_pipe_protocol = WriteSubprocessPipeProto self._start(args=args, shell=shell, stdin=stdin, stdout=stdout, stderr=stderr, bufsize=bufsize, **kwargs) self._extra['subprocess'] = self._proc @@ -84,15 +136,15 @@ class BaseSubprocessTransport(transports.SubprocessTransport): loop = self._loop if proc.stdin is not None: yield from loop.connect_write_pipe( - lambda: self._write_pipe_protocol(self, STDIN), + lambda: self._protocol.create_write_pipe_protocol(self, STDIN), proc.stdin) if proc.stdout is not None: yield from loop.connect_read_pipe( - lambda: self._read_pipe_protocol(self, STDOUT), + lambda: self._protocol.create_read_pipe_protocol(self, STDOUT), proc.stdout) if proc.stderr is not None: yield from loop.connect_read_pipe( - lambda: self._read_pipe_protocol(self, STDERR), + lambda: self._protocol.create_read_pipe_protocol(self, STDERR), proc.stderr) if not self._pipes: self._try_connected() diff --git a/asyncio/subprocess_stream.py b/asyncio/subprocess_stream.py index ef86761..ec80330 100644 --- a/asyncio/subprocess_stream.py +++ b/asyncio/subprocess_stream.py @@ -131,8 +131,6 @@ class ReadSubprocessPipeStreamProto(base_subprocess.ReadSubprocessPipeProto): class SubprocessStreamProtocol(protocols.SubprocessProtocol): - write_pipe_protocol = WriteSubprocessPipeStreamProto - def __init__(self, limit=streams._DEFAULT_LIMIT): self._pipes = {} self.limit = limit @@ -142,11 +140,14 @@ class SubprocessStreamProtocol(protocols.SubprocessProtocol): self._waiters = [] self._transport = None - def read_pipe_protocol(self, transport, fd): + def create_read_pipe_protocol(self, transport, fd): protocol = ReadSubprocessPipeStreamProto(transport, fd, self.limit) self.pipe_connection_made(fd, protocol) return protocol + def create_write_pipe_protocol(self, transport, fd): + return WriteSubprocessPipeStreamProto(transport, fd) + def connection_made(self, transport): self._transport = transport diff --git a/asyncio/transports.py b/asyncio/transports.py index 67ae7fd..95a14e4 100644 --- a/asyncio/transports.py +++ b/asyncio/transports.py @@ -5,7 +5,7 @@ import sys _PY34 = sys.version_info >= (3, 4) __all__ = ['BaseTransport', 'ReadTransport', 'WriteTransport', - 'Transport', 'DatagramTransport', 'SubprocessTransport', + 'Transport', 'DatagramTransport', ] @@ -169,53 +169,3 @@ class DatagramTransport(BaseTransport): raise NotImplementedError -class SubprocessTransport(BaseTransport): - - def get_pid(self): - """Get subprocess id.""" - raise NotImplementedError - - def get_returncode(self): - """Get subprocess returncode. - - See also - http://docs.python.org/3/library/subprocess#subprocess.Popen.returncode - """ - raise NotImplementedError - - def get_pipe_transport(self, fd): - """Get transport for pipe with number fd.""" - raise NotImplementedError - - def send_signal(self, signal): - """Send signal to subprocess. - - See also: - docs.python.org/3/library/subprocess#subprocess.Popen.send_signal - """ - raise NotImplementedError - - def terminate(self): - """Stop the subprocess. - - Alias for close() method. - - On Posix OSs the method sends SIGTERM to the subprocess. - On Windows the Win32 API function TerminateProcess() - is called to stop the subprocess. - - See also: - http://docs.python.org/3/library/subprocess#subprocess.Popen.terminate - """ - raise NotImplementedError - - def kill(self): - """Kill the subprocess. - - On Posix OSs the function sends SIGKILL to the subprocess. - On Windows kill() is an alias for terminate(). - - See also: - http://docs.python.org/3/library/subprocess#subprocess.Popen.kill - """ - raise NotImplementedError |