Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

os.stat() fails on bytes paths under Windows 7 #54117

Closed
pitrou opened this issue Sep 21, 2010 · 6 comments
Closed

os.stat() fails on bytes paths under Windows 7 #54117

pitrou opened this issue Sep 21, 2010 · 6 comments
Labels
OS-windows stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@pitrou
Copy link
Member

pitrou commented Sep 21, 2010

BPO 9908
Nosy @terryjreedy, @db3l, @jaraco, @pitrou, @ericvsmith, @tjguk, @briancurtin
Files
  • win7statbytes.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2010-09-21.18:19:40.447>
    created_at = <Date 2010-09-21.00:04:15.233>
    labels = ['type-bug', 'library', 'OS-windows']
    title = 'os.stat() fails on bytes paths under Windows 7'
    updated_at = <Date 2010-09-21.18:19:40.446>
    user = 'https://github.com/pitrou'

    bugs.python.org fields:

    activity = <Date 2010-09-21.18:19:40.446>
    actor = 'pitrou'
    assignee = 'none'
    closed = True
    closed_date = <Date 2010-09-21.18:19:40.447>
    closer = 'pitrou'
    components = ['Library (Lib)', 'Windows']
    creation = <Date 2010-09-21.00:04:15.233>
    creator = 'pitrou'
    dependencies = []
    files = ['18940']
    hgrepos = []
    issue_num = 9908
    keywords = ['patch']
    message_count = 6.0
    messages = ['117009', '117017', '117036', '117083', '117088', '117091']
    nosy_count = 7.0
    nosy_names = ['terry.reedy', 'db3l', 'jaraco', 'pitrou', 'eric.smith', 'tim.golden', 'brian.curtin']
    pr_nums = []
    priority = 'high'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue9908'
    versions = ['Python 3.2']

    @pitrou
    Copy link
    Member Author

    pitrou commented Sep 21, 2010

    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

    @pitrou pitrou added stdlib Python modules in the Lib dir OS-windows type-bug An unexpected behavior, bug, or error labels Sep 21, 2010
    @terryjreedy
    Copy link
    Member

    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?

    @pitrou
    Copy link
    Member Author

    pitrou commented Sep 21, 2010

    Has the patch been tested on WinXP to be sure it does not introduce a
    bug for this?

    No. Can you?

    @terryjreedy
    Copy link
    Member

    Only if given a revised 32bit binary (and revised test_os.py). I expect someone else who can will see this.

    @briancurtin
    Copy link
    Member

    The patch looks ok to me. I tested it on Server 2003 (same as XP) and it worked fine in addition to Windows 7.

    @pitrou
    Copy link
    Member Author

    pitrou commented Sep 21, 2010

    Ok, I committed the patch in r84956. Thank you for testing.

    @pitrou pitrou closed this as completed Sep 21, 2010
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    OS-windows stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants