Mercurial > hg-stable
changeset 43104:74802979dd9d
py3: define and use pycompat.itervalues()
.itervalues() only exists on Python 2. Python 3's equivalent is
.values(). But we don't want to blindly use .values() everywhere
because on Python 2, it will create a list, which will have performance
implications.
This commit introduces pycompat.itervalues() which will call the appropriate
method on the passed object. We update all callers of obj.itervalues()
to pycompat.itervalues(obj) instead.
With this commit, the only source tranforming remaining is for
iteritems(). Victory is near...
Differential Revision: https://phab.mercurial-scm.org/D7013
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Sun, 06 Oct 2019 17:59:15 -0400 |
parents | c95b2f40db7c |
children | 649d3ac37a12 |
files | contrib/synthrepo.py hgext/journal.py hgext/rebase.py hgext/remotefilelog/connectionpool.py hgext/remotefilelog/debugcommands.py hgext/remotefilelog/repack.py hgext/transplant.py mercurial/__init__.py mercurial/branchmap.py mercurial/exchangev2.py mercurial/localrepo.py mercurial/merge.py mercurial/patch.py mercurial/pycompat.py mercurial/statprof.py mercurial/ui.py tests/test-pathencode.py |
diffstat | 17 files changed, 32 insertions(+), 23 deletions(-) [+] |
line wrap: on
line diff
--- a/contrib/synthrepo.py Sun Oct 06 17:45:05 2019 -0400 +++ b/contrib/synthrepo.py Sun Oct 06 17:59:15 2019 -0400 @@ -213,7 +213,7 @@ for filename, mar, lineadd, lineremove, isbin in parsegitdiff(diff): if isbin: continue - added = sum(lineadd.itervalues(), 0) + added = sum(pycompat.itervalues(lineadd), 0) if mar == 'm': if added and lineremove: lineschanged[
--- a/hgext/journal.py Sun Oct 06 17:45:05 2019 -0400 +++ b/hgext/journal.py Sun Oct 06 17:59:15 2019 -0400 @@ -164,7 +164,7 @@ pass while iterable_map: - value, key, it = order(iterable_map.itervalues()) + value, key, it = order(pycompat.itervalues(iterable_map)) yield value try: iterable_map[key][0] = next(it)
--- a/hgext/rebase.py Sun Oct 06 17:45:05 2019 -0400 +++ b/hgext/rebase.py Sun Oct 06 17:59:15 2019 -0400 @@ -2288,7 +2288,7 @@ msg = _(b'rebase: (use "hg rebase --abort" to clear broken state)\n') ui.write(msg) return - numrebased = len([i for i in state.itervalues() if i >= 0]) + numrebased = len([i for i in pycompat.itervalues(state) if i >= 0]) # i18n: column positioning for "hg summary" ui.write( _(b'rebase: %s, %s (rebase --continue)\n')
--- a/hgext/remotefilelog/connectionpool.py Sun Oct 06 17:45:05 2019 -0400 +++ b/hgext/remotefilelog/connectionpool.py Sun Oct 06 17:59:15 2019 -0400 @@ -10,6 +10,7 @@ from mercurial import ( extensions, hg, + pycompat, sshpeer, util, ) @@ -59,7 +60,7 @@ return conn def close(self): - for pathpool in self._pool.itervalues(): + for pathpool in pycompat.itervalues(self._pool): for conn in pathpool: conn.close() del pathpool[:]
--- a/hgext/remotefilelog/debugcommands.py Sun Oct 06 17:45:05 2019 -0400 +++ b/hgext/remotefilelog/debugcommands.py Sun Oct 06 17:59:15 2019 -0400 @@ -207,7 +207,7 @@ continue filepath = os.path.join(root, file) size, firstnode, mapping = parsefileblob(filepath, decompress) - for p1, p2, linknode, copyfrom in mapping.itervalues(): + for p1, p2, linknode, copyfrom in pycompat.itervalues(mapping): if linknode == nullid: actualpath = os.path.relpath(root, path) key = fileserverclient.getcachekey(
--- a/hgext/remotefilelog/repack.py Sun Oct 06 17:45:05 2019 -0400 +++ b/hgext/remotefilelog/repack.py Sun Oct 06 17:59:15 2019 -0400 @@ -598,7 +598,7 @@ maxchainlen = ui.configint(b'packs', b'maxchainlen', 1000) byfile = {} - for entry in ledger.entries.itervalues(): + for entry in pycompat.itervalues(ledger.entries): if entry.datasource: byfile.setdefault(entry.filename, {})[entry.node] = entry @@ -753,7 +753,7 @@ ui = self.repo.ui byfile = {} - for entry in ledger.entries.itervalues(): + for entry in pycompat.itervalues(ledger.entries): if entry.historysource: byfile.setdefault(entry.filename, {})[entry.node] = entry
--- a/hgext/transplant.py Sun Oct 06 17:45:05 2019 -0400 +++ b/hgext/transplant.py Sun Oct 06 17:59:15 2019 -0400 @@ -100,7 +100,7 @@ if not os.path.isdir(self.path): os.mkdir(self.path) fp = self.opener(self.transplantfile, b'w') - for list in self.transplants.itervalues(): + for list in pycompat.itervalues(self.transplants): for t in list: l, r = map(nodemod.hex, (t.lnode, t.rnode)) fp.write(l + b':' + r + b'\n')
--- a/mercurial/__init__.py Sun Oct 06 17:45:05 2019 -0400 +++ b/mercurial/__init__.py Sun Oct 06 17:59:15 2019 -0400 @@ -117,7 +117,7 @@ # It changes iteritems/values to items/values as they are not # present in Python 3 world. - if fn in ('iteritems', 'itervalues') and not ( + if fn == 'iteritems' and not ( tokens[i - 1].type == token.NAME and tokens[i - 1].string == 'def' ): @@ -131,7 +131,7 @@ # ``replacetoken`` or any mechanism that changes semantics of module # loading is changed. Otherwise cached bytecode may get loaded without # the new transformation mechanisms applied. - BYTECODEHEADER = b'HG\x00\x14' + BYTECODEHEADER = b'HG\x00\x15' class hgloader(importlib.machinery.SourceFileLoader): """Custom module loader that transforms source code.
--- a/mercurial/branchmap.py Sun Oct 06 17:45:05 2019 -0400 +++ b/mercurial/branchmap.py Sun Oct 06 17:59:15 2019 -0400 @@ -91,7 +91,7 @@ clbranchinfo = cl.branchinfo rbheads = [] closed = [] - for bheads in remotebranchmap.itervalues(): + for bheads in pycompat.itervalues(remotebranchmap): rbheads += bheads for h in bheads: r = clrev(h) @@ -350,7 +350,7 @@ def iterheads(self): """ returns all the heads """ self._verifyall() - return self._entries.itervalues() + return pycompat.itervalues(self._entries) def copy(self): """return an deep copy of the branchcache object"""
--- a/mercurial/exchangev2.py Sun Oct 06 17:45:05 2019 -0400 +++ b/mercurial/exchangev2.py Sun Oct 06 17:59:15 2019 -0400 @@ -607,7 +607,7 @@ progress = repo.ui.makeprogress( _(b'files'), unit=_(b'chunks'), - total=sum(len(v) for v in fnodes.itervalues()), + total=sum(len(v) for v in pycompat.itervalues(fnodes)), ) # TODO make batch size configurable @@ -706,7 +706,7 @@ progress = repo.ui.makeprogress( _(b'files'), unit=_(b'chunks'), - total=sum(len(v) for v in fnodes.itervalues()), + total=sum(len(v) for v in pycompat.itervalues(fnodes)), ) commandmeta = remote.apidescriptor[b'commands'][b'filesdata']
--- a/mercurial/localrepo.py Sun Oct 06 17:45:05 2019 -0400 +++ b/mercurial/localrepo.py Sun Oct 06 17:59:15 2019 -0400 @@ -1763,7 +1763,7 @@ nodetagscache = {} for t, n in self._tagscache.tags.iteritems(): nodetagscache.setdefault(n, []).append(t) - for tags in nodetagscache.itervalues(): + for tags in pycompat.itervalues(nodetagscache): tags.sort() self._tagscache.nodetagscache = nodetagscache return self._tagscache.nodetagscache.get(node, [])
--- a/mercurial/merge.py Sun Oct 06 17:45:05 2019 -0400 +++ b/mercurial/merge.py Sun Oct 06 17:59:15 2019 -0400 @@ -702,7 +702,7 @@ """return counts for updated, merged and removed files in this session""" updated, merged, removed = 0, 0, 0 - for r, action in self._results.itervalues(): + for r, action in pycompat.itervalues(self._results): if r is None: updated += 1 elif r == 0:
--- a/mercurial/patch.py Sun Oct 06 17:45:05 2019 -0400 +++ b/mercurial/patch.py Sun Oct 06 17:59:15 2019 -0400 @@ -1331,7 +1331,11 @@ fixoffset += chunk.removed - chunk.added return ( sum( - [h for h in applied.itervalues() if h[0].special() or len(h) > 1], + [ + h + for h in pycompat.itervalues(applied) + if h[0].special() or len(h) > 1 + ], [], ), {},
--- a/mercurial/pycompat.py Sun Oct 06 17:45:05 2019 -0400 +++ b/mercurial/pycompat.py Sun Oct 06 17:59:15 2019 -0400 @@ -337,6 +337,7 @@ ret = shlex.split(s.decode('latin-1'), comments, posix) return [a.encode('latin-1') for a in ret] + itervalues = lambda x: x.values() else: import cStringIO @@ -413,6 +414,7 @@ ziplist = zip rawinput = raw_input getargspec = inspect.getargspec + itervalues = lambda x: x.itervalues() isjython = sysplatform.startswith(b'java')
--- a/mercurial/statprof.py Sun Oct 06 17:45:05 2019 -0400 +++ b/mercurial/statprof.py Sun Oct 06 17:59:15 2019 -0400 @@ -475,7 +475,7 @@ if i == 0: sitestat.addself() - return [s for s in stats.itervalues()] + return [s for s in pycompat.itervalues(stats)] class DisplayFormats: @@ -744,7 +744,7 @@ site = node.site visiblechildren = [ c - for c in node.children.itervalues() + for c in pycompat.itervalues(node.children) if c.count >= (limit * root.count) ] if site: @@ -752,7 +752,7 @@ filename = b'' function = b'' if len(node.children) > 0: - childsite = list(node.children.itervalues())[0].site + childsite = list(pycompat.itervalues(node.children))[0].site filename = (childsite.filename() + b':').ljust(15) function = childsite.function @@ -777,7 +777,9 @@ ) finalstring = liststring + codestring - childrensamples = sum([c.count for c in node.children.itervalues()]) + childrensamples = sum( + [c.count for c in pycompat.itervalues(node.children)] + ) # Make frames that performed more than 10% of the operation red if node.count - childrensamples > (0.1 * root.count): finalstring = b'\033[91m' + finalstring + b'\033[0m'
--- a/mercurial/ui.py Sun Oct 06 17:45:05 2019 -0400 +++ b/mercurial/ui.py Sun Oct 06 17:59:15 2019 -0400 @@ -1966,7 +1966,7 @@ if not self._loggers: return activeloggers = [ - l for l in self._loggers.itervalues() if l.tracked(event) + l for l in pycompat.itervalues(self._loggers) if l.tracked(event) ] if not activeloggers: return
--- a/tests/test-pathencode.py Sun Oct 06 17:45:05 2019 -0400 +++ b/tests/test-pathencode.py Sun Oct 06 17:59:15 2019 -0400 @@ -67,7 +67,7 @@ counts[c] += 1 for c in '\r/\n': counts.pop(c, None) - t = sum(counts.itervalues()) / 100.0 + t = sum(pycompat.itervalues(counts)) / 100.0 fp.write('probtable = (') for i, (k, v) in enumerate( sorted(counts.items(), key=lambda x: x[1], reverse=True)