diff options
author | Victor Stinner <vstinner@redhat.com> | 2015-07-31 17:44:49 +0200 |
---|---|---|
committer | Victor Stinner <vstinner@redhat.com> | 2015-07-31 17:48:25 +0200 |
commit | 5eac181ac595bdb58c7e97690c8882c6f5651ac9 (patch) | |
tree | 423b0eea510ee2c9936f6cdbdb756d8416258f4f | |
parent | ce41fba64d504647e3c2379094408acd16a38338 (diff) | |
download | trollius-git-5eac181ac595bdb58c7e97690c8882c6f5651ac9.tar.gz |
Fix ResourceWarning in BaseSubprocessTransport
Python issue #24763: Fix resource warnings when BaseSubprocessTransport
constructor fails, if subprocess.Popen raises an exception for example.
Patch written by Martin Richard, test written by me.
-rw-r--r-- | asyncio/base_subprocess.py | 9 | ||||
-rw-r--r-- | tests/test_subprocess.py | 15 |
2 files changed, 22 insertions, 2 deletions
diff --git a/asyncio/base_subprocess.py b/asyncio/base_subprocess.py index c1477b8..a6971b1 100644 --- a/asyncio/base_subprocess.py +++ b/asyncio/base_subprocess.py @@ -35,8 +35,13 @@ class BaseSubprocessTransport(transports.SubprocessTransport): self._pipes[2] = None # Create the child process: set the _proc attribute - self._start(args=args, shell=shell, stdin=stdin, stdout=stdout, - stderr=stderr, bufsize=bufsize, **kwargs) + try: + self._start(args=args, shell=shell, stdin=stdin, stdout=stdout, + stderr=stderr, bufsize=bufsize, **kwargs) + except: + self.close() + raise + self._pid = self._proc.pid self._extra['subprocess'] = self._proc diff --git a/tests/test_subprocess.py b/tests/test_subprocess.py index ea85e19..d138c26 100644 --- a/tests/test_subprocess.py +++ b/tests/test_subprocess.py @@ -1,6 +1,7 @@ import signal import sys import unittest +import warnings from unittest import mock import asyncio @@ -413,6 +414,20 @@ class SubprocessMixin: # the transport was not notified yet self.assertFalse(killed) + def test_popen_error(self): + # Issue #24763: check that the subprocess transport is closed + # when BaseSubprocessTransport fails + with mock.patch('subprocess.Popen') as popen: + exc = ZeroDivisionError + popen.side_effect = exc + + create = asyncio.create_subprocess_exec(sys.executable, '-c', + 'pass', loop=self.loop) + with warnings.catch_warnings(record=True) as warns: + with self.assertRaises(exc): + self.loop.run_until_complete(create) + self.assertEqual(warns, []) + if sys.platform != 'win32': # Unix |