Patchwork [py3,V2] revlog: explicitly evaluate list to return

login
register
mail settings
Submitter Pulkit Goyal
Date March 16, 2017, 9:51 p.m.
Message ID <16e1d9557dcebfe82706.1489701109@pulkit-goyal>
Download mbox | patch
Permalink /patch/19403/
State Changes Requested
Headers show

Comments

Pulkit Goyal - March 16, 2017, 9:51 p.m.
# HG changeset patch
# User Pulkit Goyal <7895pulkit@gmail.com>
# Date 1489639281 -19800
#      Thu Mar 16 10:11:21 2017 +0530
# Node ID 16e1d9557dcebfe827060a4d31781cb7498c2c45
# Parent  e082ef765e4112ef69d3345369b142c87f1da3f4
revlog: explicitly evaluate list to return

On python 3, map returns an iterator so we need to convert it to list before
finding the len or subscripting it.

After this `hg status --all` works on Python 3.
Gregory Szorc - March 16, 2017, 10:34 p.m.
On Thu, Mar 16, 2017 at 2:51 PM, Pulkit Goyal <7895pulkit@gmail.com> wrote:

> # HG changeset patch
> # User Pulkit Goyal <7895pulkit@gmail.com>
> # Date 1489639281 -19800
> #      Thu Mar 16 10:11:21 2017 +0530
> # Node ID 16e1d9557dcebfe827060a4d31781cb7498c2c45
> # Parent  e082ef765e4112ef69d3345369b142c87f1da3f4
> revlog: explicitly evaluate list to return
>
> On python 3, map returns an iterator so we need to convert it to list
> before
> finding the len or subscripting it.
>
> After this `hg status --all` works on Python 3.
>
> diff -r e082ef765e41 -r 16e1d9557dce mercurial/revlog.py
> --- a/mercurial/revlog.py       Thu Mar 16 10:01:12 2017 +0530
> +++ b/mercurial/revlog.py       Thu Mar 16 10:11:21 2017 +0530
> @@ -943,7 +943,7 @@
>              ancs = self.index.commonancestorsheads(a, b)
>          except (AttributeError, OverflowError): # C implementation failed
>              ancs = ancestor.commonancestorsheads(self.parentrevs, a, b)
> -        return map(self.node, ancs)
> +        return list(map(self.node, ancs))
>
>
This will create a list copy in Python 2, which could have performance
implications. But for this function, the list shouldn't have many elements,
so it is probably OK. Something to watch out for when doing other porting
work. It /might/ be worthwhile to have a pycompat.maplist() wrapper or some
such.


>      def isancestor(self, a, b):
>          """return True if node a is an ancestor of node b
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
>
Yuya Nishihara - March 17, 2017, 12:36 p.m.
On Thu, 16 Mar 2017 15:34:11 -0700, Gregory Szorc wrote:
> On Thu, Mar 16, 2017 at 2:51 PM, Pulkit Goyal <7895pulkit@gmail.com> wrote:
> 
> > # HG changeset patch
> > # User Pulkit Goyal <7895pulkit@gmail.com>
> > # Date 1489639281 -19800
> > #      Thu Mar 16 10:11:21 2017 +0530
> > # Node ID 16e1d9557dcebfe827060a4d31781cb7498c2c45
> > # Parent  e082ef765e4112ef69d3345369b142c87f1da3f4
> > revlog: explicitly evaluate list to return
> >
> > On python 3, map returns an iterator so we need to convert it to list
> > before
> > finding the len or subscripting it.
> >
> > After this `hg status --all` works on Python 3.
> >
> > diff -r e082ef765e41 -r 16e1d9557dce mercurial/revlog.py
> > --- a/mercurial/revlog.py       Thu Mar 16 10:01:12 2017 +0530
> > +++ b/mercurial/revlog.py       Thu Mar 16 10:11:21 2017 +0530
> > @@ -943,7 +943,7 @@
> >              ancs = self.index.commonancestorsheads(a, b)
> >          except (AttributeError, OverflowError): # C implementation failed
> >              ancs = ancestor.commonancestorsheads(self.parentrevs, a, b)
> > -        return map(self.node, ancs)
> > +        return list(map(self.node, ancs))
> >
> >
> This will create a list copy in Python 2, which could have performance
> implications. But for this function, the list shouldn't have many elements,
> so it is probably OK. Something to watch out for when doing other porting
> work. It /might/ be worthwhile to have a pycompat.maplist() wrapper or some
> such.

+1 for pycompat function. It's also useful for grepping py3 workarounds.

Patch

diff -r e082ef765e41 -r 16e1d9557dce mercurial/revlog.py
--- a/mercurial/revlog.py	Thu Mar 16 10:01:12 2017 +0530
+++ b/mercurial/revlog.py	Thu Mar 16 10:11:21 2017 +0530
@@ -943,7 +943,7 @@ 
             ancs = self.index.commonancestorsheads(a, b)
         except (AttributeError, OverflowError): # C implementation failed
             ancs = ancestor.commonancestorsheads(self.parentrevs, a, b)
-        return map(self.node, ancs)
+        return list(map(self.node, ancs))
 
     def isancestor(self, a, b):
         """return True if node a is an ancestor of node b