Corectly convert from %PATH%=c:\\X; "c:\\a; b" TO ['c:\\X', 'c:\\a; b']

Jeff Epler jepler at unpythonic.net
Sun Apr 3 16:45:29 EDT 2005


The C code that Python uses to find the initial value of sys.path based
on PYTHONPATH seems to be simple splitting on the equivalent of
os.pathsep.  See the source file Python/sysmodule.c, function
makepathobject().
        for (i = 0; ; i++) {
                p = strchr(path, delim); // ";" on windows, ":" on unix
                if (p == NULL) ...
                w = PyString_FromStringAndSize(path, (int) (p - path));
                if (w == NULL) ...
                PyList_SetItem(v, i, w);
                if (*p == '\0')
                        break;
                path = p+1;
        }
No special handling of quote characters happens here.

> I think I will stick to a simple splitting at the ;. Luckily all the
> directories I am dealing with have nice names.

If you do this, you'll match the behavior of python itself, and you'll match
the behavior of wine.

> I have not even tried to see what quirks there exist with unix.

None.  There's no way to quote anything in paths, so while you can't place a
directory with a colon in its name on your path, nobody loses any sleep over
it either.  Here's what the Open Group has to say about PATH:
    PATH
        This variable shall represent the sequence of path prefixes that
        certain functions and utilities apply in searching for an executable
        file known only by a filename. The prefixes shall be separated by a
        colon ( ':' ). When a non-zero-length prefix is applied to this
        filename, a slash shall be inserted between the prefix and the
        filename. A zero-length prefix is a legacy feature that indicates the
        current working directory. It appears as two adjacent colons ( "::" ),
        as an initial colon preceding the rest of the list, or as a trailing
        colon following the rest of the list. A strictly conforming application
        shall use an actual pathname (such as .) to represent the current
        working directory in PATH . The list shall be searched from beginning
        to end, applying the filename to each prefix, until an executable file
        with the specified name and appropriate execution permissions is found.
        If the pathname being sought contains a slash, the search through the
        path prefixes shall not be performed. If the pathname begins with a
        slash, the specified path is resolved (see Pathname Resolution). If
        PATH is unset or is set to null, the path search is
        implementation-defined.
ah, if only windows was so well-defined!

Jeff
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20050403/b6dbeb5e/attachment.sig>


More information about the Python-list mailing list