8000 subprocess_shell() and subprocess_exec() methods of BaseEventLoop now… · python/asyncio@9176f5f · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Nov 23, 2017. It is now read-only.

Commit 9176f5f

Browse files
committed
subprocess_shell() and subprocess_exec() methods of BaseEventLoop now raises a
ValueError instead of raising an AssertionError. Moreover, bufsize different than 0 is now considered as an error.
1 parent 0860f3c commit 9176f5f

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

asyncio/base_events.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -552,9 +552,14 @@ def subprocess_shell(self, protocol_factory, cmd, *, stdin=subprocess.PIPE,
552552
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
553553
universal_newlines=False, shell=True, bufsize=0,
554554
**kwargs):
555-
assert not universal_newlines, "universal_newlines must be False"
556-
assert shell, "shell must be True"
557-
assert isinstance(cmd, str), cmd
555+
if not isinstance(cmd, str):
556+
raise ValueError("cmd must be a string")
557+
if universal_newlines:
558+
raise ValueError("universal_newlines must be False")
559+
if not shell:
560+
raise ValueError("shell must be False")
561+
if bufsize != 0:
562+
raise ValueError("bufsize must be 0")
558563
protocol = protocol_factory()
559564
transport = yield from self._make_subprocess_transport(
560565
protocol, cmd, True, stdin, stdout, stderr, bufsize, **kwargs)
@@ -565,8 +570,12 @@ def subprocess_exec(self, protocol_factory, *args, stdin=subprocess.PIPE,
565570
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
566571
universal_newlines=False, shell=False, bufsize=0,
567572
**kwargs):
568-
assert not universal_newlines, "universal_newlines must be False"
569-
assert not shell, "shell must be False"
573+
if universal_newlines:
574+
raise ValueError("universal_newlines must be False")
575+
if shell:
576+
raise ValueError("shell must be False")
577+
if bufsize != 0:
578+
raise ValueError("bufsize must be 0")
570579
protocol = protocol_factory()
571580
transport = yield from self._make_subprocess_transport(
572581
protocol, args, False, stdin, stdout, stderr, bufsize, **kwargs)

tests/test_events.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,6 +1491,38 @@ def connect():
14911491
self.loop.run_until_complete(proto.completed)
14921492
self.assertEqual(7, proto.returncode)
14931493

1494+
def test_subprocess_exec_invalid_args(self):
1495+
@asyncio.coroutine
1496+
def connect(**kwds):
1497+
yield from self.loop.subprocess_exec(
1498+
asyncio.SubprocessProtocol,
1499+
'pwd', **kwds)
1500+
1501+
with self.assertRaises(ValueError):
1502+
self.loop.run_until_complete(connect(universal_newlines=True))
1503+
with self.assertRaises(ValueError):
1504+
self.loop.run_until_complete(connect(bufsize=4096))
1505+
with self.assertRaises(ValueError):
1506+
self.loop.run_until_complete(connect(shell=True))
1507+
1508+
def test_subprocess_shell_invalid_args(self):
1509+
@asyncio.coroutine
1510+
def connect(cmd=None, **kwds):
1511+
if not cmd:
1512+
cmd = 'pwd'
1513+
yield from self.loop.subprocess_shell(
1514+
asyncio.SubprocessProtocol,
1515+
cmd, **kwds)
1516+
1517+
with self.assertRaises(ValueError):
1518+
self.loop.run_until_complete(connect(['ls', '-l']))
1519+
with self.assertRaises(ValueError):
1520+
self.loop.run_until_complete(connect(universal_newlines=True))
1521+
with self.assertRaises(ValueError):
1522+
self.loop.run_until_complete(connect(bufsize=4096))
1523+
with self.assertRaises(ValueError):
1524+
self.loop.run_until_complete(connect(shell=False))
1525+
14941526

14951527
if sys.platform == 'win32':
14961528

0 commit comments

Comments
 (0)
0