]> granicus.if.org Git - python/commitdiff
Issue #26937: The chown() method of the tarfile.TarFile class does not fail now
authorXavier de Gaye <xdegaye@users.sourceforge.net>
Fri, 9 Dec 2016 08:33:09 +0000 (09:33 +0100)
committerXavier de Gaye <xdegaye@users.sourceforge.net>
Fri, 9 Dec 2016 08:33:09 +0000 (09:33 +0100)
when the grp module cannot be imported, as for example on Android platforms.

Lib/tarfile.py
Misc/NEWS

index b78b1b1f4bb47364d0a3faaa54e89f35c03cdab3..5d4c86ce36662d82c304382e4f0c831ab5f51e2a 100755 (executable)
@@ -50,9 +50,13 @@ import copy
 import re
 
 try:
-    import grp, pwd
+    import pwd
 except ImportError:
-    grp = pwd = None
+    pwd = None
+try:
+    import grp
+except ImportError:
+    grp = None
 
 # os.symlink on Windows prior to 6.0 raises NotImplementedError
 symlink_exception = (AttributeError, NotImplementedError)
@@ -2219,22 +2223,25 @@ class TarFile(object):
 
     def chown(self, tarinfo, targetpath, numeric_owner):
         """Set owner of targetpath according to tarinfo. If numeric_owner
-           is True, use .gid/.uid instead of .gname/.uname.
+           is True, use .gid/.uid instead of .gname/.uname. If numeric_owner
+           is False, fall back to .gid/.uid when the search based on name
+           fails.
         """
-        if pwd and hasattr(os, "geteuid") and os.geteuid() == 0:
+        if hasattr(os, "geteuid") and os.geteuid() == 0:
             # We have to be root to do so.
-            if numeric_owner:
-                g = tarinfo.gid
-                u = tarinfo.uid
-            else:
+            g = tarinfo.gid
+            u = tarinfo.uid
+            if not numeric_owner:
                 try:
-                    g = grp.getgrnam(tarinfo.gname)[2]
+                    if grp:
+                        g = grp.getgrnam(tarinfo.gname)[2]
                 except KeyError:
-                    g = tarinfo.gid
+                    pass
                 try:
-                    u = pwd.getpwnam(tarinfo.uname)[2]
+                    if pwd:
+                        u = pwd.getpwnam(tarinfo.uname)[2]
                 except KeyError:
-                    u = tarinfo.uid
+                    pass
             try:
                 if tarinfo.issym() and hasattr(os, "lchown"):
                     os.lchown(targetpath, u, g)
index deb1549a28e9a0fc05310908a158863d6b86fd10..ef0d96041fb23291ec1c5be708c34378ab95d291 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -22,6 +22,10 @@ Library
 - Issue #28847: dbm.dumb now supports reading read-only files and no longer
   writes the index file when it is not changed.
 
+- Issue #26937: The chown() method of the tarfile.TarFile class does not fail
+  now when the grp module cannot be imported, as for example on Android
+  platforms.
+
 Windows
 -------