[Python-bugs-list] [ python-Bugs-564601 ] patch 551410 broke xrange

noreply@sourceforge.net noreply@sourceforge.net
Wed, 05 Jun 2002 09:25:42 -0700


Bugs item #564601, was opened at 2002-06-04 16:31
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=564601&group_id=5470

Category: Python Interpreter Core
Group: Python 2.3
Status: Open
Resolution: None
Priority: 7
Submitted By: Guido van Rossum (gvanrossum)
Assigned to: Raymond Hettinger (rhettinger)
Summary: patch 551410 broke xrange

Initial Comment:
Patch 551410 is broken.

>>> a = iter(xrange(10))
>>> for i in a:
...     print i    
...     if i == 4: print '*', a.next()
... 
0
1
2
3
4
* 0
5
6
7
8
9
>>>

Compare to:

>>> a = iter(range(10))
>>> for i in a:
...     print i
...     if i == 4: print '*', a.next()
... 
0
1
2
3
4
* 5
6
7
8
9
>>> 


----------------------------------------------------------------------

>Comment By: Raymond Hettinger (rhettinger)
Date: 2002-06-05 11:25

Message:
Logged In: YES 
user_id=80475

Deleting patch 1.

Patch 2 matchs Py2.2 behavior for all the tests we've 
discussed and matches the object creation behavior of 
range().

----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2002-06-05 02:24

Message:
Logged In: YES 
user_id=21627

Your patch 1 does not work:

r = xrange(3)
for i in r: print i,
for i in r: print i,

ought to print
0 1 2 0 1 2
but with your patch, it prints
0 1 2
Notice that the used flag was introduced precisely to allow
reuse of the range object in multiple loops.

So I guess the rangeiterobject is the way to go, but I
haven't reviewed the patch, yet.

----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2002-06-05 00:54

Message:
Logged In: YES 
user_id=80475

Added alternative patch with full-blown rangeiterobject just 
like listobject.c.  Given x=xrange(n), the patch assures that 
id(iter(iter(x))) == id(iter(x)) and assures that id(iter(x))!=id
(x) so that xrange() is identical in behavior to range() in all 
iteration aspects.

----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2002-06-04 23:48

Message:
Logged In: YES 
user_id=80475

See attached patch.
Given x=xrange(n), the patch assures that id(iter(iter(x))) 
== id(iter(x)).

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=564601&group_id=5470