Skip to content

Added is_iterator method and fixes sage to use it. #7398

@nthiery

Description

@nthiery

The following mantra occurs at three places in Sage's code to test whether v is an iterator:

     if hasattr(v, 'next'):

This is not quite correct since some sage objects have a next method without being iterable, or with a different semantic.

Let me quote python's doc:

The iterator objects themselves are required to support the following two methods, which together form the iterator protocol:

iterator.iter()::

 Return the iterator object itself. This is required to allow both containers and iterators to be used with the for and in statements. This method corresponds to the tp_iter slot of the type structure for Python objects in the Python/C API.

iterator.next()::

Return the next item from the container. If there are no further items, raise the StopIteration exception. This method corresponds to the tp_iternext slot of the type structure for Python objects in the Python/C API.

Therefore here is the good way to test if an element is an iterator:

    try:
        return it is iter(it)
    except:
        return False

Note: it is not sufficient to check for the existence of the methods since some sage object implement __iter__ to raise a NotImplemented exception !

Florent

CC: @sagetrac-sage-combinat @williamstein

Component: misc

Keywords: iterators, itertools

Author: Florent Hivert

Reviewer: Nicolas M. Thiéry

Merged: sage-4.2.1.rc0

Issue created by migration from https://trac.sagemath.org/ticket/7398

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions