Debian Codegraph @

pypy/module/cpyext/iterator.py

raw symbols SCIP
1 from pypy.interpreter.error import OperationError, oefmt
2 from pypy.module.cpyext.api import (cpython_api, PyObject, PyObjectP,
3 CANNOT_FAIL, cts)
4 import pypy.module.__builtin__.operation as operation
5 from rpython.rtyper.lltypesystem import rffi
6
7 @cpython_api([PyObject, PyObject], PyObject)
8 def PyCallIter_New(space, w_callable, w_sentinel):
9 """Return a new iterator. The first parameter, callable, can be any Python
10 callable object that can be called with no parameters; each call to it should
11 return the next item in the iteration. When callable returns a value equal to
12 sentinel, the iteration will be terminated.
13 """
14 return operation.iter_sentinel(space, w_callable, w_sentinel)
15
16 @cpython_api([PyObject], PyObject)
17 def PyObject_GetIter(space, w_obj):
18 """This is equivalent to the Python expression iter(o). It returns a new
19 iterator for the object argument, or the object itself if the object is
20 already an iterator. Raises TypeError and returns NULL if the object
21 cannot be iterated."""
22 return space.iter(w_obj)
23
24 @cpython_api([PyObject], PyObject)
25 def PyIter_Next(space, w_obj):
26 """Return the next value from the iteration o. If the object is an
27 iterator, this retrieves the next value from the iteration, and returns
28 NULL with no exception set if there are no remaining items. If the object
29 is not an iterator, TypeError is raised, or if there is an error in
30 retrieving the item, returns NULL and passes along the exception."""
31 try:
32 return space.next(w_obj)
33 except OperationError as e:
34 if not e.match(space, space.w_StopIteration):
35 raise
36 return None
37
38 @cpython_api([PyObject, PyObject, PyObjectP], rffi.INT_real, error=-1)
39 def PyIter_Send(space, w_iter, w_arg, presult):
40 presult[0] = rffi.cast(PyObject, 0)
41 raise oefmt(space.w_NotImplementedError,
42 "PyIter_Send not implemented yet")
43 return -1
44
45 @cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL)
46 def PyIter_Check(space, w_obj):
47 """Return true if the object o supports the iterator protocol."""
48 try:
49 w_attr = space.getattr(space.type(w_obj), space.newtext("__next__"))
50 except:
51 return False
52 else:
53 return space.is_true(space.callable(w_attr))