Opened 8 years ago

Closed 5 years ago

#15084 closed defect (duplicate)

xsrange int-overflow

Reported by: dkrenn Owned by:
Priority: major Milestone: sage-duplicate/invalid/wontfix
Component: misc Keywords: xsrange int long overflow
Cc: Merged in:
Authors: Reviewers: Jeroen Demeyer
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Status badges

Description (last modified by dkrenn)

for _ in xsrange(Integer(0), Integer(2^63)):
    pass

gives

Traceback (click to the left of this block for traceback)
...
OverflowError: Python int too large to convert to C long

(clearly, the same error raises if Integer is skipped)

Change History (9)

comment:1 Changed 8 years ago by dkrenn

  • Description modified (diff)

comment:2 Changed 8 years ago by dkrenn

  • Description modified (diff)

comment:3 Changed 8 years ago by nbruin

This is a documented restriction on xrange and sage's code will only run into it in situations where the full iterator would never be exhausted (implementation uses xrange(icount)). Compare:

sage: for _ in xsrange(Integer(2^63-10), Integer(2^63)): pass #no problem
sage: for _ in xrange(Integer(2^63-10), Integer(2^63)): pass
OverflowError: Python int too large to convert to C long

so the question is really whether xsrange should also be usable to create (effectively) infinite arithmetic progressions. Incidentally, xrange and xsrange model different kinds of objects: xrange implements __getitem__ and __iter__ but not next, so it's really just modelling a finite arithmetic progression as (something close to) a list, whereas xsrange is a generator expression, i.e., an iterator:

sage: xrange(2^63-1)[2^33]
8589934592
sage: xsrange(2^63-1)[2^33]
TypeError: 'generator' object has no attribute '__getitem__'

comment:4 Changed 7 years ago by vbraun_spam

  • Milestone changed from sage-6.1 to sage-6.2

comment:5 Changed 7 years ago by vbraun_spam

  • Milestone changed from sage-6.2 to sage-6.3

comment:6 Changed 7 years ago by vbraun_spam

  • Milestone changed from sage-6.3 to sage-6.4

comment:7 Changed 5 years ago by jdemeyer

  • Milestone changed from sage-6.4 to sage-duplicate/invalid/wontfix
  • Reviewers set to Jeroen Demeyer
  • Status changed from new to needs_review

Duplicate of #20094.

comment:8 Changed 5 years ago by jdemeyer

  • Status changed from needs_review to positive_review

comment:9 Changed 5 years ago by vbraun

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