| 1 |
import py |
| 2 |
|
| 3 |
from pypy.module.cpyext.pyobject import ( |
| 4 |
PyObject, PyObjectP, make_ref, from_ref, incref, decref) |
| 5 |
from pypy.module.cpyext.test.test_api import BaseApiTest, raises_w |
| 6 |
from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase |
| 7 |
from rpython.rtyper.lltypesystem import rffi, lltype |
| 8 |
from rpython.rlib.debug import FatalError |
| 9 |
from pypy.module.cpyext.state import State |
| 10 |
from pypy.module.cpyext.tupleobject import ( |
| 11 |
PyTupleObject, PyTuple_Check, PyTuple_SetItem, PyTuple_Size) |
| 12 |
|
| 13 |
|
| 14 |
class TestTupleObject(BaseApiTest): |
| 15 |
|
| 16 |
def test_tupleobject_base(self, space): |
| 17 |
assert not PyTuple_Check(space, space.w_None) |
| 18 |
with raises_w(space, SystemError): |
| 19 |
n = make_ref(space, space.w_None) |
| 20 |
incref(space, n) |
| 21 |
PyTuple_SetItem(space, n, 0, n) |
| 22 |
atuple = space.newtuple([space.wrap(0), space.wrap(1), |
| 23 |
space.wrap('yay')]) |
| 24 |
assert PyTuple_Size(space, atuple) == 3 |
| 25 |
with raises_w(space, SystemError): |
| 26 |
PyTuple_Size(space, space.newlist([])) |
| 27 |
|
| 28 |
def test_tuple_realize_refuses_nulls(self, space, api): |
| 29 |
state = space.fromcache(State) |
| 30 |
py_tuple = state.ccall("PyTuple_New", 1) |
| 31 |
py.test.raises(FatalError, from_ref, space, py_tuple) |
| 32 |
decref(space, py_tuple) |
| 33 |
|
| 34 |
def test_tuple_resize(self, space, api): |
| 35 |
w_42 = space.wrap(42) |
| 36 |
w_43 = space.wrap(43) |
| 37 |
w_44 = space.wrap(44) |
| 38 |
ar = lltype.malloc(PyObjectP.TO, 1, flavor='raw') |
| 39 |
|
| 40 |
state = space.fromcache(State) |
| 41 |
py_tuple = state.ccall("PyTuple_New", 3) |
| 42 |
|
| 43 |
|
| 44 |
rffi.cast(PyTupleObject, py_tuple).c_ob_item[0] = make_ref(space, w_42) |
| 45 |
rffi.cast(PyTupleObject, py_tuple).c_ob_item[1] = make_ref(space, w_43) |
| 46 |
ar[0] = py_tuple |
| 47 |
api._PyTuple_Resize(ar, 2) |
| 48 |
w_tuple = from_ref(space, ar[0]) |
| 49 |
assert space.int_w(space.len(w_tuple)) == 2 |
| 50 |
assert space.int_w(space.getitem(w_tuple, space.wrap(0))) == 42 |
| 51 |
assert space.int_w(space.getitem(w_tuple, space.wrap(1))) == 43 |
| 52 |
decref(space, ar[0]) |
| 53 |
|
| 54 |
py_tuple = state.ccall("PyTuple_New", 3) |
| 55 |
rffi.cast(PyTupleObject, py_tuple).c_ob_item[0] = make_ref(space, w_42) |
| 56 |
rffi.cast(PyTupleObject, py_tuple).c_ob_item[1] = make_ref(space, w_43) |
| 57 |
rffi.cast(PyTupleObject, py_tuple).c_ob_item[2] = make_ref(space, w_44) |
| 58 |
ar[0] = py_tuple |
| 59 |
api._PyTuple_Resize(ar, 10) |
| 60 |
assert api.PyTuple_Size(ar[0]) == 10 |
| 61 |
for i in range(3, 10): |
| 62 |
rffi.cast(PyTupleObject, ar[0]).c_ob_item[i] = make_ref( |
| 63 |
space, space.wrap(42 + i)) |
| 64 |
w_tuple = from_ref(space, ar[0]) |
| 65 |
assert space.int_w(space.len(w_tuple)) == 10 |
| 66 |
for i in range(10): |
| 67 |
assert space.int_w(space.getitem(w_tuple, space.wrap(i))) == 42 + i |
| 68 |
decref(space, ar[0]) |
| 69 |
|
| 70 |
py_tuple = state.ccall("PyTuple_New", 1) |
| 71 |
ar[0] = py_tuple |
| 72 |
api._PyTuple_Resize(ar, 1) |
| 73 |
assert api.PyTuple_Size(ar[0]) == 1 |
| 74 |
decref(space, ar[0]) |
| 75 |
|
| 76 |
py_tuple = state.ccall("PyTuple_New", 1) |
| 77 |
ar[0] = py_tuple |
| 78 |
api._PyTuple_Resize(ar, 5) |
| 79 |
assert api.PyTuple_Size(ar[0]) == 5 |
| 80 |
decref(space, ar[0]) |
| 81 |
|
| 82 |
lltype.free(ar, flavor='raw') |
| 83 |
|
| 84 |
def test_setitem(self, space, api): |
| 85 |
state = space.fromcache(State) |
| 86 |
py_tuple = state.ccall("PyTuple_New", 2) |
| 87 |
api.PyTuple_SetItem(py_tuple, 0, make_ref(space, space.wrap(42))) |
| 88 |
api.PyTuple_SetItem(py_tuple, 1, make_ref(space, space.wrap(43))) |
| 89 |
|
| 90 |
w_tuple = from_ref(space, py_tuple) |
| 91 |
assert space.eq_w(w_tuple, space.newtuple([space.wrap(42), |
| 92 |
space.wrap(43)])) |
| 93 |
decref(space, py_tuple) |
| 94 |
|
| 95 |
def test_getslice(self, space, api): |
| 96 |
w_tuple = space.newtuple([space.wrap(i) for i in range(10)]) |
| 97 |
w_slice = api.PyTuple_GetSlice(w_tuple, 3, -3) |
| 98 |
assert space.eq_w(w_slice, |
| 99 |
space.newtuple([space.wrap(i) for i in range(3, 7)])) |
| 100 |
|
| 101 |
|
| 102 |
class AppTestTuple(AppTestCpythonExtensionBase): |
| 103 |
def test_refcounts(self): |
| 104 |
module = self.import_extension('foo', [ |
| 105 |
("run", "METH_NOARGS", |
| 106 |
""" |
| 107 |
PyObject *item = PyTuple_New(0); |
| 108 |
PyObject *t = PyTuple_New(1); |
| 109 |
#ifdef PYPY_VERSION |
| 110 |
// PyPy starts even empty tuples with a refcount of 1. |
| 111 |
const int initial_item_refcount = 1; |
| 112 |
#else |
| 113 |
// CPython can cache (). |
| 114 |
const int initial_item_refcount = item->ob_refcnt; |
| 115 |
#endif // PYPY_VERSION |
| 116 |
if (t->ob_refcnt != 1 || item->ob_refcnt != initial_item_refcount) { |
| 117 |
PyErr_SetString(PyExc_SystemError, "bad initial refcnt"); |
| 118 |
return NULL; |
| 119 |
} |
| 120 |
|
| 121 |
PyTuple_SetItem(t, 0, item); |
| 122 |
if (t->ob_refcnt != 1) { |
| 123 |
PyErr_SetString(PyExc_SystemError, "SetItem: t refcnt != 1"); |
| 124 |
return NULL; |
| 125 |
} |
| 126 |
if (item->ob_refcnt != initial_item_refcount) { |
| 127 |
PyErr_SetString(PyExc_SystemError, "GetItem: item refcnt != initial_item_refcount"); |
| 128 |
return NULL; |
| 129 |
} |
| 130 |
|
| 131 |
if (PyTuple_GetItem(t, 0) != item || |
| 132 |
PyTuple_GetItem(t, 0) != item) { |
| 133 |
PyErr_SetString(PyExc_SystemError, "GetItem: bogus item"); |
| 134 |
return NULL; |
| 135 |
} |
| 136 |
|
| 137 |
if (t->ob_refcnt != 1) { |
| 138 |
PyErr_SetString(PyExc_SystemError, "GetItem: t refcnt != 1"); |
| 139 |
return NULL; |
| 140 |
} |
| 141 |
if (item->ob_refcnt != initial_item_refcount) { |
| 142 |
PyErr_SetString(PyExc_SystemError, "GetItem: item refcnt != initial_item_refcount"); |
| 143 |
return NULL; |
| 144 |
} |
| 145 |
return t; |
| 146 |
"""), |
| 147 |
]) |
| 148 |
x = module.run() |
| 149 |
assert x == ((),) |
| 150 |
|
| 151 |
def test_refcounts_more(self): |
| 152 |
module = self.import_extension('foo', [ |
| 153 |
("run", "METH_NOARGS", |
| 154 |
""" |
| 155 |
Py_ssize_t prev; |
| 156 |
PyObject *t = PyTuple_New(1); |
| 157 |
prev = Py_True->ob_refcnt; |
| 158 |
Py_INCREF(Py_True); |
| 159 |
PyTuple_SetItem(t, 0, Py_True); |
| 160 |
if (Py_True->ob_refcnt != prev + 1) { |
| 161 |
PyErr_SetString(PyExc_SystemError, |
| 162 |
"SetItem: Py_True refcnt != prev + 1"); |
| 163 |
return NULL; |
| 164 |
} |
| 165 |
Py_DECREF(t); |
| 166 |
if (Py_True->ob_refcnt != prev) { |
| 167 |
PyErr_SetString(PyExc_SystemError, |
| 168 |
"after: Py_True refcnt != prev"); |
| 169 |
return NULL; |
| 170 |
} |
| 171 |
Py_INCREF(Py_None); |
| 172 |
return Py_None; |
| 173 |
"""), |
| 174 |
]) |
| 175 |
module.run() |
| 176 |
|
| 177 |
def test_tuple_subclass(self): |
| 178 |
module = self.import_module(name='foo') |
| 179 |
a = module.TupleLike(range(100, 400, 100)) |
| 180 |
assert module.is_TupleLike(a) == 1 |
| 181 |
assert isinstance(a, tuple) |
| 182 |
assert issubclass(type(a), tuple) |
| 183 |
assert list(a) == list(range(100, 400, 100)) |
| 184 |
assert list(a) == list(range(100, 400, 100)) |
| 185 |
assert list(a) == list(range(100, 400, 100)) |
| 186 |
|
| 187 |
def test_setitem(self): |
| 188 |
module = self.import_extension('foo', [ |
| 189 |
("set_after_use", "METH_O", |
| 190 |
""" |
| 191 |
PyObject *t2, *tuple = PyTuple_New(1); |
| 192 |
PyObject * one = PyLong_FromLong(1); |
| 193 |
int res; |
| 194 |
Py_INCREF(one); |
| 195 |
res = PyTuple_SetItem(tuple, 0, one); |
| 196 |
if (res != 0) |
| 197 |
{ |
| 198 |
Py_DECREF(one); |
| 199 |
Py_DECREF(tuple); |
| 200 |
return NULL; |
| 201 |
} |
| 202 |
Py_INCREF(args); |
| 203 |
res = PyTuple_SetItem(tuple, 0, args); |
| 204 |
if (res != 0) |
| 205 |
{ |
| 206 |
Py_DECREF(tuple); |
| 207 |
return NULL; |
| 208 |
} |
| 209 |
/* Do something that uses the tuple, but does not incref */ |
| 210 |
t2 = PyTuple_GetSlice(tuple, 0, 1); |
| 211 |
Py_DECREF(t2); |
| 212 |
res = PyTuple_SetItem(tuple, 0, one); |
| 213 |
if (res != 0) |
| 214 |
{ |
| 215 |
Py_DECREF(tuple); |
| 216 |
return NULL; |
| 217 |
} |
| 218 |
Py_DECREF(tuple); |
| 219 |
Py_INCREF(Py_None); |
| 220 |
return Py_None; |
| 221 |
"""), |
| 222 |
]) |
| 223 |
import sys |
| 224 |
s = 'abc' |
| 225 |
if '__pypy__' in sys.builtin_module_names: |
| 226 |
raises(SystemError, module.set_after_use, s) |
| 227 |
else: |
| 228 |
module.set_after_use(s) |
| 229 |
|
| 230 |
def test_mp_length(self): |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
module = self.import_extension('foo', [ |
| 235 |
("get_size", "METH_NOARGS", |
| 236 |
""" |
| 237 |
return (PyObject*)&THPSizeType; |
| 238 |
"""), |
| 239 |
], prologue=''' |
| 240 |
#include "Python.h" |
| 241 |
|
| 242 |
struct THPSize { |
| 243 |
PyTupleObject tuple; |
| 244 |
} THPSize; |
| 245 |
|
| 246 |
static PyMappingMethods THPSize_as_mapping = { |
| 247 |
0, //PyTuple_Type.tp_as_mapping->mp_length, |
| 248 |
0, |
| 249 |
0 |
| 250 |
}; |
| 251 |
|
| 252 |
PyTypeObject THPSizeType = { |
| 253 |
PyVarObject_HEAD_INIT(0, 0) |
| 254 |
"torch.Size", /* tp_name */ |
| 255 |
sizeof(THPSize), /* tp_basicsize */ |
| 256 |
}; |
| 257 |
''' , more_init = ''' |
| 258 |
THPSize_as_mapping.mp_length = PyTuple_Type.tp_as_mapping->mp_length; |
| 259 |
THPSizeType.tp_base = &PyTuple_Type; |
| 260 |
THPSizeType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 261 |
THPSizeType.tp_as_mapping = &THPSize_as_mapping; |
| 262 |
THPSizeType.tp_new = PyTuple_Type.tp_new; |
| 263 |
if (PyType_Ready(&THPSizeType) < 0) INITERROR; |
| 264 |
''') |
| 265 |
SZ = module.get_size() |
| 266 |
s = SZ((1, 2, 3)) |
| 267 |
assert len(s) == 3 |
| 268 |
assert len(s) == 3 |
| 269 |
|