diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2015-01-29 17:50:58 +0100 |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2015-01-29 17:50:58 +0100 |
commit | 978a9afc6af6c137065bdcf7ae4ef5450e5b2ec2 (patch) | |
tree | 54eb1a4cdf09be18b62c40e0e203a347c37c4cd6 /Lib/asyncio/base_subprocess.py | |
parent | 3c0cf05901ea5cca0694734fd4a64b2bc267cb41 (diff) | |
download | cpython-978a9afc6af6c137065bdcf7ae4ef5450e5b2ec2.tar.gz cpython-978a9afc6af6c137065bdcf7ae4ef5450e5b2ec2.zip |
Issue #23243, asyncio: Emit a ResourceWarning when an event loop or a transport
is not explicitly closed. Close also explicitly transports in test_sslproto.
Diffstat (limited to 'Lib/asyncio/base_subprocess.py')
-rw-r--r-- | Lib/asyncio/base_subprocess.py | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/Lib/asyncio/base_subprocess.py b/Lib/asyncio/base_subprocess.py index 81c6f1a71d3..651a9a291ee 100644 --- a/Lib/asyncio/base_subprocess.py +++ b/Lib/asyncio/base_subprocess.py @@ -1,5 +1,7 @@ import collections import subprocess +import sys +import warnings from . import protocols from . import transports @@ -13,6 +15,7 @@ class BaseSubprocessTransport(transports.SubprocessTransport): stdin, stdout, stderr, bufsize, extra=None, **kwargs): super().__init__(extra) + self._closed = False self._protocol = protocol self._loop = loop self._pid = None @@ -40,7 +43,10 @@ class BaseSubprocessTransport(transports.SubprocessTransport): program, self._pid) def __repr__(self): - info = [self.__class__.__name__, 'pid=%s' % self._pid] + info = [self.__class__.__name__] + if self._closed: + info.append('closed') + info.append('pid=%s' % self._pid) if self._returncode is not None: info.append('returncode=%s' % self._returncode) @@ -70,6 +76,7 @@ class BaseSubprocessTransport(transports.SubprocessTransport): raise NotImplementedError def close(self): + self._closed = True for proto in self._pipes.values(): if proto is None: continue @@ -77,6 +84,15 @@ class BaseSubprocessTransport(transports.SubprocessTransport): if self._returncode is None: self.terminate() + # On Python 3.3 and older, objects with a destructor part of a reference + # cycle are never destroyed. It's not more the case on Python 3.4 thanks + # to the PEP 442. + if sys.version_info >= (3, 4): + def __del__(self): + if not self._closed: + warnings.warn("unclosed transport %r" % self, ResourceWarning) + self.close() + def get_pid(self): return self._pid @@ -104,6 +120,7 @@ class BaseSubprocessTransport(transports.SubprocessTransport): Function called when an exception is raised during the creation of a subprocess. """ + self._closed = True if self._loop.get_debug(): logger.warning('Exception during subprocess creation, ' 'kill the subprocess %r', |