GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-18108: Adding dir_fd and follow_symlinks keyword args to shutil.chown by ta1hia · Pull Request #15811 · python/cpython

bpo-18108: Adding dir_fd and follow_symlinks keyword args to shutil.chown #15811

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.

Sign up for GitHub

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

Merged
serhiy-storchaka merged 1 commit into python:main from ta1hia:bpo-18108
Apr 22, 2024

Uh oh!

There was an error while loading. Please reload this page.

Merged

bpo-18108: Adding dir_fd and follow_symlinks keyword args to shutil.chown #15811

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
1 commit
Select commit
e464e7b
bpo-18108: Adding dir_fd and follow_symlinks kwargs to shutil.chown
serhiy-storchaka Apr 22, 2024
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

6 changes: 5 additions & 1 deletion 6 Doc/library/shutil.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,8 @@ Directory and files operations

.. availability:: Unix, Windows.

.. function:: chown(path, user=None, group=None)
.. function:: chown(path, user=None, group=None, *, dir_fd=None, \
follow_symlinks=True)

Change owner *user* and/or *group* of the given *path*.

Expand All @@ -436,6 +437,9 @@ Directory and files operations

.. versionadded:: 3.3

.. versionchanged:: 3.13
Added *dir_fd* and *follow_symlinks* parameters.


.. function:: which(cmd, mode=os.F_OK | os.X_OK, path=None)

Expand Down
4 changes: 4 additions & 0 deletions 4 Doc/whatsnew/3.13.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,10 @@ os.path
exactly one (back)slash to be absolute.
(Contributed by Barney Gale and Jon Foster in :gh:`44626`.)

* Add support of *dir_fd* and *follow_symlinks* keyword arguments in
:func:`shutil.chown`.
(Contributed by Berker Peksag and Tahia K in :gh:`62308`)

pathlib
-------

Expand Down
12 changes: 10 additions & 2 deletions 12 Lib/shutil.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1442,11 +1442,18 @@ def disk_usage(path):
return _ntuple_diskusage(total, used, free)


def chown(path, user=None, group=None):
def chown(path, user=None, group=None, *, dir_fd=None, follow_symlinks=True):
"""Change owner user and group of the given path.

user and group can be the uid/gid or the user/group names, and in that case,
they are converted to their respective uid/gid.

If dir_fd is set, it should be an open file descriptor to the directory to
be used as the root of *path* if it is relative.

If follow_symlinks is set to False and the last element of the path is a
symbolic link, chown will modify the link itself and not the file being
referenced by the link.
"""
sys.audit('shutil.chown', path, user, group)

Expand All @@ -1472,7 +1479,8 @@ def chown(path, user=None, group=None):
if _group is None:
raise LookupError("no such group: {!r}".format(group))

os.chown(path, _user, _group)
os.chown(path, _user, _group, dir_fd=dir_fd,
follow_symlinks=follow_symlinks)

def get_terminal_size(fallback=(80, 24)):
"""Get the size of the terminal window.
Expand Down
34 changes: 33 additions & 1 deletion 34 Lib/test/test_shutil.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2212,7 +2212,9 @@ def test_disk_usage(self):
def test_chown(self):
dirname = self.mkdtemp()
filename = tempfile.mktemp(dir=dirname)
linkname = os.path.join(dirname, "chown_link")
write_file(filename, 'testing chown function')
os.symlink(filename, linkname)

with self.assertRaises(ValueError):
shutil.chown(filename)
Expand All @@ -2233,7 +2235,7 @@ def test_chown(self):
gid = os.getgid()

def check_chown(path, uid=None, gid=None):
s = os.stat(filename)
s = os.stat(path)
if uid is not None:
self.assertEqual(uid, s.st_uid)
if gid is not None:
Expand Down Expand Up @@ -2269,6 +2271,36 @@ def check_chown(path, uid=None, gid=None):
shutil.chown(dirname, user, group)
check_chown(dirname, uid, gid)

dirfd = os.open(dirname, os.O_RDONLY)
self.addCleanup(os.close, dirfd)
basename = os.path.basename(filename)
baselinkname = os.path.basename(linkname)
shutil.chown(basename, uid, gid, dir_fd=dirfd)
check_chown(filename, uid, gid)
shutil.chown(basename, uid, dir_fd=dirfd)
check_chown(filename, uid)
shutil.chown(basename, group=gid, dir_fd=dirfd)
check_chown(filename, gid=gid)
shutil.chown(basename, uid, gid, dir_fd=dirfd, follow_symlinks=True)
check_chown(filename, uid, gid)
shutil.chown(basename, uid, gid, dir_fd=dirfd, follow_symlinks=False)
check_chown(filename, uid, gid)
shutil.chown(linkname, uid, follow_symlinks=True)
check_chown(filename, uid)
shutil.chown(baselinkname, group=gid, dir_fd=dirfd, follow_symlinks=False)
check_chown(filename, gid=gid)
shutil.chown(baselinkname, uid, gid, dir_fd=dirfd, follow_symlinks=True)
check_chown(filename, uid, gid)

with self.assertRaises(TypeError):
shutil.chown(filename, uid, dir_fd=dirname)

with self.assertRaises(FileNotFoundError):
shutil.chown('missingfile', uid, gid, dir_fd=dirfd)

with self.assertRaises(ValueError):
shutil.chown(filename, dir_fd=dirfd)


@support.requires_subprocess()
class TestWhich(BaseTest, unittest.TestCase):
Expand Down
1 change: 1 addition & 0 deletions 1 Misc/NEWS.d/next/Library/2019-09-09-18-18-34.bpo-18108.ajPLAO.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:func:`shutil.chown` now supports *dir_fd* and *follow_symlinks* keyword arguments.
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.