mirror of
https://github.com/python/cpython.git
synced 2025-08-01 15:43:13 +00:00
[3.9] bpo-43776: Remove list call from args in Popen repr (GH-25338) (GH-26510)
Removes the `list` call in the Popen `repr`.
Current implementation:
For cmd = `python --version`, with `shell=True`.
```bash
<Popen: returncode: None args: ['p', 'y', 't', 'h', 'o', 'n', ' ', '-', '-',...>
```
For `shell=False` and args=`['python', '--version']`, the output is correct:
```bash
<Popen: returncode: None args: ['python', '--version']>
```
With the new changes the `repr` yields:
For cmd = `python --version`, with `shell=True`:
```bash
<Popen: returncode: None args: 'python --version'>
```
For `shell=False` and args=`['python', '--version']`, the output:
```bash
<Popen: returncode: None args: ['python', '--version']>
```
Automerge-Triggered-By: GH:gpshead.
(cherry picked from commit db0c5b786d
)
Co-authored-by: M. Kocher <michael.kocher@me.com>
Co-authored-by: M. Kocher <michael.kocher@me.com>
This commit is contained in:
parent
5df4abd6b0
commit
5a8ddcc452
3 changed files with 19 additions and 22 deletions
|
@ -20,6 +20,7 @@ import threading
|
|||
import gc
|
||||
import textwrap
|
||||
import json
|
||||
import pathlib
|
||||
from test.support import FakePath
|
||||
|
||||
try:
|
||||
|
@ -1373,28 +1374,23 @@ class ProcessTestCase(BaseTestCase):
|
|||
p.communicate(b"x" * 2**20)
|
||||
|
||||
def test_repr(self):
|
||||
# Run a command that waits for user input, to check the repr() of
|
||||
# a Proc object while and after the sub-process runs.
|
||||
code = 'import sys; input(); sys.exit(57)'
|
||||
cmd = [sys.executable, '-c', code]
|
||||
result = "<Popen: returncode: {}"
|
||||
path_cmd = pathlib.Path("my-tool.py")
|
||||
pathlib_cls = path_cmd.__class__.__name__
|
||||
|
||||
with subprocess.Popen(
|
||||
cmd, stdin=subprocess.PIPE, universal_newlines=True) as proc:
|
||||
self.assertIsNone(proc.returncode)
|
||||
self.assertTrue(
|
||||
repr(proc).startswith(result.format(proc.returncode)) and
|
||||
repr(proc).endswith('>')
|
||||
)
|
||||
|
||||
proc.communicate(input='exit...\n')
|
||||
proc.wait()
|
||||
|
||||
self.assertIsNotNone(proc.returncode)
|
||||
self.assertTrue(
|
||||
repr(proc).startswith(result.format(proc.returncode)) and
|
||||
repr(proc).endswith('>')
|
||||
)
|
||||
cases = [
|
||||
("ls", True, 123, "<Popen: returncode: 123 args: 'ls'>"),
|
||||
('a' * 100, True, 0,
|
||||
"<Popen: returncode: 0 args: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...>"),
|
||||
(["ls"], False, None, "<Popen: returncode: None args: ['ls']>"),
|
||||
(["ls", '--my-opts', 'a' * 100], False, None,
|
||||
"<Popen: returncode: None args: ['ls', '--my-opts', 'aaaaaaaaaaaaaaaaaaaaaaaa...>"),
|
||||
(path_cmd, False, 7, f"<Popen: returncode: 7 args: {pathlib_cls}('my-tool.py')>")
|
||||
]
|
||||
with unittest.mock.patch.object(subprocess.Popen, '_execute_child'):
|
||||
for cmd, shell, code, sx in cases:
|
||||
p = subprocess.Popen(cmd, shell=shell)
|
||||
p.returncode = code
|
||||
self.assertEqual(repr(p), sx)
|
||||
|
||||
def test_communicate_epipe_only_stdin(self):
|
||||
# Issue 10963: communicate() should hide EPIPE
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue