summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorXavier de Gaye <xdegaye@users.sourceforge.net>2016-12-09 08:33:09 (GMT)
committerXavier de Gaye <xdegaye@users.sourceforge.net>2016-12-09 08:33:09 (GMT)
commitf44abdab1eeb75e2a876e905e3af20b461ab0f6c (patch)
tree6943b71d40e06633b240e6e3bfe059b84101851f /Lib
parent20367420c8b32602a550834fa3fb061a9d9a3b55 (diff)
downloadcpython-f44abdab1eeb75e2a876e905e3af20b461ab0f6c.zip
cpython-f44abdab1eeb75e2a876e905e3af20b461ab0f6c.tar.gz
cpython-f44abdab1eeb75e2a876e905e3af20b461ab0f6c.tar.bz2
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.
Diffstat (limited to 'Lib')
-rwxr-xr-xLib/tarfile.py31
1 files changed, 19 insertions, 12 deletions
diff --git a/Lib/tarfile.py b/Lib/tarfile.py
index b78b1b1..5d4c86c 100755
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -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)