[Python-Dev] Special file "nul" in Windows and os.stat

Scott Dial scott+python-dev at scottdial.com
Wed Nov 7 02:03:57 CET 2007


Martin v. Löwis wrote:
>> So, the question is what we should do?:
> 
> Before this question can be answered, I think we need to fully
> understand what precisely is happening in 2.4, and what precisely
> is happening in 2.5.
> 

Seeing this thread drag on was enough to get me to find out what 
changed. The implementation of "os.stat" has been changed. In 2.4.4, 
_stati64/_wstati64 were called directly, but in 2.5.1, a new pair of 
functions were wrote win32_stat/win32_wstat.

_stati64/_wstati64 (as others have noted) fallback onto the use of 
FindFirstFile. win32_stat/win32_wstat use some functions called 
Py_GetFileAttributesExA/Py_GetFileAttributesExW which ultimately use 
GetFileAttributesA/GetFileAttributesW.

The change to this implementation is r42230 with the Misc/NEWS comment 
saying:

     - Use Win32 API to implement os.stat/fstat. As a result, subsecond
       timestamps are reported, the limit on path name lengths is
       removed, and stat reports WindowsError now (instead of OSError).

As to the specifics of what FindFirstFile* does with the values, I 
tested this quickly with ctypes on 'nul' (or any of the other special 
files):

cAlternameFileName:
cFileName: nul
dwFileAttributes: 32
dwReserved0: 0
dwReserved1: 0
ftCreationTime: (dwLowDateTime: 0, dwHighDateTime: 0)
ftLastAccessTime: (dwLowDateTime: 0, dwHighDateTime: 0)
ftLastWriteTime: (dwLowDateTime: 0, dwHighDateTime: 0)
nFileSizeHigh: 0
nFileSizeLow: 0

In order to keep the higher accuracy timestamps for normal files and to 
maintain the old behavior, my recommendation would be that the existing 
implementation of win32_stat/win32_wstat be extended to use 
FindFileFirst if GetFileAttributes* fails. I would be willing to do the 
legwork for such a patch if everyone agrees this is the appropriate 
solution.

* As an aside, Martin, I find the argument that "hard-wiring is bad" to 
be against what is actually occurring in the posixmodule. For that 
matter, the S_IFEXEC flag is hardwired to path in (*.bat, *.cmd, *.exe, 
*.com) despite the fact that the platform says it is really anything in 
the list of os.getenv('PATHEXT'), but I suppose that is a bug for 
another day.

-Scott

-- 
Scott Dial
scott at scottdial.com
scodial at cs.indiana.edu


More information about the Python-Dev mailing list