Opened 4 years ago

Closed 4 years ago

#18378 closed defect (fixed)

list(ZZ) --> infinite loop

Reported by: ncohen Owned by:
Priority: major Milestone: sage-6.7
Component: basic arithmetic Keywords:
Cc: vdelecroix Merged in:
Authors: Nathann Cohen Reviewers: Jean-Pierre Flori
Report Upstream: N/A Work issues:
Branch: 47cec14 (Commits) Commit: 47cec14873eae9f5371b31d41abd912b36c58595
Dependencies: Stopgaps:

Description (last modified by ncohen)

For some reason, list(something) breaks if __len__ raises a NotImplementedError, but a TypeError is ignored.

Python....

As len(NN) already raises a NotImplementedError, I just copied the behaviour.

Nathann

Change History (9)

comment:1 Changed 4 years ago by ncohen

  • Branch set to public/18378
  • Status changed from new to needs_review

comment:2 Changed 4 years ago by git

  • Commit set to 9ba72b347c17cf60cd7d5f0f7cda9185c33f9fa0

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

9ba72b3trac #18378: list(ZZ) --> infinite loop

comment:3 Changed 4 years ago by ncohen

  • Description modified (diff)

comment:4 follow-up: Changed 4 years ago by nbruin

For posterity, the reason that TypeError gets caught is to make this work:

sage: l=(i for i in range(10))
sage: len(l)
TypeError: object of type 'generator' has no len()
sage: list(l)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

It may well be an accident that list checks len of its argument before it checks if it's iterable, so I would not consider changing the error type a reliable way to prevent an infinite loop. I don't think python officially supports a way of preventing list(<infinite iterable>) from going in an infinite loop. It works, though...

comment:5 in reply to: ↑ 4 Changed 4 years ago by ncohen

It may well be an accident that list checks len of its argument before it checks if it's iterable, so I would not consider changing the error type a reliable way to prevent an infinite loop. I don't think python officially supports a way of preventing list(<infinite iterable>) from going in an infinite loop.

Oh I see. Thanks ;-)

comment:6 Changed 4 years ago by git

  • Commit changed from 9ba72b347c17cf60cd7d5f0f7cda9185c33f9fa0 to 47cec14873eae9f5371b31d41abd912b36c58595

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

47cec14trac #18378: list(ZZ) --> infinite loop

comment:7 Changed 4 years ago by jpflori

  • Reviewers set to Jean-Pierre Flori
  • Status changed from needs_review to positive_review

Looks good enough to me, especially if NN already behaves like this.

comment:8 Changed 4 years ago by ncohen

Thanks !

Nathann

comment:9 Changed 4 years ago by vbraun

  • Branch changed from public/18378 to 47cec14873eae9f5371b31d41abd912b36c58595
  • Resolution set to fixed
  • Status changed from positive_review to closed
Note: See TracTickets for help on using tickets.