[Python-checkins] cpython (merge 3.6 -> default): Issue #26937: Merge 3.6.

xavier.degaye python-checkins at python.org
Fri Dec 9 03:36:25 EST 2016


https://hg.python.org/cpython/rev/da510d1aa683
changeset:   105545:da510d1aa683
parent:      105543:adcd9131b7c6
parent:      105544:e4e7bc640865
user:        Xavier de Gaye <xdegaye at users.sourceforge.net>
date:        Fri Dec 09 09:35:49 2016 +0100
summary:
  Issue #26937: Merge 3.6.

files:
  Lib/tarfile.py |  31 +++++++++++++++++++------------
  Misc/NEWS      |   4 ++++
  2 files changed, 23 insertions(+), 12 deletions(-)


diff --git a/Lib/tarfile.py b/Lib/tarfile.py
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -50,9 +50,13 @@
 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 @@
 
     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)
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -168,6 +168,10 @@
 Library
 -------
 
+- 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.
+
 - Issue #28847: dbm.dumb now supports reading read-only files and no longer
   writes the index file when it is not changed.  A deprecation warning is now
   emitted if the index file is missed and recreated in the 'r' and 'w' modes

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list