This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: subprocess must escape redirection characters under win32
Type: Stage:
Components: Library (Lib) Versions:
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: astrand Nosy List: astrand, nnorwitz, pmezard
Priority: normal Keywords:

Created on 2007-05-01 20:46 by pmezard, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
test_subp pmezard, 2007-05-01 20:46 subprocess.list2cmdline test
fix_subp pmezard, 2007-05-01 20:47 Patch list2cmdline
Messages (4)
msg31927 - (view) Author: Patrick Mézard (pmezard) Date: 2007-05-01 20:46
Hello,
For some reason, subprocess.Popen arguments are not processed correctly when one of them contains a redirection character ("<>&|") when calling a batch file.

Unittest and patch are attached below. Here are the steps to reproduce it:

callee.py
"""
import sys
print sys.argv
"""

callee.bat
"""
python callee.py %*
"""

caller.py
"""
import subprocess

args = [
    'a<b',
    'a>b',
    'a|b',
    'a&b',
]

for arg in args:
    subprocess.check_call(['callee.bat', arg])
"""

Then:
"""
>python caller.py
The system cannot find the file specified.
Traceback (most recent call last):
  File "caller.py", line 22, in <module>
    subprocess.check_call(['callee.bat', arg])
  File "C:\Python251\lib\subprocess.py", line 461, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['callee.bat', 'a<b']' returned non-zero
 exit status 1
"""

With:
"""
Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
"""
msg31928 - (view) Author: Patrick Mézard (pmezard) Date: 2007-05-01 20:47
File Added: fix_subp
msg31929 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2007-05-02 06:38
Peter, can you take a look at this?
msg31930 - (view) Author: Peter Åstrand (astrand) * (Python committer) Date: 2007-05-04 21:13
I'm testing with a simpler pair of files:

callee.bat:
echo arg1 %1
echo arg2 %2
echo arg3 %3

caller.py:
import subprocess
subprocess.call(['callee.bat', 'a<b'])

This fails. What happens is that CreateProcess is called with:
lpApplicationName=None
lpCommandLine="callee.bat a<b"

As far as I know, this is correct: I can't find anything in the documentation of CreateProcess that indicates that "<" is a magical meta character in this context. I find it very surprising that CreateProcess behaves like this. 

After some further investigations, I've found out that it's the fact that we are calling a batch file that's the problem. CreateProcess does not fail on a command line such as "view.exe a<b". Indeed, the documentation as well as a community comment on http://msdn2.microsoft.com/en-us/library/ms682425.aspx indicates that batch files are handled in a special (broken?) way. 

I'm reluctant to accept the attached patch. It might for this particular situation, but as far as I can tell, we are following the documented API. You should be able to work around this problem by quoting in your application. 
History
Date User Action Args
2022-04-11 14:56:24adminsetgithub: 44918
2007-05-01 20:46:25pmezardcreate