[Python-Dev] test_popen broken on Win2K

Tim Peters tim.one@comcast.net
Fri, 07 Mar 2003 16:03:36 -0500

Someone changed test_popen to "quote" the path to python:

    cmd = '"%s" -c "import sys;print sys.argv" %s' % (sys.executable,
           ^  ^

The double-quote characters above the carets are new.

This causes test_popen to fail on Win2K, but not on Win98.  The relevant
difference appears to be the default shell (cmd.exe on the former,
command.com on the latter).

Simplifed example, on Win2K:

>>> p = os.popen('python -c "print 666"')
>>> p.read()
>>> p.close()

Worked fine, but doesn't if python is quoted:

>>> p = os.popen('"python" -c "print 666"')
>>> p.read()
>>> p.close()

The same kind of behavior can be observed directly from a DOS-box prompt:

C:\Code\python\PCbuild>cmd /c python -c "print 666"


Worked fine, but quoting the program name flops:

C:\Code\python\PCbuild>cmd /c "python" -c "print 666"
'python" -c "print' is not recognized as an internal or external command,
operable program or batch file.


So it looks like it stripped off the first and last double-quote characters,
leaving two senseless double-quote characters "in the middle".

>From the docs for 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
          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.

We're apparently in case #2, if for no other reason then for that there
aren't "exactly two quote characters".

I'll check in a hack to worm around this in the test, but anyone who can do
better, please do (I won't have access to a Win2K box next week).