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

Inconsistent behavior: Get st_nlink from os.stat() and os.scandir() #72228

Closed
MohansonLeaf mannequin opened this issue Sep 9, 2016 · 3 comments
Closed

Inconsistent behavior: Get st_nlink from os.stat() and os.scandir() #72228

MohansonLeaf mannequin opened this issue Sep 9, 2016 · 3 comments
Labels
OS-windows type-bug An unexpected behavior, bug, or error

Comments

@MohansonLeaf
Copy link
Mannequin

MohansonLeaf mannequin commented Sep 9, 2016

BPO 28041
Nosy @pfmoore, @vstinner, @tjguk, @zware, @zooba, @MojoVampire
PRs
  • bpo-28041: Inconsistent behavior: Get st_nlink from os.stat() and os.scandir() #795
  • Files
  • stat.py: python3.5 stat.py
  • 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 2017-04-21.23:46:00.386>
    created_at = <Date 2016-09-09.09:24:54.047>
    labels = ['type-bug', 'invalid', 'OS-windows']
    title = 'Inconsistent behavior: Get st_nlink from os.stat() and os.scandir()'
    updated_at = <Date 2017-04-21.23:46:00.384>
    user = 'https://bugs.python.org/MohansonLeaf'

    bugs.python.org fields:

    activity = <Date 2017-04-21.23:46:00.384>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2017-04-21.23:46:00.386>
    closer = 'vstinner'
    components = ['Windows']
    creation = <Date 2016-09-09.09:24:54.047>
    creator = 'Mohanson Leaf'
    dependencies = []
    files = ['44494']
    hgrepos = []
    issue_num = 28041
    keywords = []
    message_count = 3.0
    messages = ['275293', '290072', '292085']
    nosy_count = 7.0
    nosy_names = ['paul.moore', 'vstinner', 'tim.golden', 'zach.ware', 'steve.dower', 'josh.r', 'Mohanson Leaf']
    pr_nums = ['795']
    priority = 'normal'
    resolution = 'not a bug'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue28041'
    versions = ['Python 3.5']

    @MohansonLeaf
    Copy link
    Mannequin Author

    MohansonLeaf mannequin commented Sep 9, 2016

    os.stat(file).st_nlink gives 1 but same file's st_nlink from os.scandir(dir) gives 0.

    """
    Inconsistent behavior: Get st_nlink from os.stat() and os.scandir()
    Platform: Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:38:48) [MSC v.1900 32 bit (Intel)] on win32
    """

    import os
    import os.path

    print('os.stat:', os.stat(file).st_nlink)

    for entry in os.scandir(os.path.dirname(os.path.abspath(__file__))):
        if entry.name == 'stat.py':
            print('os.scandir:', entry.stat().st_nlink)

    """
    os.stat: 1
    os.scandir: 0
    """

    @MohansonLeaf MohansonLeaf mannequin added OS-windows type-bug An unexpected behavior, bug, or error labels Sep 9, 2016
    @MojoVampire
    Copy link
    Mannequin

    MojoVampire mannequin commented Mar 24, 2017

    This is documented behavior. Per the docs for os.DirEntry's stat method (the objects yielded by os.scandir):

    On Windows, the st_ino, st_dev and st_nlink attributes of the stat_result are always set to zero. Call os.stat() to get these attributes.

    It might be nice if those values could be cached on read (through a lazily initialized value on property access or the like), but this is not a bug, it's working as documented.

    @vstinner
    Copy link
    Member

    scandir () is designed for effiency, not for portability or correctness. The API has deliberate limitations which are well documented, as the one you spotted:
    https://docs.python.org/dev/library/os.html#os.DirEntry.stat

    Your PR would defeat the whole purpose of scandir.

    @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 type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant