classification
Title: subprocess popen broken for bytes and backslash
Type: behavior Stage: resolved
Components: IO, Library (Lib) Versions: Python 3.2, Python 2.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, kaizhu
Priority: normal Keywords:

Created on 2011-02-02 02:28 by kaizhu, last changed 2011-02-02 08:19 by eric.smith. This issue is now closed.

Messages (2)
msg127714 - (view) Author: kai zhu (kaizhu) Date: 2011-02-02 02:28
noticed when trying to call grep w/ backslashes in regexp, in shell mode.
same behavior on python2.5 & python3.2

in shell mode:
1. bytes is broken
2. 1st character after backslash is always silently truncated (with exception of '\\')

$ python3.2
Python 3.2rc1+ (py3k, Jan 24 2011, 15:00:02) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.call(b'echo aa', shell = True) ## bytes broken
Traceback (most recent call last):
  File "<pseudosugar console>", line 1, in <module>
  File "/home/public/i486-pc-linux-gnu/lib/python3.2/subprocess.py", line 460, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/home/public/i486-pc-linux-gnu/lib/python3.2/subprocess.py", line 736, in __init__
    restore_signals, start_new_session)
  File "/home/public/i486-pc-linux-gnu/lib/python3.2/subprocess.py", line 1175, in _execute_child
    restore_signals, start_new_session, preexec_fn)
TypeError: Can't convert 'int' object to str implicitly
>>> subprocess.call('echo \aa', shell = True) ## backslash \
a
0
>>> subprocess.call('echo \\aa', shell = True) ## backslash \\
aa
0
>>> subprocess.call('echo \\\aa', shell = True) ## backslash \\\
a
0
>>> subprocess.call('echo \\\\aa', shell = True) ## backslash \\\\
a
0
>>> subprocess.call('echo \\\\\aa', shell = True) ## backslash \\\\\
\a
0
>>> subprocess.call('echo \\\\\\aa', shell = True) ## backslash \\\\\\
a
0
>>> subprocess.call('echo \\\\\\\\\\\\\\\\\\\\\\\\\aa', shell = True)
\\\a
0
msg127723 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2011-02-02 08:19
1: subprocess.call is documented as taking a string, not bytes. If you think it should also take bytes, I suggest opening a separate bug as a feature request.

2: You're running into both Python and the shell escaping. If you have an odd number of backslashes, then python is converting '\a' into an ascii BEL character:
http://docs.python.org/py3k/reference/lexical_analysis.html#string-literals
For the remaining backslashes, Python is converting every 2 into a single backslash when it does its escaping, and the shell is doing the same thing, as is echo. That accounts for 8 backslashes becoming a single backslash in the output.
History
Date User Action Args
2011-02-02 08:19:50eric.smithsetstatus: open -> closed

nosy: + eric.smith
messages: + msg127723

resolution: not a bug
stage: resolved
2011-02-02 02:30:32kaizhusetcomponents: + Library (Lib), IO
2011-02-02 02:28:40kaizhucreate