classification
Title: pathlib.owner() and pathlib.group() raise ImportError on Windows
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.6, Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: BreamoreBoy, berker.peksag, pitrou, python-dev, spiralx, vstinner
Priority: normal Keywords: patch

Created on 2014-02-11 03:25 by spiralx, last changed 2016-03-12 00:51 by berker.peksag. This issue is now closed.

Files
File name Uploaded Description Edit
win_owner_group_error_fix.patch spiralx, 2014-02-11 03:25 Patches WindowsPath to fix the error review
Messages (9)
msg210896 - (view) Author: James Skinner (spiralx) Date: 2014-02-11 03:25
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.
msg210925 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-02-11 11:48
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.
msg211029 - (view) Author: James Skinner (spiralx) Date: 2014-02-11 22:19
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.
msg236229 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2015-02-19 18:23
Just to note that 3.5 still raises ImportError rather than NotImplementedError although the latter is the consensus here.
msg261601 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-03-11 21:07
New changeset 8ac695499fa3 by Berker Peksag in branch '3.5':
Issue #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 #20589: Invoking Path.owner() and Path.group() on Windows now raise
https://hg.python.org/cpython/rev/1602fa504e72
msg261602 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2016-03-11 21:13
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.
msg261610 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-03-11 21:47
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'
msg261612 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-03-11 21:53
New changeset 64d45f3d0978 by Victor Stinner in branch '3.5':
Issue #20589: Fix test_pathlib
https://hg.python.org/cpython/rev/64d45f3d0978
msg261625 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2016-03-12 00:51
Thanks Victor.
History
Date User Action Args
2016-03-12 00:51:48berker.peksagsetstatus: open -> closed
resolution: fixed
messages: + msg261625
2016-03-11 21:53:35python-devsetmessages: + msg261612
2016-03-11 21:47:48vstinnersetstatus: closed -> open

nosy: + vstinner
messages: + msg261610

resolution: fixed -> (no value)
2016-03-11 21:13:35berker.peksagsetstatus: open -> closed

type: behavior
versions: + Python 3.5, Python 3.6, - Python 3.4
nosy: + berker.peksag

messages: + msg261602
resolution: fixed
stage: resolved
2016-03-11 21:07:40python-devsetnosy: + python-dev
messages: + msg261601
2015-02-19 18:23:33BreamoreBoysetnosy: + BreamoreBoy
messages: + msg236229
2014-02-11 22:20:00spiralxsetmessages: + msg211029
2014-02-11 11:48:28pitrousetmessages: + msg210925
2014-02-11 03:30:36vajraskysetnosy: + pitrou
2014-02-11 03:25:09spiralxcreate