[Tutor] slashes in paths

eryksun eryksun at gmail.com
Sun Jul 21 04:42:50 CEST 2013


On Sat, Jul 20, 2013 at 7:43 PM, Dominik George <nik at naturalnet.de> wrote:
> the base path is \, and one exists for every drive. C:\foo is foo in C:'s
> root, C:foo is foo in C:'s current working directory.

NT process parameters only track a single working directory, while DOS
maintained a current directory for each drive. This is emulated in NT
by setting special environment variables such as "=C:" for drive C:
(which is really \Device\HarddiskVolume1 in NT's native namespace).
Using environment variables is a simple way to have this inherited in
child processes.

Since Microsoft's C runtime ignores these variables when copying the
process environment, you won't find them in Python's os.environ.
They're supposed to be 'hidden'. Here's an example of using kernel32
GetEnvironmentVariableW to read the value of "=C:":

    >>> os.chdir('C:\Python27')

    >>> from ctypes import *
    >>> cwd_c = (c_wchar * 260)()
    >>> windll.kernel32.GetEnvironmentVariableW(u'=C:', cwd_c, 260)
    11
    >>> print cwd_c.value
    C:\Python27

The cmd shell gives you read-only access:

    >>> os.system('echo %=C:%')
    C:\Python27
    0

In practice, ntdll's RtlGetFullPathName_U uses these variables, but
RtlSetCurrentDirectory_U doesn't *set* them. NT leaves it up to the
application to maintain this state. cmd.exe is an obvious example of a
Win32 application that uses this. Also, if you use Microsoft's C
runtime, it sets these for you when you _wchdir(). Since CPython rolls
its own nt.chdir, it has to manage these "magic" variables:

http://hg.python.org/cpython/file/ab05e7dd2788/Modules/posixmodule.c#l890

This trick isn't perfect. You can get silly results if you manually
modify the values (use SetEnvironmentVariableW via ctypes). I'll leave
that as an exercise.


More information about the Tutor mailing list