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
Comments
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. |
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. |
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: 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. |
Just to note that 3.5 still raises ImportError rather than NotImplementedError although the latter is the consensus here. |
New changeset 8ac695499fa3 by Berker Peksag in branch '3.5': New changeset 1602fa504e72 by Berker Peksag in branch 'default': |
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. |
Tests fail... ====================================================================== 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' ====================================================================== 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' |
New changeset 64d45f3d0978 by Victor Stinner in branch '3.5': |
Thanks Victor. |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: