Gentoo Archives: gentoo-portage-dev

From: Zac Medico <zmedico@g.o>
To: gentoo-portage-dev@l.g.o
Cc: Zac Medico <zmedico@g.o>
Subject: [gentoo-portage-dev] [PATCH] bintree.py: fix str() calls for Python 2 (532784)
Date: Wed, 17 Dec 2014 17:36:53
Message-Id: 1418837765-22607-1-git-send-email-zmedico@gentoo.org
1 Avoid a UnicodeDecodeError raised when str(e) converts an exception
2 to bytes with Python 2. Since this file has unicode_literals enabled,
3 use literal unicode format strings to format messages for unicode
4 exceptions. However, with Python 2, an EnvironmentError exception
5 may contain either bytes or unicode, so use _unicode_decode to ensure
6 safety for EnvironmentError with all locales.
7
8 Also, convert remaining str() calls to use _unicode() for uniform
9 behavior regardless of python version.
10
11 X-Gentoo-Bug: 532784
12 X-Gentoo-Bug-URL: https://bugs.gentoo.org/show_bug.cgi?id=532784
13 ---
14 pym/portage/dbapi/bintree.py | 20 +++++++++++---------
15 1 file changed, 11 insertions(+), 9 deletions(-)
16
17 diff --git a/pym/portage/dbapi/bintree.py b/pym/portage/dbapi/bintree.py
18 index d7c7f95..b9098b2 100644
19 --- a/pym/portage/dbapi/bintree.py
20 +++ b/pym/portage/dbapi/bintree.py
21 @@ -391,7 +391,7 @@ class binarytree(object):
22 # sanity check
23 for atom in (origcp, newcp):
24 if not isjustname(atom):
25 - raise InvalidPackageName(str(atom))
26 + raise InvalidPackageName(_unicode(atom))
27 mynewcat = catsplit(newcp)[0]
28 origmatches=self.dbapi.cp_list(origcp)
29 moves = 0
30 @@ -803,8 +803,8 @@ class binarytree(object):
31
32 d["CPV"] = mycpv
33 d["SLOT"] = slot
34 - d["MTIME"] = str(s[stat.ST_MTIME])
35 - d["SIZE"] = str(s.st_size)
36 + d["MTIME"] = _unicode(s[stat.ST_MTIME])
37 + d["SIZE"] = _unicode(s.st_size)
38
39 d.update(zip(self._pkgindex_aux_keys,
40 self.dbapi.aux_get(mycpv, self._pkgindex_aux_keys)))
41 @@ -1024,7 +1024,10 @@ class binarytree(object):
42 except EnvironmentError as e:
43 writemsg(_("\n\n!!! Error fetching binhost package" \
44 " info from '%s'\n") % _hide_url_passwd(base_url))
45 - writemsg("!!! %s\n\n" % str(e))
46 + # With Python 2, the exception message may contain
47 + # bytes or unicode, so use _unicode_decode to ensure
48 + # safety with all locales (bug #532784).
49 + writemsg("!!! %s\n\n" % _unicode_decode(e))
50 del e
51 pkgindex = None
52 if proc is not None:
53 @@ -1242,8 +1245,8 @@ class binarytree(object):
54
55 d["CPV"] = cpv
56 st = os.stat(pkg_path)
57 - d["MTIME"] = str(st[stat.ST_MTIME])
58 - d["SIZE"] = str(st.st_size)
59 + d["MTIME"] = _unicode(st[stat.ST_MTIME])
60 + d["SIZE"] = _unicode(st.st_size)
61
62 rel_path = self._pkg_paths[cpv]
63 # record location if it's non-default
64 @@ -1270,7 +1273,7 @@ class binarytree(object):
65 if profile_path.startswith(profiles_base):
66 profile_path = profile_path[len(profiles_base):]
67 header["PROFILE"] = profile_path
68 - header["VERSION"] = str(self._pkgindex_version)
69 + header["VERSION"] = _unicode(self._pkgindex_version)
70 base_uri = self.settings.get("PORTAGE_BINHOST_HEADER_URI")
71 if base_uri:
72 header["URI"] = base_uri
73 @@ -1316,8 +1319,7 @@ class binarytree(object):
74 deps = use_reduce(deps, uselist=use, token_class=token_class)
75 deps = paren_enclose(deps)
76 except portage.exception.InvalidDependString as e:
77 - writemsg("%s: %s\n" % (k, str(e)),
78 - noiselevel=-1)
79 + writemsg("%s: %s\n" % (k, e), noiselevel=-1)
80 raise
81 metadata[k] = deps
82
83 --
84 2.0.4

Replies