[Python-Dev] Testsuite fails on Windows if a space is in the path

Tim Peters tim.peters at gmail.com
Sat Sep 16 21:49:52 CEST 2006

[Martin v. Löwis]
> ...
> Can somebody remember what the reason is to invoke cmd.exe (or COMSPEC)
> in os.popen?

Absolutely necessary, as any number of shell gimmicks can be used in
the passed string, same as on non-Windows boxes; .e.g.,

>>> import os
>>> os.environ['STR'] = 'SSL'
>>> p = os.popen("findstr %STR% *.py | sort")
>>> print p.read()
build_ssl.py:        print " None of these versions appear suitable
for building OpenSSL"
build_ssl.py:        print "Could not find an SSL directory in '%s'" %
build_ssl.py:        print "Found an SSL directory at '%s'" % (best_name,)
build_ssl.py:    # Look for SSL 2 levels up from pcbuild - ie, same
place zlib etc all live.

That illustrates envar substitution and setting up a pipe in the
passed string, and people certainly do things like that.

These are the MS docs for cmd.exe's inscrutable quoting rules after /C:

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.


    cmd.exe /c "c:\Program Files\python25\python.exe"

example fit clause #1 above.

    cmd.exe /c "c:\Program Files\python25\python.exe" -c "import sys;print

fails the "exactly two quote characters" part of #1, so falls into #2,
and after stripping the first and last quotes leaves the senseless:

cmd.exe /c c:\Program Files\python25\python.exe" -c "import sys;print

> (i.e. doubling the quotes at the beginning and the end) [works]

And that follows from the above, although not for a reason any sane
person would guess :-(

I personally wouldn't change anything here for 2.5.  It's a minefield,
and people who care a lot already have their own workarounds in place,
which we'd risk breaking.  It remains a minefield for newbies, but
we're really just passing on cmd.exe's behaviors.  People are
well-advised to accept the installer's default directory.

More information about the Python-Dev mailing list