diff --git a/Lib/subprocess.py b/Lib/subprocess.py index 2b785496e4f..8203aded7d5 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -1003,7 +1003,7 @@ def __init__(self, args, bufsize=-1, executable=None, def __repr__(self): obj_repr = ( f"<{self.__class__.__name__}: " - f"returncode: {self.returncode} args: {list(self.args)!r}>" + f"returncode: {self.returncode} args: {self.args!r}>" ) if len(obj_repr) > 80: obj_repr = obj_repr[:76] + "...>" diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py index 70b54f4155a..7c79365f411 100644 --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -23,6 +23,7 @@ import gc import textwrap import json +import pathlib from test.support.os_helper import FakePath try: @@ -1442,28 +1443,23 @@ def test_communicate_epipe(self): 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 = "') - ) - - 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, ""), + ('a' * 100, True, 0, + ""), + (["ls"], False, None, ""), + (["ls", '--my-opts', 'a' * 100], False, None, + ""), + (path_cmd, False, 7, f"") + ] + 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 diff --git a/Misc/NEWS.d/next/Library/2021-04-12-00-00-00.bpo-43776.p14y7a.rst b/Misc/NEWS.d/next/Library/2021-04-12-00-00-00.bpo-43776.p14y7a.rst new file mode 100644 index 00000000000..51bc791f10d --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-04-12-00-00-00.bpo-43776.p14y7a.rst @@ -0,0 +1 @@ +When :class:`subprocess.Popen` args are provided as a string or as :class:`pathlib.Path`, the Popen instance repr now shows the right thing. \ No newline at end of file