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 v2] bintree.py: fix str() calls for Python 2 (532784)
Date: Wed, 17 Dec 2014 17:59:35
Message-Id: 1418839162-23865-1-git-send-email-zmedico@gentoo.org
In Reply to: [gentoo-portage-dev] [PATCH] bintree.py: fix str() calls for Python 2 (532784) by Zac Medico
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(errors="replace")
6 to ensure 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 PATCH v2 replaces the _unicode_decode call with
15 _unicode(errors="replace"), which is required in order to force the
16 EnvironmentError object to be converted to a unicode string.
17
18 pym/portage/dbapi/bintree.py | 21 ++++++++++++---------
19 1 file changed, 12 insertions(+), 9 deletions(-)
20
21 diff --git a/pym/portage/dbapi/bintree.py b/pym/portage/dbapi/bintree.py
22 index d7c7f95..1156b66 100644
23 --- a/pym/portage/dbapi/bintree.py
24 +++ b/pym/portage/dbapi/bintree.py
25 @@ -391,7 +391,7 @@ class binarytree(object):
26 # sanity check
27 for atom in (origcp, newcp):
28 if not isjustname(atom):
29 - raise InvalidPackageName(str(atom))
30 + raise InvalidPackageName(_unicode(atom))
31 mynewcat = catsplit(newcp)[0]
32 origmatches=self.dbapi.cp_list(origcp)
33 moves = 0
34 @@ -803,8 +803,8 @@ class binarytree(object):
35
36 d["CPV"] = mycpv
37 d["SLOT"] = slot
38 - d["MTIME"] = str(s[stat.ST_MTIME])
39 - d["SIZE"] = str(s.st_size)
40 + d["MTIME"] = _unicode(s[stat.ST_MTIME])
41 + d["SIZE"] = _unicode(s.st_size)
42
43 d.update(zip(self._pkgindex_aux_keys,
44 self.dbapi.aux_get(mycpv, self._pkgindex_aux_keys)))
45 @@ -1024,7 +1024,11 @@ class binarytree(object):
46 except EnvironmentError as e:
47 writemsg(_("\n\n!!! Error fetching binhost package" \
48 " info from '%s'\n") % _hide_url_passwd(base_url))
49 - writemsg("!!! %s\n\n" % str(e))
50 + # With Python 2, the EnvironmentError message may
51 + # contain bytes or unicode, so use _unicode to ensure
52 + # safety with all locales (bug #532784).
53 + writemsg("!!! %s\n\n" % _unicode(e,
54 + _encodings["stdio"], errors="replace"))
55 del e
56 pkgindex = None
57 if proc is not None:
58 @@ -1242,8 +1246,8 @@ class binarytree(object):
59
60 d["CPV"] = cpv
61 st = os.stat(pkg_path)
62 - d["MTIME"] = str(st[stat.ST_MTIME])
63 - d["SIZE"] = str(st.st_size)
64 + d["MTIME"] = _unicode(st[stat.ST_MTIME])
65 + d["SIZE"] = _unicode(st.st_size)
66
67 rel_path = self._pkg_paths[cpv]
68 # record location if it's non-default
69 @@ -1270,7 +1274,7 @@ class binarytree(object):
70 if profile_path.startswith(profiles_base):
71 profile_path = profile_path[len(profiles_base):]
72 header["PROFILE"] = profile_path
73 - header["VERSION"] = str(self._pkgindex_version)
74 + header["VERSION"] = _unicode(self._pkgindex_version)
75 base_uri = self.settings.get("PORTAGE_BINHOST_HEADER_URI")
76 if base_uri:
77 header["URI"] = base_uri
78 @@ -1316,8 +1320,7 @@ class binarytree(object):
79 deps = use_reduce(deps, uselist=use, token_class=token_class)
80 deps = paren_enclose(deps)
81 except portage.exception.InvalidDependString as e:
82 - writemsg("%s: %s\n" % (k, str(e)),
83 - noiselevel=-1)
84 + writemsg("%s: %s\n" % (k, e), noiselevel=-1)
85 raise
86 metadata[k] = deps
87
88 --
89 2.0.4

Replies