[Python-bugs-list] [ python-Bugs-512433 ] Quote handling in os.system & os.popen

noreply@sourceforge.net noreply@sourceforge.net
Sat, 09 Mar 2002 17:47:01 -0800


Bugs item #512433, was opened at 2002-02-03 12:36
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=512433&group_id=5470

Category: Windows
Group: Python 2.2
>Status: Closed
>Resolution: Wont Fix
Priority: 5
Submitted By: Jimmy Retzlaff (jretz)
Assigned to: Tim Peters (tim_one)
Summary: Quote handling in os.system & os.popen

Initial Comment:
On Python 2.2 under Windows XP:

    os.system('"notepad" "test.py"')

does not work as expected. It appears that os.system 
attempts to run:

    notepad" "test.py

A workaround is to use:

    os.system('""notepad" "test.py""')

Both of the following work as expected:

    os.system('notepad "test.py"')
    os.system('"notepad" test.py')

os.popen exhibits the same behaviour. In naive 
testing, the following hack seems to make things 
better:

    os_system = os.system
    os.system = lambda command: os_system('"%s"' % 
command)

This may suggest a potential fix in the C code - or 
it may simply offend the sensibilities of those more 
knowledgeable than me. :)

----------------------------------------------------------------------

>Comment By: Tim Peters (tim_one)
Date: 2002-03-09 20:47

Message:
Logged In: YES 
user_id=31435

Sorry, I'm closing as "Won't Fix".  os.system() and os.popen
() are barely usable on Windows, and it's going to remain 
that way until Python grows its own command shell.  Before 
that, it's at the mercy of what the MS shells happen to 
do.  In the case of XP's cmd.exe, you're a victim of 
documented (by MS) behavior:  see the /C and /K options to 
cmd.exe:

"""
If /C or /K is specified, then the remainder of the command 
line after the switch is processed as a command line, where 
the following logic is used to process quote (") 
characters: 

    1. If all of the following conditions are met, then 
quote characters on the command line are preserved: 

        - no /S switch 
        - exactly two quote characters 
        - no special characters between the two quote 
characters, where special is one of: &<>()@^| 
        - there are one or more whitespace characters 
between the two quote characters 
        - the string between the two quote characters is 
the name of an executable file. 

    2. Otherwise, old behavior is to see if the first 
character is a quote character and if so, strip the leading 
character and remove the last quote character on the 
command line, preserving any text after the last quote 
character.
"""

You're a victim of clause #2 there.  The MS shells aren't 
consistent about these rules, so there's nothing Python can 
do to try to out-guess them, short of heroic efforts.  For 
example, if we took your suggestion, things that work fine 
today under Win98's command.com would suddenly break (W98 
does *not* strip the new quotes you're adding, so "Bad 
command or file name" is the usual result).

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=512433&group_id=5470