classification
Title: Can't pass Path like objects to subprocess api's on Windows.
Type: behavior Stage:
Components: Windows Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: paul.moore, rasjani, serhiy.storchaka, steve.dower, terry.reedy, tim.golden, xtreak, zach.ware
Priority: normal Keywords:

Created on 2020-08-27 14:25 by rasjani, last changed 2020-09-12 01:28 by terry.reedy.

Messages (5)
msg375993 - (view) Author: Jani Mikkonen (rasjani) Date: 2020-08-27 14:25
Following code:

```
from pathlib import Path
import os
import subprocess

dir_name = os.environ.get("WORKSPACE", None) or "."
output_directory = Path(dir_name) / "results"
res = subprocess.run(["mytest", "--output", output_directory])
```

Works on macos and linux but on windows, this causes "TypeError: argument of type 'WindowsPath' is not iterable" at https://github.com/python/cpython/blob/master/Lib/subprocess.py#L568

(line is different, depending on the actual python release but i guess that gives the idea where it is)..

Quick test to check if i can do  `" " in Path("/tmp")` on posix platforms shows the same exception but with PosixPath type but apparently this sort of check does not happen on those platforms as the example code works fine there.

It would be nice to be able to pass path objects to subprocess api calls also in windows without explicitly casting the object into string before passing it as argument.
msg375995 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2020-08-27 14:40
See also https://bugs.python.org/issue31961
msg376024 - (view) Author: Jani Mikkonen (rasjani) Date: 2020-08-28 08:59
@xtreak

I went thru the comments of the report you linked. It definitely looks like the discussion was related  but it didnt really fix the issue here.

https://bugs.python.org/issue31961#msg311775 without trying out, that comment actually touches the underlying issue, on windows, list2cmdline throws that typeerror because iterated argument list (output_directory in my example) is WindowsPath and thus not iterable and and testing the arg with "in" operator fails ..
msg376043 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-08-28 16:10
I do not understand how you get this result. At that line of code arg can only be string, because it is a result of os.fsdecode(). Are you sure that you use Python version which includes issue31961 changes? What exactly Python version do you use?
msg376760 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2020-09-12 01:28
I closed #41753 as a duplicate of this.  

On that issue, Eryk Sun said "The underlying subprocess.Popen class was updated in 3.8 to support path-like objects in `args` (with shell=False) and `executable` on Windows [1]. This change was not backported to 3.6 and 3.7."

The latter is because 3.6 and 3.7 only get security patches.  Jani, please verify that this is still an issue with 3.8 and/or 3.9.
History
Date User Action Args
2020-09-12 01:28:59terry.reedysetnosy: + terry.reedy

messages: + msg376760
versions: - Python 3.5, Python 3.6, Python 3.7
2020-09-12 01:25:10terry.reedylinkissue41753 superseder
2020-08-28 16:10:55serhiy.storchakasetmessages: + msg376043
2020-08-28 08:59:48rasjanisetmessages: + msg376024
2020-08-27 14:40:41xtreaksetnosy: + serhiy.storchaka, xtreak
messages: + msg375995
2020-08-27 14:25:18rasjanicreate