| 1 | """ HPyType slot tests on legacy types. """ |
| 2 | |
| 3 | from .support import HPyTest, make_hpy_abi_fixture |
| 4 | from .test_hpytype_legacy import LegacyPointTemplate |
| 5 | from .test_slots import TestSlots as _TestSlots, TestSqSlots as _TestSqSlots |
| 6 | |
| 7 | hpy_abi = make_hpy_abi_fixture('with hybrid') |
| 8 | |
| 9 | class TestLegacySlots(_TestSlots): |
| 10 | ExtensionTemplate = LegacyPointTemplate |
| 11 | |
| 12 | |
| 13 | class TestLegacySqSlots(_TestSqSlots): |
| 14 | ExtensionTemplate = LegacyPointTemplate |
| 15 | |
| 16 | |
| 17 | class TestCustomLegacySlotsFeatures(HPyTest): |
| 18 | |
| 19 | def test_legacy_slots(self): |
| 20 | mod = self.make_module(""" |
| 21 | #include <Python.h> |
| 22 | |
| 23 | static PyObject *Dummy_repr(PyObject *self) |
| 24 | { |
| 25 | return PyUnicode_FromString("myrepr"); |
| 26 | } |
| 27 | |
| 28 | static PyObject *Dummy_add(PyObject *self, PyObject *other) |
| 29 | { |
| 30 | return Py_BuildValue("OO", self, other); |
| 31 | } |
| 32 | |
| 33 | HPyDef_SLOT(Dummy_abs, HPy_nb_absolute); |
| 34 | static HPy Dummy_abs_impl(HPyContext *ctx, HPy self) |
| 35 | { |
| 36 | return HPyLong_FromLong(ctx, 1234); |
| 37 | } |
| 38 | |
| 39 | static HPyDef *Dummy_defines[] = { |
| 40 | &Dummy_abs, |
| 41 | NULL |
| 42 | }; |
| 43 | static PyType_Slot Dummy_type_slots[] = { |
| 44 | {Py_tp_repr, (void*)Dummy_repr}, |
| 45 | {Py_nb_add, (void*)Dummy_add}, |
| 46 | {0, 0}, |
| 47 | }; |
| 48 | static HPyType_Spec Dummy_spec = { |
| 49 | .name = "mytest.Dummy", |
| 50 | .builtin_shape = HPyType_BuiltinShape_Legacy, |
| 51 | .legacy_slots = Dummy_type_slots, |
| 52 | .defines = Dummy_defines |
| 53 | }; |
| 54 | |
| 55 | @EXPORT_TYPE("Dummy", Dummy_spec) |
| 56 | @INIT |
| 57 | """) |
| 58 | d = mod.Dummy() |
| 59 | assert repr(d) == 'myrepr' |
| 60 | assert d + 42 == (d, 42) |
| 61 | assert abs(d) == 1234 |
| 62 | |
| 63 | def test_legacy_slots_methods(self): |
| 64 | mod = self.make_module(""" |
| 65 | #include <Python.h> |
| 66 | |
| 67 | static PyObject *Dummy_foo(PyObject *self, PyObject *arg) |
| 68 | { |
| 69 | Py_INCREF(arg); |
| 70 | return arg; |
| 71 | } |
| 72 | |
| 73 | HPyDef_METH(Dummy_bar, "bar", HPyFunc_NOARGS) |
| 74 | static HPy Dummy_bar_impl(HPyContext *ctx, HPy self) |
| 75 | { |
| 76 | return HPyLong_FromLong(ctx, 1234); |
| 77 | } |
| 78 | |
| 79 | static PyMethodDef dummy_methods[] = { |
| 80 | {"foo", Dummy_foo, METH_O}, |
| 81 | {NULL, NULL} /* Sentinel */ |
| 82 | }; |
| 83 | |
| 84 | static PyType_Slot dummy_type_slots[] = { |
| 85 | {Py_tp_methods, dummy_methods}, |
| 86 | {0, 0}, |
| 87 | }; |
| 88 | |
| 89 | static HPyDef *dummy_type_defines[] = { |
| 90 | &Dummy_bar, |
| 91 | NULL |
| 92 | }; |
| 93 | |
| 94 | static HPyType_Spec dummy_type_spec = { |
| 95 | .name = "mytest.Dummy", |
| 96 | .builtin_shape = HPyType_BuiltinShape_Legacy, |
| 97 | .legacy_slots = dummy_type_slots, |
| 98 | .defines = dummy_type_defines |
| 99 | }; |
| 100 | |
| 101 | @EXPORT_TYPE("Dummy", dummy_type_spec) |
| 102 | @INIT |
| 103 | """) |
| 104 | d = mod.Dummy() |
| 105 | assert d.foo(21) == 21 |
| 106 | assert d.bar() == 1234 |
| 107 | |
| 108 | def test_legacy_slots_members(self): |
| 109 | import pytest |
| 110 | mod = self.make_module(""" |
| 111 | #include <Python.h> |
| 112 | #include "structmember.h" |
| 113 | |
| 114 | typedef struct { |
| 115 | PyObject_HEAD |
| 116 | long x; |
| 117 | long y; |
| 118 | } PointObject; |
| 119 | |
| 120 | HPyDef_SLOT(Point_new, HPy_tp_new) |
| 121 | static HPy Point_new_impl(HPyContext *ctx, HPy cls, const HPy *args, |
| 122 | HPy_ssize_t nargs, HPy kw) |
| 123 | { |
| 124 | PointObject *point; |
| 125 | HPy h_point = HPy_New(ctx, cls, &point); |
| 126 | if (HPy_IsNull(h_point)) |
| 127 | return HPy_NULL; |
| 128 | point->x = 7; |
| 129 | point->y = 3; |
| 130 | return h_point; |
| 131 | } |
| 132 | |
| 133 | HPyDef_MEMBER(Point_x, "x", HPyMember_LONG, offsetof(PointObject, x)) |
| 134 | |
| 135 | // legacy members |
| 136 | static PyMemberDef legacy_members[] = { |
| 137 | {"y", T_LONG, offsetof(PointObject, y), 0}, |
| 138 | {"y_ro", T_LONG, offsetof(PointObject, y), READONLY}, |
| 139 | {NULL} |
| 140 | }; |
| 141 | |
| 142 | static PyType_Slot legacy_slots[] = { |
| 143 | {Py_tp_members, legacy_members}, |
| 144 | {0, NULL} |
| 145 | }; |
| 146 | |
| 147 | static HPyDef *Point_defines[] = { |
| 148 | &Point_new, |
| 149 | &Point_x, |
| 150 | NULL |
| 151 | }; |
| 152 | static HPyType_Spec Point_spec = { |
| 153 | .name = "mytest.Point", |
| 154 | .basicsize = sizeof(PointObject), |
| 155 | .builtin_shape = HPyType_BuiltinShape_Legacy, |
| 156 | .legacy_slots = legacy_slots, |
| 157 | .defines = Point_defines |
| 158 | }; |
| 159 | |
| 160 | @EXPORT_TYPE("Point", Point_spec) |
| 161 | @INIT |
| 162 | """) |
| 163 | p = mod.Point() |
| 164 | assert p.x == 7 |
| 165 | assert p.y == 3 |
| 166 | assert p.y_ro == 3 |
| 167 | p.x = 123 |
| 168 | p.y = 456 |
| 169 | with pytest.raises(AttributeError): |
| 170 | p.y_ro = 789 |
| 171 | assert p.x == 123 |
| 172 | assert p.y == 456 |
| 173 | |
| 174 | def test_legacy_slots_getsets(self): |
| 175 | mod = self.make_module(""" |
| 176 | #include <Python.h> |
| 177 | |
| 178 | typedef struct { |
| 179 | PyObject_HEAD |
| 180 | long x; |
| 181 | long y; |
| 182 | } PointObject; |
| 183 | |
| 184 | HPyDef_SLOT(Point_new, HPy_tp_new) |
| 185 | static HPy Point_new_impl(HPyContext *ctx, HPy cls, const HPy *args, |
| 186 | HPy_ssize_t nargs, HPy kw) |
| 187 | { |
| 188 | PointObject *point; |
| 189 | HPy h_point = HPy_New(ctx, cls, &point); |
| 190 | if (HPy_IsNull(h_point)) |
| 191 | return HPy_NULL; |
| 192 | point->x = 7; |
| 193 | point->y = 3; |
| 194 | return h_point; |
| 195 | } |
| 196 | |
| 197 | static PyObject *z_get(PointObject *point, void *closure) |
| 198 | { |
| 199 | long z = point->x*10 + point->y + (long)(HPy_ssize_t)closure; |
| 200 | return PyLong_FromLong(z); |
| 201 | } |
| 202 | |
| 203 | // legacy getsets |
| 204 | static PyGetSetDef legacy_getsets[] = { |
| 205 | {"z", (getter)z_get, NULL, NULL, (void *)2000}, |
| 206 | {NULL} |
| 207 | }; |
| 208 | |
| 209 | static PyType_Slot legacy_slots[] = { |
| 210 | {Py_tp_getset, legacy_getsets}, |
| 211 | {0, NULL} |
| 212 | }; |
| 213 | |
| 214 | static HPyDef *Point_defines[] = { |
| 215 | &Point_new, |
| 216 | NULL |
| 217 | }; |
| 218 | static HPyType_Spec Point_spec = { |
| 219 | .name = "mytest.Point", |
| 220 | .basicsize = sizeof(PointObject), |
| 221 | .builtin_shape = HPyType_BuiltinShape_Legacy, |
| 222 | .legacy_slots = legacy_slots, |
| 223 | .defines = Point_defines |
| 224 | }; |
| 225 | |
| 226 | @EXPORT_TYPE("Point", Point_spec) |
| 227 | @INIT |
| 228 | """) |
| 229 | p = mod.Point() |
| 230 | assert p.z == 2073 |
| 231 | |
| 232 | def test_legacy_slots_fails_without_legacy(self): |
| 233 | import pytest |
| 234 | mod_src = """ |
| 235 | #include <Python.h> |
| 236 | |
| 237 | static PyObject *Dummy_foo(PyObject *self, PyObject *arg) |
| 238 | { |
| 239 | Py_INCREF(arg); |
| 240 | return arg; |
| 241 | } |
| 242 | |
| 243 | static PyMethodDef dummy_methods[] = { |
| 244 | {"foo", Dummy_foo, METH_O}, |
| 245 | {NULL, NULL} /* Sentinel */ |
| 246 | }; |
| 247 | |
| 248 | static PyType_Slot dummy_type_slots[] = { |
| 249 | {Py_tp_methods, dummy_methods}, |
| 250 | {0, 0}, |
| 251 | }; |
| 252 | |
| 253 | static HPyType_Spec dummy_type_spec = { |
| 254 | .name = "mytest.Dummy", |
| 255 | .builtin_shape = HPyType_BuiltinShape_Object, |
| 256 | .legacy_slots = dummy_type_slots, |
| 257 | }; |
| 258 | |
| 259 | @EXPORT_TYPE("Dummy", dummy_type_spec) |
| 260 | @INIT |
| 261 | """ |
| 262 | with pytest.raises(TypeError) as err: |
| 263 | self.make_module(mod_src) |
| 264 | assert str(err.value) == ( |
| 265 | "cannot specify .legacy_slots without setting .builtin_shape=HPyType_BuiltinShape_Legacy") |