[Python-checkins] bpo-26067: Do not fail test_shutil / chown when gid/uid cannot be resolved (#19032)

Matthias Braun webhook-mailer at python.org
Tue Mar 17 12:51:53 EDT 2020


https://github.com/python/cpython/commit/52268941f37e3e27bd01792b081877ec3bc9ce12
commit: 52268941f37e3e27bd01792b081877ec3bc9ce12
branch: master
author: Matthias Braun <matze at braunis.de>
committer: GitHub <noreply at github.com>
date: 2020-03-17T09:51:44-07:00
summary:

bpo-26067: Do not fail test_shutil / chown when gid/uid cannot be resolved (#19032)

* bpo-26067: Do not fail test_shutil.chown when gid/uid cannot be resolved

There is no guarantee that the users primary uid or gid can be resolved
in the unix group/account databases. Skip the last part of the chown
test if we cannot resolve the gid or uid to a name.

* 📜🤖 Added by blurb_it.

* Address review feedback

* address review feedback correctly

* fix typo

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>

files:
A Misc/NEWS.d/next/Tests/2020-03-16-20-54-55.bpo-26067.m18_VV.rst
M Lib/test/test_shutil.py

diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py
index 076c450e09bf6..b9fdfd1350a09 100644
--- a/Lib/test/test_shutil.py
+++ b/Lib/test/test_shutil.py
@@ -1666,12 +1666,17 @@ def check_chown(path, uid=None, gid=None):
         shutil.chown(dirname, group=gid)
         check_chown(dirname, gid=gid)
 
-        user = pwd.getpwuid(uid)[0]
-        group = grp.getgrgid(gid)[0]
-        shutil.chown(filename, user, group)
-        check_chown(filename, uid, gid)
-        shutil.chown(dirname, user, group)
-        check_chown(dirname, uid, gid)
+        try:
+            user = pwd.getpwuid(uid)[0]
+            group = grp.getgrgid(gid)[0]
+        except KeyError:
+            # On some systems uid/gid cannot be resolved.
+            pass
+        else:
+            shutil.chown(filename, user, group)
+            check_chown(filename, uid, gid)
+            shutil.chown(dirname, user, group)
+            check_chown(dirname, uid, gid)
 
 
 class TestWhich(BaseTest, unittest.TestCase):
diff --git a/Misc/NEWS.d/next/Tests/2020-03-16-20-54-55.bpo-26067.m18_VV.rst b/Misc/NEWS.d/next/Tests/2020-03-16-20-54-55.bpo-26067.m18_VV.rst
new file mode 100644
index 0000000000000..8b897a825d6a8
--- /dev/null
+++ b/Misc/NEWS.d/next/Tests/2020-03-16-20-54-55.bpo-26067.m18_VV.rst
@@ -0,0 +1 @@
+Do not fail test_shutil test_chown test when uid or gid of user cannot be resolved to a name.



More information about the Python-checkins mailing list