Title: sys.argv and quoted arguments on command line
Type: behavior Stage: resolved
Components: Versions: Python 3.1, Python 2.5
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, fcr, r.david.murray
Priority: normal Keywords:

Created on 2010-11-05 10:06 by fcr, last changed 2010-11-05 13:11 by eric.smith. This issue is now closed.

File name Uploaded Description Edit
argtest fcr, 2010-11-05 10:06 test program to reproduce problem
Messages (6)
msg120480 - (view) Author: Frank Rügheimer (fcr) Date: 2010-11-05 10:06
Words in quoted command line arguments containing whitespace are split into separate entries of the argument vector sys.argv. This implemetation (quote removal + word splitting) removes information required to read string arguments passed via the command line.

The expected behaviour would be to unquote the argument, but not to conduct word splitting within the quoted text.

Test program output:

> ./argtest arg1 arg2 "this should be a single argument"
  ['./argtest', 'arg1', 'arg2', 'this', 'should', 'be', 'a', 'single', 'argument']

(observed with Python 3.1.2 (r312:79147, Oct 28 2010, 14:12:33)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2
msg120484 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2010-11-05 11:24
I don't see this behavior on MacOS:

$ ./argtest arg1 arg2 "this should be a single argument"
2.6.1 (r261:67515, Feb 11 2010, 15:47:53) 
[GCC 4.2.1 (Apple Inc. build 5646)]
['./argtest', 'arg1', 'arg2', 'this should be a single argument']

This splitting is done by the shell (Unix-like systems) or by the C runtime (Windows), not by Python. What OS are you running?
msg120486 - (view) Author: Frank Rügheimer (fcr) Date: 2010-11-05 12:00
2.6.18-194.17.1.el5 #1 SMP Wed Sep 29 12:51:33 EDT 2010 i686 i686 i386 GNU/Linux

GNU bash, version 3.2.25(1)-release (i686-redhat-linux-gnu)

I wrote test program in C to do the same thing as before and the arguments are treated properly:

> ./testcargs arg1 arg2 "this should be one arg"
argument 0 is:    ./testcargs
argument 1 is:    arg1
argument 2 is:    arg2
argument 3 is:    this should be one arg

My suspicion is that sys implementation goes into a branch intended for a different OS.
msg120487 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2010-11-05 12:13
The python version works for me also on a Fedora box with 3.2 and 2.7.

What shell are you using?

Did you compile this python yourself, or did it come with your distro?
msg120488 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-11-05 12:23
Works fine for me on Gentoo linux.

What exactly is 'python' in your path, and what happens if you do /usr/bin/python3 argtest?

I'm 99.99% certain this is not a bug in Python, otherwise it would have been reported long before now, since it would represent a major change in behavior.
msg120491 - (view) Author: Frank Rügheimer (fcr) Date: 2010-11-05 13:09
You are right, it seems to work when the file is passed directly into python so the quotes are stripped somewhere before python even gets to see them. 

Date User Action Args
2010-11-05 13:11:26eric.smithsetnosy: eric.smith, r.david.murray, fcr
resolution: not a bug
components: - None
stage: resolved
2010-11-05 13:09:25fcrsetstatus: open -> closed

messages: + msg120491
2010-11-05 12:23:07r.david.murraysetnosy: + r.david.murray
messages: + msg120488
2010-11-05 12:13:15eric.smithsetmessages: + msg120487
2010-11-05 12:00:22fcrsetmessages: + msg120486
2010-11-05 11:24:58eric.smithsetnosy: + eric.smith
messages: + msg120484
2010-11-05 10:06:34fcrcreate