Revert "bpo-31961: subprocess now accepts path-like args (GH-4329)" (… · python/cpython@be50a7b

4 files changed

lines changed

Original file line numberDiff line numberDiff line change

@@ -339,12 +339,12 @@ functions.

339339

the class uses the Windows ``CreateProcess()`` function. The arguments to

340340

:class:`Popen` are as follows.

341341
342-

*args* should be a sequence of program arguments or else a single string or

343-

:term:`path-like object`. By default, the program to execute is the first

344-

item in *args* if *args* is a sequence. If *args* is a string, the

345-

interpretation is platform-dependent and described below. See the *shell*

346-

and *executable* arguments for additional differences from the default

347-

behavior. Unless otherwise stated, it is recommended to pass *args* as a sequence.

342+

*args* should be a sequence of program arguments or else a single string.

343+

By default, the program to execute is the first item in *args* if *args* is

344+

a sequence. If *args* is a string, the interpretation is

345+

platform-dependent and described below. See the *shell* and *executable*

346+

arguments for additional differences from the default behavior. Unless

347+

otherwise stated, it is recommended to pass *args* as a sequence.

348348
349349

On POSIX, if *args* is a string, the string is interpreted as the name or

350350

path of the program to execute. However, this can only be done if not

@@ -558,10 +558,6 @@ functions.

558558

Popen destructor now emits a :exc:`ResourceWarning` warning if the child

559559

process is still running.

560560
561-

.. versionchanged:: 3.7

562-

*args*, or the first element of *args* if *args* is a sequence, can now

563-

be a :term:`path-like object`.

564-
565561
566562

Exceptions

567563

^^^^^^^^^^

Original file line numberDiff line numberDiff line change

@@ -1097,12 +1097,7 @@ def _execute_child(self, args, executable, preexec_fn, close_fds,

10971097

assert not pass_fds, "pass_fds not supported on Windows."

10981098
10991099

if not isinstance(args, str):

1100-

try:

1101-

args = os.fsdecode(args) # os.PathLike -> str

1102-

except TypeError: # not an os.PathLike, must be a sequence.

1103-

args = list(args)

1104-

args[0] = os.fsdecode(args[0]) # os.PathLike -> str

1105-

args = list2cmdline(args)

1100+

args = list2cmdline(args)

11061101
11071102

# Process startup details

11081103

if startupinfo is None:

@@ -1374,10 +1369,7 @@ def _execute_child(self, args, executable, preexec_fn, close_fds,

13741369

if isinstance(args, (str, bytes)):

13751370

args = [args]

13761371

else:

1377-

try:

1378-

args = list(args)

1379-

except TypeError: # os.PathLike instead of a sequence?

1380-

args = [os.fsencode(args)] # os.PathLike -> [str]

1372+

args = list(args)

13811373
13821374

if shell:

13831375

# On Android the default shell is at '/system/bin/sh'.

Original file line numberDiff line numberDiff line change

@@ -1475,37 +1475,6 @@ def test_run_kwargs(self):

14751475

env=newenv)

14761476

self.assertEqual(cp.returncode, 33)

14771477
1478-

def test_run_with_pathlike_path(self):

1479-

# bpo-31961: test run(pathlike_object)

1480-

class Path:

1481-

def __fspath__(self):

1482-

# the name of a command that can be run without

1483-

# any argumenets that exit fast

1484-

return 'dir' if mswindows else 'ls'

1485-
1486-

path = Path()

1487-

if mswindows:

1488-

res = subprocess.run(path, stdout=subprocess.DEVNULL, shell=True)

1489-

else:

1490-

res = subprocess.run(path, stdout=subprocess.DEVNULL)

1491-
1492-

self.assertEqual(res.returncode, 0)

1493-
1494-

def test_run_with_pathlike_path_and_arguments(self):

1495-

# bpo-31961: test run([pathlike_object, 'additional arguments'])

1496-

class Path:

1497-

def __fspath__(self):

1498-

# the name of a command that can be run without

1499-

# any argumenets that exits fast

1500-

return sys.executable

1501-
1502-

path = Path()

1503-
1504-

args = [path, '-c', 'import sys; sys.exit(57)']

1505-

res = subprocess.run(args)

1506-
1507-

self.assertEqual(res.returncode, 57)

1508-
15091478

def test_capture_output(self):

15101479

cp = self.run_python(("import sys;"

15111480

"sys.stdout.write('BDFL'); "

Original file line numberDiff line numberDiff line change

@@ -630,6 +630,7 @@ Add contextlib.AsyncExitStack. Patch by Alexander Mohr and Ilya Kulakov.

630630

.. nonce: x5Sv0R

631631

.. section: Library

632632
633+

*Removed in Python 3.7.0b2.*

633634

The *args* argument of subprocess.Popen can now be a :term:`path-like

634635

object`. If *args* is given as a sequence, it's first element can now be a

635636

:term:`path-like object` as well.