| 1 |
import pytest |
| 2 |
from pypy.interpreter.error import OperationError |
| 3 |
from pypy.module.cpyext.test.test_api import BaseApiTest |
| 4 |
from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase |
| 5 |
from pypy.module.cpyext.iterator import PyIter_Next |
| 6 |
|
| 7 |
|
| 8 |
class TestIterator(BaseApiTest): |
| 9 |
def test_check_iter(self, space, api): |
| 10 |
assert api.PyIter_Check(space.iter(space.wrap("a"))) |
| 11 |
assert api.PyIter_Check(space.iter(space.newlist([]))) |
| 12 |
assert not api.PyIter_Check(space.w_type) |
| 13 |
assert not api.PyIter_Check(space.wrap(2)) |
| 14 |
|
| 15 |
def test_getIter(self, space, api): |
| 16 |
w_iter = api.PyObject_GetIter(space.wrap([1, 2, 3])) |
| 17 |
assert space.unwrap(api.PyIter_Next(w_iter)) == 1 |
| 18 |
assert space.unwrap(api.PyIter_Next(w_iter)) == 2 |
| 19 |
assert space.unwrap(api.PyIter_Next(w_iter)) == 3 |
| 20 |
assert PyIter_Next(space, w_iter) is None |
| 21 |
|
| 22 |
def test_iternext_error(self, space): |
| 23 |
with pytest.raises(OperationError) as excinfo: |
| 24 |
PyIter_Next(space, space.w_None) |
| 25 |
assert excinfo.value.w_type is space.w_TypeError |
| 26 |
|
| 27 |
|
| 28 |
def test_reversed(self, space, api): |
| 29 |
arg = space.wrap([1, 2, 3]) |
| 30 |
rev = api.PyObject_Call(api.PyReversed_Type, space.newtuple([arg]), None) |
| 31 |
app_rev = [space.int_w(x) for x in space.listview(rev)] |
| 32 |
assert app_rev == [3, 2, 1] |
| 33 |
|
| 34 |
class AppTestIterator(AppTestCpythonExtensionBase): |
| 35 |
def test_noniterable_object_with_mapping_interface(self): |
| 36 |
module = self.import_extension('foo', [ |
| 37 |
("test", "METH_NOARGS", |
| 38 |
''' |
| 39 |
PyObject *obj; |
| 40 |
obj = PyObject_New(PyObject, &Foo_Type); |
| 41 |
return obj; |
| 42 |
''' |
| 43 |
), |
| 44 |
("get_dictproxy", "METH_O", |
| 45 |
''' |
| 46 |
return PyDictProxy_New(args); |
| 47 |
''' |
| 48 |
), |
| 49 |
("check", "METH_O", |
| 50 |
''' |
| 51 |
return PyLong_FromLong( |
| 52 |
PySequence_Check(args) + |
| 53 |
PyMapping_Check(args) * 2); |
| 54 |
''') |
| 55 |
], prologue = ''' |
| 56 |
static PyObject * |
| 57 |
mp_subscript(PyObject *self, PyObject *key) |
| 58 |
{ |
| 59 |
return PyLong_FromLong(42); |
| 60 |
} |
| 61 |
static Py_ssize_t |
| 62 |
mp_length(PyObject *self) |
| 63 |
{ |
| 64 |
return 2; |
| 65 |
} |
| 66 |
PyMappingMethods tp_as_mapping; |
| 67 |
static PyTypeObject Foo_Type = { |
| 68 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 69 |
"foo.foo", |
| 70 |
}; |
| 71 |
''', more_init = ''' |
| 72 |
Foo_Type.tp_flags = Py_TPFLAGS_DEFAULT; |
| 73 |
Foo_Type.tp_as_mapping = &tp_as_mapping; |
| 74 |
tp_as_mapping.mp_length = mp_length; |
| 75 |
tp_as_mapping.mp_subscript = mp_subscript; |
| 76 |
if (PyType_Ready(&Foo_Type) < 0) INITERROR; |
| 77 |
''') |
| 78 |
obj = module.test() |
| 79 |
assert obj["hi there"] == 42 |
| 80 |
assert len(obj) == 2 |
| 81 |
assert not hasattr(obj, "__iter__") |
| 82 |
e = raises(TypeError, iter, obj) |
| 83 |
assert str(e.value).endswith("object is not iterable") |
| 84 |
|
| 85 |
assert module.check(obj) == 2 |
| 86 |
|
| 87 |
assert module.check({'a': 1}) == 2 |
| 88 |
obj = module.get_dictproxy({'a': 10}) |
| 89 |
assert module.check(obj) == 2 |
| 90 |
|
| 91 |
def test_iterable_nonmapping_object(self): |
| 92 |
module = self.import_extension('foo', [ |
| 93 |
("test", "METH_NOARGS", |
| 94 |
''' |
| 95 |
PyObject *obj; |
| 96 |
obj = PyObject_New(PyObject, &Foo_Type); |
| 97 |
return obj; |
| 98 |
'''), |
| 99 |
("check", "METH_O", |
| 100 |
''' |
| 101 |
return PyLong_FromLong( |
| 102 |
PySequence_Check(args) + |
| 103 |
PyMapping_Check(args) * 2); |
| 104 |
''') |
| 105 |
], prologue=''' |
| 106 |
static PyObject * |
| 107 |
sq_item(PyObject *self, Py_ssize_t size) |
| 108 |
{ |
| 109 |
return PyLong_FromLong(42); |
| 110 |
} |
| 111 |
static Py_ssize_t |
| 112 |
sq_length(PyObject *self) |
| 113 |
{ |
| 114 |
return 2; |
| 115 |
} |
| 116 |
static PyObject * |
| 117 |
sq_repeat(PyObject *self, Py_ssize_t n) { |
| 118 |
return PyLong_FromLong(144); |
| 119 |
} |
| 120 |
static PyObject * |
| 121 |
sq_inplace_repeat(PyObject *self, Py_ssize_t n) { |
| 122 |
//fprintf(stdout, "in sq_inplace_repeat, n=%ld\\n", n); |
| 123 |
Py_INCREF(self); |
| 124 |
return self; |
| 125 |
} |
| 126 |
PySequenceMethods tp_as_sequence; |
| 127 |
static PyTypeObject Foo_Type = { |
| 128 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 129 |
"foo.foo", |
| 130 |
}; |
| 131 |
''', more_init=''' |
| 132 |
Foo_Type.tp_flags = Py_TPFLAGS_DEFAULT; |
| 133 |
Foo_Type.tp_as_sequence = &tp_as_sequence; |
| 134 |
tp_as_sequence.sq_length = sq_length; |
| 135 |
tp_as_sequence.sq_item = sq_item; |
| 136 |
tp_as_sequence.sq_repeat = sq_repeat; |
| 137 |
tp_as_sequence.sq_inplace_repeat = sq_inplace_repeat; |
| 138 |
if (PyType_Ready(&Foo_Type) < 0) INITERROR; |
| 139 |
''') |
| 140 |
obj = module.test() |
| 141 |
assert obj[1] == 42 |
| 142 |
assert len(obj) == 2 |
| 143 |
assert not hasattr(obj, "__iter__") |
| 144 |
it = iter(obj) |
| 145 |
assert next(it) == 42 |
| 146 |
assert next(it) == 42 |
| 147 |
|
| 148 |
assert module.check(obj) == 1 |
| 149 |
assert obj * 3 == 144 |
| 150 |
obj *= 3 |
| 151 |
assert obj[1] == 42 |