Patchwork [3,of,7] pycompat: alias xrange to range in py3

login
register
mail settings
Submitter timeless@mozdev.org
Date April 8, 2016, 4:22 p.m.
Message ID <cbae00f13ee7afa9dfee.1460132567@waste.org>
Download mbox | patch
Permalink /patch/14440/
State Accepted
Headers show

Comments

timeless@mozdev.org - April 8, 2016, 4:22 p.m.
# HG changeset patch
# User timeless <timeless@mozdev.org>
# Date 1459982152 0
#      Wed Apr 06 22:35:52 2016 +0000
# Node ID cbae00f13ee7afa9dfee577a325ee31d46a84a59
# Parent  295d79e7d8dd3da8e9ce33611916574d7ddc0ad8
pycompat: alias xrange to range in py3
Yuya Nishihara - April 9, 2016, 2:39 a.m.
On Fri, 08 Apr 2016 11:22:47 -0500, timeless wrote:
> # HG changeset patch
> # User timeless <timeless@mozdev.org>
> # Date 1459982152 0
> #      Wed Apr 06 22:35:52 2016 +0000
> # Node ID cbae00f13ee7afa9dfee577a325ee31d46a84a59
> # Parent  295d79e7d8dd3da8e9ce33611916574d7ddc0ad8
> pycompat: alias xrange to range in py3
> 
> diff --git a/mercurial/pycompat.py b/mercurial/pycompat.py
> --- a/mercurial/pycompat.py
> +++ b/mercurial/pycompat.py
> @@ -17,3 +17,9 @@
>      import queue as _queue
>  empty = _queue.Empty
>  queue = _queue.Queue
> +
> +try:
> +    xrange
> +except NameError:
> +    import builtins
> +    builtins.xrange = range

Import-time side effect works badly with demandimport and static code analysis.
All users of xrange have to a) import util (or pycompat) even if they never
use util.xxx, and b) bypass demand-loading of util to execute this code.
Gregory Szorc - April 9, 2016, 3:28 a.m.
> On Apr 8, 2016, at 09:22, timeless <timeless@mozdev.org> wrote:
> 
> # HG changeset patch
> # User timeless <timeless@mozdev.org>
> # Date 1459982152 0
> #      Wed Apr 06 22:35:52 2016 +0000
> # Node ID cbae00f13ee7afa9dfee577a325ee31d46a84a59
> # Parent  295d79e7d8dd3da8e9ce33611916574d7ddc0ad8
> pycompat: alias xrange to range in py3
> 
> diff --git a/mercurial/pycompat.py b/mercurial/pycompat.py
> --- a/mercurial/pycompat.py
> +++ b/mercurial/pycompat.py
> @@ -17,3 +17,9 @@
>     import queue as _queue
> empty = _queue.Empty
> queue = _queue.Queue
> +
> +try:
> +    xrange
> +except NameError:
> +    import builtins
> +    builtins.xrange = range

I'm really not a fan of mucking around with the standard library. I'd rather we use pycompat.xrange (or similar) everywhere (or at least cases where we need a generator for perf reasons).
timeless - April 10, 2016, 1:54 a.m.
mpm suggested this approach.

On Fri, Apr 8, 2016 at 11:28 PM, Gregory Szorc <gregory.szorc@gmail.com> wrote:
>
>
>> On Apr 8, 2016, at 09:22, timeless <timeless@mozdev.org> wrote:
>>
>> # HG changeset patch
>> # User timeless <timeless@mozdev.org>
>> # Date 1459982152 0
>> #      Wed Apr 06 22:35:52 2016 +0000
>> # Node ID cbae00f13ee7afa9dfee577a325ee31d46a84a59
>> # Parent  295d79e7d8dd3da8e9ce33611916574d7ddc0ad8
>> pycompat: alias xrange to range in py3
>>
>> diff --git a/mercurial/pycompat.py b/mercurial/pycompat.py
>> --- a/mercurial/pycompat.py
>> +++ b/mercurial/pycompat.py
>> @@ -17,3 +17,9 @@
>>     import queue as _queue
>> empty = _queue.Empty
>> queue = _queue.Queue
>> +
>> +try:
>> +    xrange
>> +except NameError:
>> +    import builtins
>> +    builtins.xrange = range
>
> I'm really not a fan of mucking around with the standard library. I'd rather we use pycompat.xrange (or similar) everywhere (or at least cases where we need a generator for perf reasons).
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
Gregory Szorc - April 10, 2016, 2:37 a.m.
> On Apr 9, 2016, at 18:54, timeless <timeless@gmail.com> wrote:
> 
> mpm suggested this approach.

I know. I disagree with mpm. But I understand where he's coming from.

> 
>> On Fri, Apr 8, 2016 at 11:28 PM, Gregory Szorc <gregory.szorc@gmail.com> wrote:
>> 
>> 
>>> On Apr 8, 2016, at 09:22, timeless <timeless@mozdev.org> wrote:
>>> 
>>> # HG changeset patch
>>> # User timeless <timeless@mozdev.org>
>>> # Date 1459982152 0
>>> #      Wed Apr 06 22:35:52 2016 +0000
>>> # Node ID cbae00f13ee7afa9dfee577a325ee31d46a84a59
>>> # Parent  295d79e7d8dd3da8e9ce33611916574d7ddc0ad8
>>> pycompat: alias xrange to range in py3
>>> 
>>> diff --git a/mercurial/pycompat.py b/mercurial/pycompat.py
>>> --- a/mercurial/pycompat.py
>>> +++ b/mercurial/pycompat.py
>>> @@ -17,3 +17,9 @@
>>>    import queue as _queue
>>> empty = _queue.Empty
>>> queue = _queue.Queue
>>> +
>>> +try:
>>> +    xrange
>>> +except NameError:
>>> +    import builtins
>>> +    builtins.xrange = range
>> 
>> I'm really not a fan of mucking around with the standard library. I'd rather we use pycompat.xrange (or similar) everywhere (or at least cases where we need a generator for perf reasons).
>> _______________________________________________
>> Mercurial-devel mailing list
>> Mercurial-devel@mercurial-scm.org
>> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel

Patch

diff --git a/mercurial/pycompat.py b/mercurial/pycompat.py
--- a/mercurial/pycompat.py
+++ b/mercurial/pycompat.py
@@ -17,3 +17,9 @@ 
     import queue as _queue
 empty = _queue.Empty
 queue = _queue.Queue
+
+try:
+    xrange
+except NameError:
+    import builtins
+    builtins.xrange = range