Debian Codegraph @

pypy/module/_hpy_universal/test/_vendored/test_slots.py

raw symbols SCIP
1 import pytest
2 from .support import HPyTest
3 from .test_hpytype import PointTemplate
4
5
6 class TestSlots(HPyTest):
7
8 ExtensionTemplate = PointTemplate
9
10 def test_tp_init(self):
11 mod = self.make_module("""
12 @DEFINE_PointObject
13 @DEFINE_Point_xy
14 HPyDef_SLOT_IMPL(Point_new, HPyType_GenericNew, HPy_tp_new)
15
16 HPyDef_SLOT(Point_init, HPy_tp_init)
17 static int Point_init_impl(HPyContext *ctx, HPy self, const HPy *args,
18 HPy_ssize_t nargs, HPy kw)
19 {
20 long x, y;
21 if (!HPyArg_Parse(ctx, NULL, args, nargs, "ll", &x, &y))
22 return -1;
23
24 PointObject *p = PointObject_AsStruct(ctx, self);
25 p->x = x;
26 p->y = y;
27 return 0;
28 }
29
30 @EXPORT_POINT_TYPE(&Point_new, &Point_init, &Point_x, &Point_y)
31 @INIT
32 """)
33 p = mod.Point(1, 2)
34 assert p.x == 1
35 assert p.y == 2
36
37 @pytest.mark.syncgc
38 def test_tp_destroy(self):
39 import gc
40 mod = self.make_module("""
41 @DEFINE_PointObject
42 @DEFINE_Point_new
43
44 static long destroyed_x;
45
46 HPyDef_SLOT(Point_destroy, HPy_tp_destroy)
47 static void Point_destroy_impl(void *obj)
48 {
49 PointObject *point = (PointObject *)obj;
50 destroyed_x += point->x;
51 }
52
53 HPyDef_METH(get_destroyed_x, "get_destroyed_x", HPyFunc_NOARGS)
54 static HPy get_destroyed_x_impl(HPyContext *ctx, HPy self)
55 {
56 return HPyLong_FromLong(ctx, destroyed_x);
57 }
58
59 @EXPORT_POINT_TYPE(&Point_new, &Point_destroy)
60 @EXPORT(get_destroyed_x)
61 @INIT
62 """)
63 point = mod.Point(7, 3)
64 self.debug_collect()
65 assert mod.get_destroyed_x() == 0
66 del point
67 self.debug_collect()
68 assert mod.get_destroyed_x() == 7
69 self.debug_collect()
70 assert mod.get_destroyed_x() == 7
71
72 @pytest.mark.syncgc
73 def test_tp_finalize_nongc(self):
74 import gc
75 mod = self.make_module("""
76 @DEFINE_PointObject
77 @DEFINE_Point_new
78
79 static long finalized_x;
80
81 HPyDef_SLOT(Point_finalize, HPy_tp_finalize)
82 static void Point_finalize_impl(HPyContext *ctx, HPy obj)
83 {
84 PointObject *point = PointObject_AsStruct(ctx, obj);
85 finalized_x += point->x;
86 }
87
88 HPyDef_METH(get_finalized_x, "get_finalized_x", HPyFunc_NOARGS)
89 static HPy get_finalized_x_impl(HPyContext *ctx, HPy self)
90 {
91 return HPyLong_FromLong(ctx, finalized_x);
92 }
93
94 @EXPORT_POINT_TYPE(&Point_new, &Point_finalize)
95 @EXPORT(get_finalized_x)
96 @INIT
97 """)
98 point = mod.Point(7, 3)
99 assert mod.get_finalized_x() == 0
100 del point
101 self.debug_collect()
102 assert mod.get_finalized_x() == 7
103 self.debug_collect()
104 assert mod.get_finalized_x() == 7
105
106 @pytest.mark.syncgc
107 def test_tp_finalize_gc(self):
108 import gc
109 mod = self.make_module("""
110 @DEFINE_PointObject
111 @DEFINE_Point_new
112
113 static long finalized_x;
114
115 HPyDef_SLOT(Point_finalize, HPy_tp_finalize)
116 static void Point_finalize_impl(HPyContext *ctx, HPy obj)
117 {
118 PointObject *point = PointObject_AsStruct(ctx, obj);
119 finalized_x += point->x;
120 }
121
122 HPyDef_SLOT(Point_traverse, HPy_tp_traverse)
123 static int Point_traverse_impl(void *self, HPyFunc_visitproc visit, void *arg)
124 {
125 return 0;
126 }
127
128 static HPyDef *Point_defines[] = {
129 &Point_new, &Point_finalize, &Point_traverse, NULL};
130 static HPyType_Spec Point_spec = {
131 .name = "mytest.Point",
132 .basicsize = sizeof(PointObject),
133 .flags = HPy_TPFLAGS_DEFAULT | HPy_TPFLAGS_HAVE_GC,
134 .builtin_shape = SHAPE(PointObject),
135 .defines = Point_defines,
136 };
137 @EXPORT_TYPE("Point", Point_spec)
138
139 HPyDef_METH(get_finalized_x, "get_finalized_x", HPyFunc_NOARGS)
140 static HPy get_finalized_x_impl(HPyContext *ctx, HPy self)
141 {
142 return HPyLong_FromLong(ctx, finalized_x);
143 }
144
145 @EXPORT(get_finalized_x)
146 @INIT
147 """)
148 point = mod.Point(7, 3)
149 assert mod.get_finalized_x() == 0
150 del point
151 self.debug_collect()
152 assert mod.get_finalized_x() == 7
153 self.debug_collect()
154 assert mod.get_finalized_x() == 7
155
156 def test_nb_ops_binary(self):
157 import operator
158 mod = self.make_module(r"""
159 @DEFINE_PointObject
160
161 #define MYSLOT(NAME) \
162 HPyDef_SLOT_IMPL(p_##NAME, NAME##_impl, HPy_nb_##NAME) \
163 static HPy NAME##_impl(HPyContext *ctx, HPy self, HPy other) \
164 { \
165 HPy s = HPyUnicode_FromString(ctx, #NAME); \
166 HPy res = HPyTuple_Pack(ctx, 3, self, s, other); \
167 HPy_Close(ctx, s); \
168 return res; \
169 }
170
171 MYSLOT(add)
172 MYSLOT(and)
173 MYSLOT(divmod)
174 MYSLOT(floor_divide)
175 MYSLOT(lshift)
176 MYSLOT(multiply)
177 MYSLOT(or)
178 MYSLOT(remainder)
179 MYSLOT(rshift)
180 MYSLOT(subtract)
181 MYSLOT(true_divide)
182 MYSLOT(xor)
183 MYSLOT(matrix_multiply)
184
185 @EXPORT_POINT_TYPE(&p_add, &p_and, &p_divmod, &p_floor_divide, &p_lshift, &p_multiply, &p_or, &p_remainder, &p_rshift, &p_subtract, &p_true_divide, &p_xor, &p_matrix_multiply)
186 @INIT
187 """)
188 p = mod.Point()
189 assert p + 42 == (p, "add", 42)
190 assert p & 42 == (p, "and", 42)
191 assert divmod(p, 42) == (p, "divmod", 42)
192 assert p // 42 == (p, "floor_divide", 42)
193 assert p << 42 == (p, "lshift", 42)
194 assert p * 42 == (p, "multiply", 42)
195 assert p | 42 == (p, "or", 42)
196 assert p % 42 == (p, "remainder", 42)
197 assert p >> 42 == (p, "rshift", 42)
198 assert p - 42 == (p, "subtract", 42)
199 assert p / 42 == (p, "true_divide", 42)
200 assert p ^ 42 == (p, "xor", 42)
201 # we can't use '@' because we want to be importable on py27
202 assert operator.matmul(p, 42) == (p, "matrix_multiply", 42)
203
204 def test_nb_ops_inplace(self):
205 import operator
206 mod = self.make_module(r"""
207 @DEFINE_PointObject
208
209 #define MYSLOT(NAME) \
210 HPyDef_SLOT_IMPL(p_##NAME, NAME##_impl, HPy_nb_##NAME); \
211 static HPy NAME##_impl(HPyContext *ctx, HPy self, HPy other) \
212 { \
213 HPy s = HPyUnicode_FromString(ctx, #NAME); \
214 HPy res = HPyTuple_Pack(ctx, 3, self, s, other); \
215 HPy_Close(ctx, s); \
216 return res; \
217 }
218
219 MYSLOT(inplace_add)
220 MYSLOT(inplace_and)
221 MYSLOT(inplace_floor_divide)
222 MYSLOT(inplace_lshift)
223 MYSLOT(inplace_multiply)
224 MYSLOT(inplace_or)
225 MYSLOT(inplace_remainder)
226 MYSLOT(inplace_rshift)
227 MYSLOT(inplace_subtract)
228 MYSLOT(inplace_true_divide)
229 MYSLOT(inplace_xor)
230 MYSLOT(inplace_matrix_multiply)
231
232 @EXPORT_POINT_TYPE(&p_inplace_add, &p_inplace_and, &p_inplace_floor_divide, &p_inplace_lshift, &p_inplace_multiply, &p_inplace_or, &p_inplace_remainder, &p_inplace_rshift, &p_inplace_subtract, &p_inplace_true_divide, &p_inplace_xor, &p_inplace_matrix_multiply)
233 @INIT
234 """)
235 p = mod.Point()
236 tmp = p; tmp += 42; assert tmp == (p, "inplace_add", 42)
237 tmp = p; tmp &= 42; assert tmp == (p, "inplace_and", 42)
238 tmp = p; tmp //= 42; assert tmp == (p, "inplace_floor_divide", 42)
239 tmp = p; tmp <<= 42; assert tmp == (p, "inplace_lshift", 42)
240 tmp = p; tmp *= 42; assert tmp == (p, "inplace_multiply", 42)
241 tmp = p; tmp |= 42; assert tmp == (p, "inplace_or", 42)
242 tmp = p; tmp %= 42; assert tmp == (p, "inplace_remainder", 42)
243 tmp = p; tmp >>= 42; assert tmp == (p, "inplace_rshift", 42)
244 tmp = p; tmp -= 42; assert tmp == (p, "inplace_subtract", 42)
245 tmp = p; tmp /= 42; assert tmp == (p, "inplace_true_divide", 42)
246 tmp = p; tmp ^= 42; assert tmp == (p, "inplace_xor", 42)
247 #
248 # we can't use '@=' because we want to be importable on py27
249 tmp = p
250 tmp = operator.imatmul(p, 42)
251 assert tmp == (p, "inplace_matrix_multiply", 42)
252
253 def test_nb_ops_unary(self):
254 mod = self.make_module(r"""
255 @DEFINE_PointObject
256
257 #define MYSLOT(NAME) \
258 HPyDef_SLOT_IMPL(p_##NAME, NAME##_impl, HPy_nb_##NAME); \
259 static HPy NAME##_impl(HPyContext *ctx, HPy self) \
260 { \
261 HPy s = HPyUnicode_FromString(ctx, #NAME); \
262 HPy res = HPyTuple_Pack(ctx, 2, s, self); \
263 HPy_Close(ctx, s); \
264 return res; \
265 }
266
267 MYSLOT(negative)
268 MYSLOT(positive)
269 MYSLOT(absolute)
270 MYSLOT(invert)
271
272 @EXPORT_POINT_TYPE(&p_negative, &p_positive, &p_absolute, &p_invert)
273 @INIT
274 """)
275 p = mod.Point()
276 assert +p == ('positive', p)
277 assert -p == ('negative', p)
278 assert abs(p) == ('absolute', p)
279 assert ~p == ('invert', p)
280
281 def test_nb_ops_type_conversion(self):
282 import operator
283 mod = self.make_module(r"""
284 @DEFINE_PointObject
285 @DEFINE_Point_new
286
287 HPyDef_SLOT(p_int, HPy_nb_int);
288 static HPy p_int_impl(HPyContext *ctx, HPy self)
289 {
290 return HPyLong_FromLong(ctx, 42);
291 }
292
293 HPyDef_SLOT(p_float, HPy_nb_float);
294 static HPy p_float_impl(HPyContext *ctx, HPy self)
295 {
296 return HPyFloat_FromDouble(ctx, 123.4);
297 }
298
299 HPyDef_SLOT(p_index, HPy_nb_index);
300 static HPy p_index_impl(HPyContext *ctx, HPy self)
301 {
302 return HPyLong_FromLong(ctx, -456);
303 }
304
305 HPyDef_SLOT(p_bool, HPy_nb_bool);
306 static int p_bool_impl(HPyContext *ctx, HPy self)
307 {
308 PointObject *point = PointObject_AsStruct(ctx, self);
309 return (point->x != 0);
310 }
311
312 @EXPORT_POINT_TYPE(&Point_new, &p_int, &p_float, &p_index, &p_bool)
313 @INIT
314 """)
315 p = mod.Point(0, 0)
316 assert int(p) == 42
317 assert float(p) == 123.4
318 assert operator.index(p) == -456
319 #
320 assert bool(mod.Point(0, 0)) is False
321 assert bool(mod.Point(1, 0)) is True
322
323 def test_nb_ops_power(self):
324 mod = self.make_module(r"""
325 @DEFINE_PointObject
326
327 HPyDef_SLOT(p_power, HPy_nb_power);
328 static HPy p_power_impl(HPyContext *ctx, HPy self, HPy x, HPy y)
329 {
330 HPy s = HPyUnicode_FromString(ctx, "power");
331 HPy res = HPyTuple_Pack(ctx, 4, self, s, x, y);
332 HPy_Close(ctx, s);
333 return res;
334 }
335
336 HPyDef_SLOT(p_inplace_power, HPy_nb_inplace_power);
337 static HPy p_inplace_power_impl(HPyContext *ctx, HPy self, HPy x, HPy y)
338 {
339 HPy s = HPyUnicode_FromString(ctx, "inplace_power");
340 HPy res = HPyTuple_Pack(ctx, 4, self, s, x, y);
341 HPy_Close(ctx, s);
342 return res;
343 }
344
345 @EXPORT_POINT_TYPE(&p_power, &p_inplace_power)
346 @INIT
347 """)
348 p = mod.Point()
349 assert p**42 == (p, 'power', 42, None)
350 assert pow(p, 42, 123) == (p, 'power', 42, 123)
351 tmp = p
352 tmp **= 42
353 assert tmp == (p, 'inplace_power', 42, None)
354
355 def test_buffer(self):
356 import pytest
357 import sys
358 import gc
359 mod = self.make_module("""
360 @TYPE_STRUCT_BEGIN(FakeArrayObject)
361 int exports;
362 @TYPE_STRUCT_END
363
364 static char static_mem[12] = {0,1,2,3,4,5,6,7,8,9,10,11};
365 static HPy_ssize_t _shape[1] = {12};
366 static HPy_ssize_t _strides[1] = {1};
367
368 HPyDef_SLOT_IMPL(FakeArray_getbuffer, _getbuffer_impl, HPy_bf_getbuffer)
369 static int _getbuffer_impl(HPyContext *ctx, HPy self, HPy_buffer* buf, int flags) {
370 FakeArrayObject *arr = FakeArrayObject_AsStruct(ctx, self);
371 if (arr->exports > 0) {
372 buf->obj = HPy_NULL;
373 HPyErr_SetString(ctx, ctx->h_BufferError,
374 "only one buffer allowed");
375 return -1;
376 }
377 arr->exports++;
378 buf->buf = static_mem;
379 buf->len = 12;
380 buf->itemsize = 1;
381 buf->readonly = 1;
382 buf->ndim = 1;
383 buf->format = (char*)"B";
384 buf->shape = _shape;
385 buf->strides = _strides;
386 buf->suboffsets = NULL;
387 buf->internal = NULL;
388 buf->obj = HPy_Dup(ctx, self);
389 return 0;
390 }
391
392 HPyDef_SLOT_IMPL(FakeArray_releasebuffer, _relbuffer_impl, HPy_bf_releasebuffer)
393 static void _relbuffer_impl(HPyContext *ctx, HPy h_obj, HPy_buffer* buf) {
394 FakeArrayObject *arr = FakeArrayObject_AsStruct(ctx, h_obj);
395 arr->exports--;
396 }
397
398 static HPyDef *FakeArray_defines[] = {
399 &FakeArray_getbuffer,
400 &FakeArray_releasebuffer,
401 NULL
402 };
403
404 static HPyType_Spec FakeArray_Spec = {
405 .name = "mytest.FakeArray",
406 .basicsize = sizeof(FakeArrayObject),
407 .builtin_shape = SHAPE(FakeArrayObject),
408 .defines = FakeArray_defines,
409 };
410
411 @EXPORT_TYPE("FakeArray", FakeArray_Spec)
412 @INIT
413 """)
414 arr = mod.FakeArray()
415 if self.supports_refcounts():
416 init_refcount = sys.getrefcount(arr)
417 with memoryview(arr) as mv:
418 with pytest.raises(BufferError):
419 mv2 = memoryview(arr)
420 if self.supports_refcounts():
421 assert sys.getrefcount(arr) == init_refcount + 1
422 for i in range(12):
423 assert mv[i] == i
424 gc.collect()
425 if self.supports_refcounts():
426 assert sys.getrefcount(arr) == init_refcount
427 mv2 = memoryview(arr) # doesn't raise
428
429 def test_tp_repr_and_tp_str(self):
430 mod = self.make_module("""
431 #include <stdio.h>
432
433 #define BUF_SIZE 128
434
435 @DEFINE_PointObject
436 @DEFINE_Point_new
437
438 static HPy
439 point_str_repr(HPyContext *ctx, HPy h, int str)
440 {
441 char buf[BUF_SIZE];
442 PointObject *p = PointObject_AsStruct(ctx, h);
443 snprintf(buf, BUF_SIZE, "%s(Point(%ld, %ld))",
444 (str ? "str" : "repr"), p->x, p->y);
445 return HPyUnicode_FromString(ctx, buf);
446 }
447
448 HPyDef_SLOT(Point_repr, HPy_tp_repr)
449 static HPy Point_repr_impl(HPyContext *ctx, HPy self)
450 {
451 return point_str_repr(ctx, self, 0);
452 }
453
454 HPyDef_SLOT(Point_str, HPy_tp_str)
455 static HPy Point_str_impl(HPyContext *ctx, HPy self)
456 {
457 return point_str_repr(ctx, self, 1);
458 }
459
460 @EXPORT_POINT_TYPE(&Point_new, &Point_str, &Point_repr)
461 @INIT
462 """)
463 p = mod.Point(1, 2)
464 assert str(p) == 'str(Point(1, 2))'
465 assert repr(p) == 'repr(Point(1, 2))'
466
467 def test_tp_hash(self):
468 import pytest
469 mod = self.make_module("""
470 @DEFINE_PointObject
471 @DEFINE_Point_new
472
473 HPyDef_SLOT(Point_hash, HPy_tp_hash)
474 static HPy_ssize_t Point_hash_impl(HPyContext *ctx, HPy self)
475 {
476 PointObject *p = PointObject_AsStruct(ctx, self);
477 if (p->x < 0) {
478 HPyErr_SetString(ctx, ctx->h_ValueError, "cannot hash Point object with negative x");
479 return -1;
480 }
481 return p->x + p->y;
482 }
483
484 @EXPORT_POINT_TYPE(&Point_new, &Point_hash)
485 @INIT
486 """)
487 p = mod.Point(1, 10)
488 assert p.__hash__() == 11
489 assert hash(p) == 11
490 # We expect that the slot wrapper accepts hash code -1 without
491 # complaining. This is not the case for built-in function 'hash'.
492 assert mod.Point(0, -1).__hash__() == -1
493 with pytest.raises(ValueError):
494 hash(mod.Point(-1, 10))
495
496
497 class TestSqSlots(HPyTest):
498
499 ExtensionTemplate = PointTemplate
500
501 def test_mp_subscript_and_mp_length(self):
502 mod = self.make_module("""
503 @DEFINE_PointObject
504
505 HPyDef_SLOT(Point_getitem, HPy_mp_subscript);
506 static HPy Point_getitem_impl(HPyContext *ctx, HPy self, HPy key)
507 {
508 HPy prefix = HPyUnicode_FromString(ctx, "key was: ");
509 HPy key_repr = HPy_Repr(ctx, key);
510 HPy res = HPy_Add(ctx, prefix, key_repr);
511 HPy_Close(ctx, key_repr);
512 HPy_Close(ctx, prefix);
513 return res;
514 }
515
516 HPyDef_SLOT(Point_length, HPy_mp_length);
517 static HPy_ssize_t Point_length_impl(HPyContext *ctx, HPy self)
518 {
519 return 1234;
520 }
521
522 @EXPORT_POINT_TYPE(&Point_getitem, &Point_length)
523 @INIT
524 """)
525 p = mod.Point()
526 class Dummy:
527 def __repr__(self):
528 return "Hello, World"
529 assert len(p) == 1234
530 assert p[4] == "key was: 4"
531 assert p["hello"] == "key was: 'hello'"
532 assert p[Dummy()] == "key was: Hello, World"
533
534 def test_mp_ass_subscript(self):
535 import pytest
536 mod = self.make_module("""
537 @DEFINE_PointObject
538 @DEFINE_Point_new
539 @DEFINE_Point_xy
540
541 HPyDef_SLOT(Point_len, HPy_mp_length);
542 static HPy_ssize_t Point_len_impl(HPyContext *ctx, HPy self)
543 {
544 return 2;
545 }
546
547 HPyDef_SLOT(Point_setitem, HPy_mp_ass_subscript);
548 static int Point_setitem_impl(HPyContext *ctx, HPy self, HPy key,
549 HPy h_value)
550 {
551 long value;
552 if (HPy_IsNull(h_value)) {
553 value = -123; // this is the del p[] case
554 } else {
555 value = HPyLong_AsLong(ctx, h_value);
556 if (HPyErr_Occurred(ctx))
557 return -1;
558 }
559 PointObject *point = PointObject_AsStruct(ctx, self);
560 const char *s_key = HPyUnicode_AsUTF8AndSize(ctx, key, NULL);
561 if (s_key == NULL) {
562 return -1;
563 }
564 if (s_key[0] == 'x') {
565 point->x = value;
566 } else if (s_key[0] == 'y') {
567 point->y = value;
568 } else {
569 HPyErr_SetString(ctx, ctx->h_KeyError, "invalid key");
570 return -1;
571 }
572 return 0;
573 }
574
575 @EXPORT_POINT_TYPE(&Point_new, &Point_x, &Point_y, &Point_len, &Point_setitem)
576 @INIT
577 """)
578 p = mod.Point(1, 2)
579 # check __setitem__
580 p['x'] = 100
581 assert p.x == 100
582 p['y'] = 200
583 assert p.y == 200
584 with pytest.raises(KeyError):
585 p['z'] = 300
586 with pytest.raises(TypeError):
587 p[0] = 300
588 # check __delitem__
589 del p['x']
590 assert p.x == -123
591 del p['y']
592 assert p.y == -123
593
594 def test_sq_item_and_sq_length(self):
595 mod = self.make_module("""
596 @DEFINE_PointObject
597
598 HPyDef_SLOT(Point_getitem, HPy_sq_item);
599 static HPy Point_getitem_impl(HPyContext *ctx, HPy self, HPy_ssize_t idx)
600 {
601 return HPyLong_FromLong(ctx, (long)idx*2);
602 }
603
604 HPyDef_SLOT(Point_length, HPy_sq_length);
605 static HPy_ssize_t Point_length_impl(HPyContext *ctx, HPy self)
606 {
607 return 1234;
608 }
609
610 @EXPORT_POINT_TYPE(&Point_getitem, &Point_length)
611 @INIT
612 """)
613 p = mod.Point()
614 assert len(p) == 1234
615 assert p[4] == 8
616 assert p[21] == 42
617 assert p[-1] == 1233 * 2
618
619 def test_sq_ass_item(self):
620 import pytest
621 mod = self.make_module("""
622 @DEFINE_PointObject
623 @DEFINE_Point_new
624 @DEFINE_Point_xy
625
626 HPyDef_SLOT(Point_len, HPy_sq_length);
627 static HPy_ssize_t Point_len_impl(HPyContext *ctx, HPy self)
628 {
629 return 2;
630 }
631
632 HPyDef_SLOT(Point_setitem, HPy_sq_ass_item);
633 static int Point_setitem_impl(HPyContext *ctx, HPy self, HPy_ssize_t idx,
634 HPy h_value)
635 {
636 long value;
637 if (HPy_IsNull(h_value))
638 value = -123; // this is the del p[] case
639 else {
640 value = HPyLong_AsLong(ctx, h_value);
641 if (HPyErr_Occurred(ctx))
642 return -1;
643 }
644 PointObject *point = PointObject_AsStruct(ctx, self);
645 if (idx == 0)
646 point->x = value;
647 else if (idx == 1)
648 point->y = value;
649 else {
650 HPyErr_SetString(ctx, ctx->h_IndexError, "invalid index");
651 return -1;
652 }
653 return 0;
654 }
655
656 @EXPORT_POINT_TYPE(&Point_new, &Point_x, &Point_y, &Point_len, &Point_setitem)
657 @INIT
658 """)
659 p = mod.Point(1, 2)
660 # check __setitem__
661 p[0] = 100
662 assert p.x == 100
663 p[1] = 200
664 assert p.y == 200
665 with pytest.raises(IndexError):
666 p[2] = 300
667 # check __delitem__
668 del p[0]
669 assert p.x == -123
670 del p[1]
671 assert p.y == -123
672 # check negative indexes
673 p[-2] = 400
674 p[-1] = 500
675 assert p.x == 400
676 assert p.y == 500
677 del p[-2]
678 assert p.x == -123
679 del p[-1]
680 assert p.y == -123
681
682 def test_sq_concat_and_sq_inplace_concat(self):
683 mod = self.make_module("""
684 @DEFINE_PointObject
685
686 HPyDef_SLOT(Point_concat, HPy_sq_concat);
687 static HPy Point_concat_impl(HPyContext *ctx, HPy self, HPy other)
688 {
689 HPy s = HPyUnicode_FromString(ctx, "sq_concat");
690 HPy res = HPyTuple_Pack(ctx, 3, self, s, other);
691 HPy_Close(ctx, s);
692 return res;
693 }
694
695 HPyDef_SLOT(Point_inplace_concat, HPy_sq_inplace_concat);
696 static HPy Point_inplace_concat_impl(HPyContext *ctx, HPy self, HPy other)
697 {
698 HPy s = HPyUnicode_FromString(ctx, "sq_inplace_concat");
699 HPy res = HPyTuple_Pack(ctx, 3, self, s, other);
700 HPy_Close(ctx, s);
701 return res;
702 }
703
704 @EXPORT_POINT_TYPE(&Point_concat, &Point_inplace_concat)
705 @INIT
706 """)
707 p = mod.Point()
708 res = p + 42
709 assert res == (p, "sq_concat", 42)
710 #
711 tmp = p
712 tmp += 43
713 assert tmp == (p, "sq_inplace_concat", 43)
714
715 def test_sq_repeat_and_sq_inplace_repeat(self):
716 mod = self.make_module("""
717 @DEFINE_PointObject
718
719 HPyDef_SLOT(Point_repeat, HPy_sq_repeat);
720 static HPy Point_repeat_impl(HPyContext *ctx, HPy self, HPy_ssize_t t)
721 {
722 HPy s = HPyUnicode_FromString(ctx, "sq_repeat");
723 HPy other = HPyLong_FromLong(ctx, (long) t);
724 HPy res = HPyTuple_Pack(ctx, 3, self, s, other);
725 HPy_Close(ctx, other);
726 HPy_Close(ctx, s);
727 return res;
728 }
729
730 HPyDef_SLOT(Point_inplace_repeat, HPy_sq_inplace_repeat);
731 static HPy Point_inplace_repeat_impl(HPyContext *ctx, HPy self, HPy_ssize_t t)
732 {
733 HPy s = HPyUnicode_FromString(ctx, "sq_inplace_repeat");
734 HPy other = HPyLong_FromLong(ctx, (long) t);
735 HPy res = HPyTuple_Pack(ctx, 3, self, s, other);
736 HPy_Close(ctx, other);
737 HPy_Close(ctx, s);
738 return res;
739 }
740
741 @EXPORT_POINT_TYPE(&Point_repeat, &Point_inplace_repeat)
742 @INIT
743 """)
744 p = mod.Point()
745 res = p * 42
746 assert res == (p, "sq_repeat", 42)
747 #
748 tmp = p
749 tmp *= 43
750 assert tmp == (p, "sq_inplace_repeat", 43)
751
752 def test_sq_contains(self):
753 import pytest
754 mod = self.make_module("""
755 @DEFINE_PointObject
756
757 HPyDef_SLOT(Point_contains, HPy_sq_contains);
758 static int Point_contains_impl(HPyContext *ctx, HPy self, HPy other)
759 {
760 long val = HPyLong_AsLong(ctx, other);
761 if (HPyErr_Occurred(ctx))
762 return -1;
763 if (val == 42)
764 return 1;
765 return 0;
766 }
767
768 @EXPORT_POINT_TYPE(&Point_contains)
769 @INIT
770 """)
771 p = mod.Point()
772 assert 42 in p
773 assert 43 not in p
774 with pytest.raises(TypeError):
775 'hello' in p
776
777 def test_tp_richcompare(self):
778 mod = self.make_module("""
779 @DEFINE_PointObject
780 @DEFINE_Point_new
781
782 HPyDef_SLOT(Point_cmp, HPy_tp_richcompare);
783 static HPy Point_cmp_impl(HPyContext *ctx, HPy self, HPy o, HPy_RichCmpOp op)
784 {
785 // XXX we should check the type of o
786 PointObject *p1 = PointObject_AsStruct(ctx, self);
787 PointObject *p2 = PointObject_AsStruct(ctx, o);
788 HPy_RETURN_RICHCOMPARE(ctx, p1->x, p2->x, op);
789 }
790
791 @EXPORT_POINT_TYPE(&Point_new, &Point_cmp)
792 @INIT
793 """)
794 p1 = mod.Point(10, 10)
795 p2 = mod.Point(20, 20)
796 assert p1 == p1
797 assert not p1 == p2
798 #
799 assert p1 != p2
800 assert not p1 != p1
801 #
802 assert p1 < p2
803 assert not p1 < p1
804 #
805 assert not p1 > p2
806 assert not p1 > p1
807 #
808 assert p1 <= p2
809 assert p1 <= p1
810 #
811 assert not p1 >= p2
812 assert p1 >= p1
813
814 def test_tp_descr_get(self):
815 mod = self.make_module("""
816 @DEFINE_PointObject
817 @DEFINE_Point_new
818
819 HPyDef_SLOT(Point_get, HPy_tp_descr_get);
820 static HPy
821 Point_get_impl(HPyContext *ctx, HPy self, HPy obj, HPy type)
822 {
823 if (HPy_IsNull(obj) || HPy_Is(ctx, self, ctx->h_None)) {
824 return HPy_Dup(ctx, self);
825 }
826 return HPyLong_FromLong(ctx, 123);
827 }
828
829 @EXPORT_POINT_TYPE(&Point_new, &Point_get)
830 @INIT
831 """)
832 p = mod.Point(10, 10)
833 class Dummy:
834 point_func = p
835 # assert Dummy.point_func is p
836 assert Dummy().point_func == 123