classification
Title: os.stat() fails on bytes paths under Windows 7
Type: behavior Stage: resolved
Components: Library (Lib), Windows Versions: Python 3.2
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: brian.curtin, db3l, eric.smith, jaraco, pitrou, terry.reedy, tim.golden
Priority: high Keywords: patch

Created on 2010-09-21 00:04 by pitrou, last changed 2010-09-21 18:19 by pitrou. This issue is now closed.

Files
File name Uploaded Description Edit
win7statbytes.patch pitrou, 2010-09-21 00:04
Messages (6)
msg117009 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-09-21 00:04
Under Windows 7, there is another path in posixmodule.c for stat() (because of the link dereferencing feature, it seems). This path fails for the bytes version. It turns out that GetFinalPathNameByHandleA returns a value which is one byte too small (while GetFinalPathNameByHandleW returns the expected value). The MSDN doc (*) seems to mention it although with a strange wording:

    “Windows Server 2008 and Windows Vista:  For the ANSI version of this function, GetFinalPathNameByHandleA, the return value includes the size of the terminating null character.”

(*) http://msdn.microsoft.com/en-us/library/aa364962%28VS.85%29.aspx

The net result is that, when we give 'buf_size+1' to the second GetFinalPathNameByHandleA() call after 'buf_size' was returned by the first call, the buffer is still not big enough and it doesn't get filled. The subsequent call to win32_lstat() is done with a bogus path and fails with "[Error 2]: the system cannot find the file specified".

Here is a patch fixing this and also harmonizing win32_stat() and win32_stat_w(). I've added a test, although additional tests for bytes symlinks would probably be deserved. Please review.

PS: the context is:
http://mail.python.org/pipermail/python-dev/2010-September/103860.html
msg117017 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2010-09-21 02:05
The bug appears to really be limited to Vista?/W7. On WinXP,
>>> os.path.exists('c:/programs/python31')
True
>>> os.path.exists(b'c:/programs/python31')
True
Has the patch been tested on WinXP to be sure it does not introduce a bug for this?
msg117036 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-09-21 08:45
> Has the patch been tested on WinXP to be sure it does not introduce a
> bug for this?

No. Can you?
msg117083 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2010-09-21 17:12
Only if given a revised 32bit binary (and revised test_os.py). I expect someone else who can will see this.
msg117088 - (view) Author: Brian Curtin (brian.curtin) * (Python committer) Date: 2010-09-21 17:59
The patch looks ok to me. I tested it on Server 2003 (same as XP) and it worked fine in addition to Windows 7.
msg117091 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-09-21 18:19
Ok, I committed the patch in r84956. Thank you for testing.
History
Date User Action Args
2010-09-21 18:19:40pitrousetstatus: open -> closed
resolution: fixed
messages: + msg117091

stage: resolved
2010-09-21 17:59:35brian.curtinsetmessages: + msg117088
2010-09-21 17:12:23terry.reedysetmessages: + msg117083
2010-09-21 08:45:23pitrousetmessages: + msg117036
2010-09-21 02:05:59terry.reedysetnosy: + terry.reedy
messages: + msg117017
2010-09-21 00:31:28eric.smithsetnosy: + jaraco, eric.smith
2010-09-21 00:04:15pitroucreate