Debian Codegraph @

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