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

pathlib.owner() and pathlib.group() raise ImportError on Windows #64788

Closed
spiralx mannequin opened this issue Feb 11, 2014 · 9 comments
Closed

pathlib.owner() and pathlib.group() raise ImportError on Windows #64788

spiralx mannequin opened this issue Feb 11, 2014 · 9 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@spiralx
Copy link
Mannequin

spiralx mannequin commented Feb 11, 2014

BPO 20589
Nosy @pitrou, @vstinner, @berkerpeksag
Files
  • win_owner_group_error_fix.patch: Patches WindowsPath to fix the error
  • 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 2016-03-12.00:51:48.320>
    created_at = <Date 2014-02-11.03:25:09.272>
    labels = ['type-bug', 'library']
    title = 'pathlib.owner() and pathlib.group() raise ImportError on Windows'
    updated_at = <Date 2016-03-12.00:51:48.319>
    user = 'https://bugs.python.org/spiralx'

    bugs.python.org fields:

    activity = <Date 2016-03-12.00:51:48.319>
    actor = 'berker.peksag'
    assignee = 'none'
    closed = True
    closed_date = <Date 2016-03-12.00:51:48.320>
    closer = 'berker.peksag'
    components = ['Library (Lib)']
    creation = <Date 2014-02-11.03:25:09.272>
    creator = 'spiralx'
    dependencies = []
    files = ['34033']
    hgrepos = []
    issue_num = 20589
    keywords = ['patch']
    message_count = 9.0
    messages = ['210896', '210925', '211029', '236229', '261601', '261602', '261610', '261612', '261625']
    nosy_count = 6.0
    nosy_names = ['pitrou', 'vstinner', 'BreamoreBoy', 'python-dev', 'berker.peksag', 'spiralx']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue20589'
    versions = ['Python 3.5', 'Python 3.6']

    @spiralx
    Copy link
    Mannequin Author

    spiralx mannequin commented Feb 11, 2014

    When using the Path class in Lib/pathlib.py under Windows, calling p.owner() or p.group() fails with an ImportError due to importing the pwd and grp modules respectively, as neither of those exist. The documentation doesn't mention this behaviour.

    The precedent for handling this is set by os.stat(), which simply sets the st_uid and st_gid fields to 0 under Windows, and this behaviour underlies p.stat(). Therefore both p.owner() and p.group() should return 0 under Windows as well.

    >>> p = Path("foo.py")
    >>> p.stat()
    os.stat_result(st_mode=33206, st_ino=434125405408, st_dev=318314347, st_nlink=1, st_uid=0, st_gid=0, st_size=40, st_atime=1392076800, st_mtime=1392084010, st_ctime=1392083969)
    >>> p.owner()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "D:\Python34\Lib\pathlib.py", line 1051, in owner
        import pwd
    ImportError: No module named 'pwd'
    >>> p.group()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "D:\Python34\Lib\pathlib.py", line 1058, in group
        import grp
    ImportError: No module named 'grp'

    After the patch, the behaviour is:

    >>> p = Path("foo.py")
    >>> p.stat()
    os.stat_result(st_mode=33206, st_ino=434125405408, st_dev=318314347, st_nlink=1, st_uid=0, st_gid=0, st_size=40, st_atime=1392076800, st_mtime=1392084010, st_ctime=1392083969)
    >>> p.owner()
    0
    >>> p.group()
    0
    >>> p.owner() == p.stat().st_uid
    True
    >>> p.group() == p.stat().st_gid
    True

    The supplied patch just creates overriden versions of owner() and group() that return 0 under the WindowsPath class.

    @spiralx spiralx mannequin added the stdlib Python modules in the Lib dir label Feb 11, 2014
    @pitrou
    Copy link
    Member

    pitrou commented Feb 11, 2014

    But owner() and group() don't return the uid or gid: they return the *name* of the owner or group (respectively). If you want the uid (resp. gid), just use st_uid (resp. st_gid) as shown in your example.

    Under Unix:

    >>> p = Path('setup.py')
    >>> p.owner()
    'antoine'
    >>> p.group()
    'antoine'

    So if you want to fix the issue under Windows, you need to find a way to grap the file owner's name (and group, if that makes sense under the Windows permission model).

    In the meantime though, perhaps the ImportError should be fixed to a NotImplementedError under Windows.

    @spiralx
    Copy link
    Mannequin Author

    spiralx mannequin commented Feb 11, 2014

    Ok, I'm not sure when I got the idea they should return ids instead of the names :) But raising NotImplementedError is certainly an improvement on ImportError.

    As to implementing it on Windows, the answer by eryksun on this page works without requiring anything other than ctypes:

    http://stackoverflow.com/questions/8086412/howto-determine-file-owner-on-windows-using-python-without-pywin32

    That's about as simple as it gets it seems, the process for the group is almost identical, but groups aren't used very much on Windows anyway.

    @BreamoreBoy
    Copy link
    Mannequin

    BreamoreBoy mannequin commented Feb 19, 2015

    Just to note that 3.5 still raises ImportError rather than NotImplementedError although the latter is the consensus here.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Mar 11, 2016

    New changeset 8ac695499fa3 by Berker Peksag in branch '3.5':
    Issue bpo-20589: Invoking Path.owner() and Path.group() on Windows now raise
    https://hg.python.org/cpython/rev/8ac695499fa3

    New changeset 1602fa504e72 by Berker Peksag in branch 'default':
    Issue bpo-20589: Invoking Path.owner() and Path.group() on Windows now raise
    https://hg.python.org/cpython/rev/1602fa504e72

    @berkerpeksag
    Copy link
    Member

    I've changed the exception to raise NotImplementedError. I'm not sure about using the ctypes solution in the stdlib. Perhaps we should wrap Windows GetSecurity* APIs (similar to what we did for UNIX/POSIX APIs) first then we can use it in pathlib.

    @berkerpeksag berkerpeksag added the type-bug An unexpected behavior, bug, or error label Mar 11, 2016
    @vstinner
    Copy link
    Member

    Tests fail...
    http://buildbot.python.org/all/builders/x86-64 Ubuntu 15.10 Skylake CPU 3.5/builds/276/

    ======================================================================
    ERROR: test_group (test.test_pathlib.PureWindowsPathTest)
    ----------------------------------------------------------------------

    Traceback (most recent call last):
      File "/home/buildbot/buildarea/3.5.intel-ubuntu-skylake/build/Lib/test/test_pathlib.py", line 1167, in test_group
        P('c:/').group()
    AttributeError: 'PureWindowsPath' object has no attribute 'group'

    ======================================================================
    ERROR: test_owner (test.test_pathlib.PureWindowsPathTest)
    ----------------------------------------------------------------------

    Traceback (most recent call last):
      File "/home/buildbot/buildarea/3.5.intel-ubuntu-skylake/build/Lib/test/test_pathlib.py", line 1162, in test_owner
        P('c:/').owner()
    AttributeError: 'PureWindowsPath' object has no attribute 'owner'

    @vstinner vstinner reopened this Mar 11, 2016
    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Mar 11, 2016

    New changeset 64d45f3d0978 by Victor Stinner in branch '3.5':
    Issue bpo-20589: Fix test_pathlib
    https://hg.python.org/cpython/rev/64d45f3d0978

    @berkerpeksag
    Copy link
    Member

    Thanks Victor.

    @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
    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