Opened 5 years ago

Closed 4 years ago

#18538 closed defect (duplicate)

xsrange is not an iterator when called with Python ints or longs

Reported by: tmonteil Owned by:
Priority: major Milestone: sage-duplicate/invalid/wontfix
Component: misc Keywords:
Cc: Merged in:
Authors: Reviewers: Jeroen Demeyer
Report Upstream: N/A Work issues:
Branch: u/tmonteil/xsrange_is_not_an_iterator_when_called_with_python_ints_or_longs (Commits) Commit: 91011192c160bf352906971ca0cd62e6ef18e954
Dependencies: Stopgaps:

Description (last modified by tmonteil)

As reported in this ask question, there is a problem with ellipsis_iter:

sage: r = lambda n: (n-(n%2))//2
sage: for n in range(6):
....:     print([r(n-k) + r(n+k) for k in (-n..n)])
AttributeError: 'xrange' object has no attribute 'next'

This is due to the fact that, while xsrange claims to return an iterator, it does not provide a .next() method when called with Python ints or longs:

sage: xsrange(int(42)).next()
AttributeError: 'xrange' object has no attribute 'next'

This is because in those cases, it returns an xrange object (which is only an iterable).

Change History (15)

comment:1 Changed 5 years ago by tmonteil

  • Branch set to u/tmonteil/xsrange_is_not_an_iterator_when_called_with_python_ints_or_longs

comment:2 Changed 5 years ago by git

  • Commit set to 4a59f332523192b4fd236f3c3e0533049eff9f37

Branch pushed to git repo; I updated commit sha1. New commits:

4a59f33#18538 : remove redundency in linking to trac ticket.

comment:3 Changed 5 years ago by tmonteil

  • Description modified (diff)

comment:4 Changed 5 years ago by git

  • Commit changed from 4a59f332523192b4fd236f3c3e0533049eff9f37 to 91c1b9c81f40b530f0e6b57971abcea5f4f6f301

Branch pushed to git repo; I updated commit sha1. New commits:

91c1b9c#18538 : add the original example as doctest.

comment:5 Changed 5 years ago by tmonteil

  • Status changed from new to needs_review

comment:6 Changed 5 years ago by tmonteil

  • Authors set to Thierry Monteil

comment:7 Changed 5 years ago by aapitzsch

  • Status changed from needs_review to needs_work

Please replace next() method by next() function. See #17398.

comment:8 Changed 5 years ago by git

  • Commit changed from 91c1b9c81f40b530f0e6b57971abcea5f4f6f301 to 91011192c160bf352906971ca0cd62e6ef18e954

Branch pushed to git repo; I updated commit sha1. New commits:

9101119#18538 Python3 compatibility.

comment:9 Changed 5 years ago by tmonteil

  • Status changed from needs_work to needs_review

comment:10 Changed 4 years ago by vdelecroix

Hello,

Why xsrange should have a next method?

sage: next(xrange(10))
Traceback (most recent call last):
...
TypeError: xrange object is not an iterator

I think that we should keep compatibility between xsrange and xrange.

Vincent

comment:11 Changed 4 years ago by jdemeyer

  • Milestone changed from sage-6.8 to sage-6.10
  • Status changed from needs_review to needs_work

I also think it's better to use iter(foo) instead of foo.__iter__().

comment:12 Changed 4 years ago by jdemeyer

This:

``.next()`` or a ``.__next__()`` method

should be replaced by

``.__next__()`` method

But even then, I find it a bit confusing that you talk about a __next__ method and then show two tests not explicitly involving __next__

comment:13 Changed 4 years ago by jdemeyer

  • Milestone changed from sage-6.10 to sage-duplicate/invalid/wontfix
  • Status changed from needs_work to needs_review

Fixed by #20094 instead (which removes the special cases for Python int).

comment:14 Changed 4 years ago by jdemeyer

  • Authors Thierry Monteil deleted
  • Reviewers set to Jeroen Demeyer
  • Status changed from needs_review to positive_review

comment:15 Changed 4 years ago by vbraun

  • Resolution set to duplicate
  • Status changed from positive_review to closed
Note: See TracTickets for help on using tickets.