| 1 |
""" HPyType slot tests on legacy types. """ |
| 2 |
|
| 3 |
from .support import HPyTest |
| 4 |
from .test_hpytype_legacy import LegacyPointTemplate |
| 5 |
from .test_slots import TestSlots as _TestSlots, TestSqSlots as _TestSqSlots |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
class TestLegacySlots(_TestSlots): |
| 10 |
ExtensionTemplate = LegacyPointTemplate |
| 11 |
USE_CPYEXT = True |
| 12 |
|
| 13 |
|
| 14 |
class TestLegacySqSlots(_TestSqSlots): |
| 15 |
ExtensionTemplate = LegacyPointTemplate |
| 16 |
USE_CPYEXT = True |
| 17 |
|
| 18 |
|
| 19 |
class TestCustomLegacySlotsFeatures(HPyTest): |
| 20 |
USE_CPYEXT = True |
| 21 |
|
| 22 |
def test_legacy_slots(self): |
| 23 |
mod = self.make_module(""" |
| 24 |
#include <Python.h> |
| 25 |
|
| 26 |
static PyObject *Dummy_repr(PyObject *self) |
| 27 |
{ |
| 28 |
return PyUnicode_FromString("myrepr"); |
| 29 |
} |
| 30 |
|
| 31 |
static PyObject *Dummy_add(PyObject *self, PyObject *other) |
| 32 |
{ |
| 33 |
return Py_BuildValue("OO", self, other); |
| 34 |
} |
| 35 |
|
| 36 |
HPyDef_SLOT(Dummy_abs, HPy_nb_absolute); |
| 37 |
static HPy Dummy_abs_impl(HPyContext *ctx, HPy self) |
| 38 |
{ |
| 39 |
return HPyLong_FromLong(ctx, 1234); |
| 40 |
} |
| 41 |
|
| 42 |
static HPyDef *Dummy_defines[] = { |
| 43 |
&Dummy_abs, |
| 44 |
NULL |
| 45 |
}; |
| 46 |
static PyType_Slot Dummy_type_slots[] = { |
| 47 |
{Py_tp_repr, (void*)Dummy_repr}, |
| 48 |
{Py_nb_add, (void*)Dummy_add}, |
| 49 |
{0, 0}, |
| 50 |
}; |
| 51 |
static HPyType_Spec Dummy_spec = { |
| 52 |
.name = "mytest.Dummy", |
| 53 |
.builtin_shape = HPyType_BuiltinShape_Legacy, |
| 54 |
.legacy_slots = Dummy_type_slots, |
| 55 |
.defines = Dummy_defines |
| 56 |
}; |
| 57 |
|
| 58 |
@EXPORT_TYPE("Dummy", Dummy_spec) |
| 59 |
@INIT |
| 60 |
""") |
| 61 |
d = mod.Dummy() |
| 62 |
assert repr(d) == 'myrepr' |
| 63 |
assert d + 42 == (d, 42) |
| 64 |
assert abs(d) == 1234 |
| 65 |
|
| 66 |
def test_legacy_slots_methods(self): |
| 67 |
mod = self.make_module(""" |
| 68 |
#include <Python.h> |
| 69 |
|
| 70 |
static PyObject *Dummy_foo(PyObject *self, PyObject *arg) |
| 71 |
{ |
| 72 |
Py_INCREF(arg); |
| 73 |
return arg; |
| 74 |
} |
| 75 |
|
| 76 |
HPyDef_METH(Dummy_bar, "bar", HPyFunc_NOARGS) |
| 77 |
static HPy Dummy_bar_impl(HPyContext *ctx, HPy self) |
| 78 |
{ |
| 79 |
return HPyLong_FromLong(ctx, 1234); |
| 80 |
} |
| 81 |
|
| 82 |
static PyMethodDef dummy_methods[] = { |
| 83 |
{"foo", Dummy_foo, METH_O}, |
| 84 |
{NULL, NULL} /* Sentinel */ |
| 85 |
}; |
| 86 |
|
| 87 |
static PyType_Slot dummy_type_slots[] = { |
| 88 |
{Py_tp_methods, dummy_methods}, |
| 89 |
{0, 0}, |
| 90 |
}; |
| 91 |
|
| 92 |
static HPyDef *dummy_type_defines[] = { |
| 93 |
&Dummy_bar, |
| 94 |
NULL |
| 95 |
}; |
| 96 |
|
| 97 |
static HPyType_Spec dummy_type_spec = { |
| 98 |
.name = "mytest.Dummy", |
| 99 |
.builtin_shape = HPyType_BuiltinShape_Legacy, |
| 100 |
.legacy_slots = dummy_type_slots, |
| 101 |
.defines = dummy_type_defines |
| 102 |
}; |
| 103 |
|
| 104 |
@EXPORT_TYPE("Dummy", dummy_type_spec) |
| 105 |
@INIT |
| 106 |
""") |
| 107 |
d = mod.Dummy() |
| 108 |
assert d.foo(21) == 21 |
| 109 |
assert d.bar() == 1234 |
| 110 |
|
| 111 |
def test_legacy_slots_members(self): |
| 112 |
import pytest |
| 113 |
mod = self.make_module(""" |
| 114 |
#include <Python.h> |
| 115 |
#include "structmember.h" |
| 116 |
|
| 117 |
typedef struct { |
| 118 |
PyObject_HEAD |
| 119 |
long x; |
| 120 |
long y; |
| 121 |
} PointObject; |
| 122 |
|
| 123 |
HPyDef_SLOT(Point_new, HPy_tp_new) |
| 124 |
static HPy Point_new_impl(HPyContext *ctx, HPy cls, const HPy *args, |
| 125 |
HPy_ssize_t nargs, HPy kw) |
| 126 |
{ |
| 127 |
PointObject *point; |
| 128 |
HPy h_point = HPy_New(ctx, cls, &point); |
| 129 |
if (HPy_IsNull(h_point)) |
| 130 |
return HPy_NULL; |
| 131 |
point->x = 7; |
| 132 |
point->y = 3; |
| 133 |
return h_point; |
| 134 |
} |
| 135 |
|
| 136 |
HPyDef_MEMBER(Point_x, "x", HPyMember_LONG, offsetof(PointObject, x)) |
| 137 |
|
| 138 |
// legacy members |
| 139 |
static PyMemberDef legacy_members[] = { |
| 140 |
{"y", T_LONG, offsetof(PointObject, y), 0}, |
| 141 |
{"y_ro", T_LONG, offsetof(PointObject, y), READONLY}, |
| 142 |
{NULL} |
| 143 |
}; |
| 144 |
|
| 145 |
static PyType_Slot legacy_slots[] = { |
| 146 |
{Py_tp_members, legacy_members}, |
| 147 |
{0, NULL} |
| 148 |
}; |
| 149 |
|
| 150 |
static HPyDef *Point_defines[] = { |
| 151 |
&Point_new, |
| 152 |
&Point_x, |
| 153 |
NULL |
| 154 |
}; |
| 155 |
static HPyType_Spec Point_spec = { |
| 156 |
.name = "mytest.Point", |
| 157 |
.basicsize = sizeof(PointObject), |
| 158 |
.builtin_shape = HPyType_BuiltinShape_Legacy, |
| 159 |
.legacy_slots = legacy_slots, |
| 160 |
.defines = Point_defines |
| 161 |
}; |
| 162 |
|
| 163 |
@EXPORT_TYPE("Point", Point_spec) |
| 164 |
@INIT |
| 165 |
""") |
| 166 |
p = mod.Point() |
| 167 |
assert p.x == 7 |
| 168 |
assert p.y == 3 |
| 169 |
assert p.y_ro == 3 |
| 170 |
p.x = 123 |
| 171 |
p.y = 456 |
| 172 |
with pytest.raises(AttributeError): |
| 173 |
p.y_ro = 789 |
| 174 |
assert p.x == 123 |
| 175 |
assert p.y == 456 |
| 176 |
|
| 177 |
def test_legacy_slots_getsets(self): |
| 178 |
mod = self.make_module(""" |
| 179 |
#include <Python.h> |
| 180 |
|
| 181 |
typedef struct { |
| 182 |
PyObject_HEAD |
| 183 |
long x; |
| 184 |
long y; |
| 185 |
} PointObject; |
| 186 |
|
| 187 |
HPyDef_SLOT(Point_new, HPy_tp_new) |
| 188 |
static HPy Point_new_impl(HPyContext *ctx, HPy cls, const HPy *args, |
| 189 |
HPy_ssize_t nargs, HPy kw) |
| 190 |
{ |
| 191 |
PointObject *point; |
| 192 |
HPy h_point = HPy_New(ctx, cls, &point); |
| 193 |
if (HPy_IsNull(h_point)) |
| 194 |
return HPy_NULL; |
| 195 |
point->x = 7; |
| 196 |
point->y = 3; |
| 197 |
return h_point; |
| 198 |
} |
| 199 |
|
| 200 |
static PyObject *z_get(PointObject *point, void *closure) |
| 201 |
{ |
| 202 |
long z = point->x*10 + point->y + (long)(HPy_ssize_t)closure; |
| 203 |
return PyLong_FromLong(z); |
| 204 |
} |
| 205 |
|
| 206 |
// legacy getsets |
| 207 |
static PyGetSetDef legacy_getsets[] = { |
| 208 |
{"z", (getter)z_get, NULL, NULL, (void *)2000}, |
| 209 |
{NULL} |
| 210 |
}; |
| 211 |
|
| 212 |
static PyType_Slot legacy_slots[] = { |
| 213 |
{Py_tp_getset, legacy_getsets}, |
| 214 |
{0, NULL} |
| 215 |
}; |
| 216 |
|
| 217 |
static HPyDef *Point_defines[] = { |
| 218 |
&Point_new, |
| 219 |
NULL |
| 220 |
}; |
| 221 |
static HPyType_Spec Point_spec = { |
| 222 |
.name = "mytest.Point", |
| 223 |
.basicsize = sizeof(PointObject), |
| 224 |
.builtin_shape = HPyType_BuiltinShape_Legacy, |
| 225 |
.legacy_slots = legacy_slots, |
| 226 |
.defines = Point_defines |
| 227 |
}; |
| 228 |
|
| 229 |
HPyDef_METH(get_z, "get_z", HPyFunc_O) |
| 230 |
static HPy get_z_impl(HPyContext *ctx, HPy self, HPy arg) |
| 231 |
{ |
| 232 |
return HPy_GetAttr_s(ctx, arg, "z"); |
| 233 |
} |
| 234 |
|
| 235 |
@EXPORT_TYPE("Point", Point_spec) |
| 236 |
@EXPORT(get_z) |
| 237 |
@INIT |
| 238 |
""") |
| 239 |
p = mod.Point() |
| 240 |
assert p.z == 2073 |
| 241 |
assert mod.get_z(p) == 2073 |
| 242 |
|
| 243 |
def test_legacy_slots_fails_without_legacy(self): |
| 244 |
import pytest |
| 245 |
mod_src = """ |
| 246 |
#include <Python.h> |
| 247 |
|
| 248 |
static PyObject *Dummy_foo(PyObject *self, PyObject *arg) |
| 249 |
{ |
| 250 |
Py_INCREF(arg); |
| 251 |
return arg; |
| 252 |
} |
| 253 |
|
| 254 |
static PyMethodDef dummy_methods[] = { |
| 255 |
{"foo", Dummy_foo, METH_O}, |
| 256 |
{NULL, NULL} /* Sentinel */ |
| 257 |
}; |
| 258 |
|
| 259 |
static PyType_Slot dummy_type_slots[] = { |
| 260 |
{Py_tp_methods, dummy_methods}, |
| 261 |
{0, 0}, |
| 262 |
}; |
| 263 |
|
| 264 |
static HPyType_Spec dummy_type_spec = { |
| 265 |
.name = "mytest.Dummy", |
| 266 |
.builtin_shape = HPyType_BuiltinShape_Object, |
| 267 |
.legacy_slots = dummy_type_slots, |
| 268 |
}; |
| 269 |
|
| 270 |
@EXPORT_TYPE("Dummy", dummy_type_spec) |
| 271 |
@INIT |
| 272 |
""" |
| 273 |
with pytest.raises(TypeError) as err: |
| 274 |
self.make_module(mod_src) |
| 275 |
assert str(err.value) == ( |
| 276 |
"cannot specify .legacy_slots without setting .builtin_shape=HPyType_BuiltinShape_Legacy") |
| 277 |
|
| 278 |
def test_buffer_legacy(self): |
| 279 |
|
| 280 |
|
| 281 |
mod = self.make_module(""" |
| 282 |
#include <Python.h> |
| 283 |
typedef struct { |
| 284 |
PyObject_HEAD |
| 285 |
int exports; |
| 286 |
} FakeArrayObject; |
| 287 |
|
| 288 |
HPyType_LEGACY_HELPERS(FakeArrayObject); |
| 289 |
|
| 290 |
static char static_mem[12] = {0,1,2,3,4,5,6,7,8,9,10,11}; |
| 291 |
static HPy_ssize_t _shape[1] = {12}; |
| 292 |
static HPy_ssize_t _strides[1] = {1}; |
| 293 |
|
| 294 |
HPyDef_SLOT_IMPL(FakeArray_getbuffer, _getbuffer_impl, HPy_bf_getbuffer) |
| 295 |
static int _getbuffer_impl(HPyContext *ctx, HPy self, HPy_buffer* buf, int flags) { |
| 296 |
FakeArrayObject *arr = FakeArrayObject_AsStruct(ctx, self); |
| 297 |
if (arr->exports > 0) { |
| 298 |
buf->obj = HPy_NULL; |
| 299 |
HPyErr_SetString(ctx, ctx->h_BufferError, |
| 300 |
"only one buffer allowed"); |
| 301 |
return -1; |
| 302 |
} |
| 303 |
arr->exports++; |
| 304 |
buf->buf = static_mem; |
| 305 |
buf->len = 12; |
| 306 |
buf->itemsize = 1; |
| 307 |
buf->readonly = 1; |
| 308 |
buf->ndim = 1; |
| 309 |
buf->format = (char*)"B"; |
| 310 |
buf->shape = _shape; |
| 311 |
buf->strides = _strides; |
| 312 |
buf->suboffsets = NULL; |
| 313 |
buf->internal = NULL; |
| 314 |
buf->obj = HPy_Dup(ctx, self); |
| 315 |
return 0; |
| 316 |
} |
| 317 |
|
| 318 |
HPyDef_SLOT_IMPL(FakeArray_releasebuffer, _relbuffer_impl, HPy_bf_releasebuffer) |
| 319 |
static void _relbuffer_impl(HPyContext *ctx, HPy h_obj, HPy_buffer* buf) { |
| 320 |
FakeArrayObject *arr = FakeArrayObject_AsStruct(ctx, h_obj); |
| 321 |
arr->exports--; |
| 322 |
} |
| 323 |
|
| 324 |
static HPyDef *FakeArray_defines[] = { |
| 325 |
&FakeArray_getbuffer, |
| 326 |
&FakeArray_releasebuffer, |
| 327 |
NULL |
| 328 |
}; |
| 329 |
|
| 330 |
static HPyType_Spec FakeArray_Spec = { |
| 331 |
.name = "mytest.FakeArray", |
| 332 |
.basicsize = sizeof(FakeArrayObject), |
| 333 |
.builtin_shape = SHAPE(FakeArrayObject), |
| 334 |
.defines = FakeArray_defines, |
| 335 |
}; |
| 336 |
|
| 337 |
HPyDef_METH(pybuffer, "pybuffer", HPyFunc_O) |
| 338 |
static HPy pybuffer_impl(HPyContext *ctx, HPy self, HPy arg) |
| 339 |
{ |
| 340 |
Py_buffer view; |
| 341 |
PyObject *o = HPy_AsPyObject(ctx, arg); |
| 342 |
size_t hasbuffer = PyObject_CheckBuffer(o); |
| 343 |
if (!hasbuffer) { |
| 344 |
Py_DecRef(o); |
| 345 |
HPyErr_SetString(ctx, ctx->h_RuntimeError, "PyObject_CheckBuffer failed"); |
| 346 |
return HPy_NULL; |
| 347 |
} |
| 348 |
if (PyObject_GetBuffer(o, &view, PyBUF_SIMPLE) != 0) { |
| 349 |
Py_DecRef(o); |
| 350 |
return HPy_NULL; |
| 351 |
} |
| 352 |
PyBuffer_Release(&view); |
| 353 |
return HPyLong_FromLong(ctx, 0); |
| 354 |
} |
| 355 |
|
| 356 |
@EXPORT(pybuffer) |
| 357 |
@EXPORT_TYPE("FakeArray", FakeArray_Spec) |
| 358 |
@INIT |
| 359 |
""") |
| 360 |
arr = mod.FakeArray() |
| 361 |
assert mod.pybuffer(arr) == 0 |