cython

Форк
0
/
special_methods_T561.pyx 
1125 строк · 30.1 Кб
1
# mode: run
2
# ticket: t561
3
# ticket: t3
4

5
# The patch in #561 changes code generation for most special methods
6
# to remove the Cython-generated wrapper and let PyType_Ready()
7
# generate its own wrapper.  (This wrapper would be used, for instance,
8
# when using the special method as a bound method.)
9

10
# To test this, we go through and verify that each affected special
11
# method works as a bound method.
12

13
# Special methods that are treated the same under Python 2 and 3 are
14
# tested here; see also special_methods_T561_py2.pyx and
15
# special_methods_T561_py3.pyx for tests of the differences between
16
# Python 2 and 3.
17

18
# Regarding ticket 3, we should additionally test that unbound method
19
# calls to these special methods (e.g. ExtType.__init__()) do not use
20
# a runtime lookup indirection.
21
#
22
# Additional tests added for 2 and 3 argument pow and ipow
23

24
import sys
25

26
from cpython.number cimport PyNumber_InPlacePower
27

28
__doc__ = u"""
29
    >>> # If you define either setitem or delitem, you get wrapper objects
30
    >>> # for both methods.  (This behavior is unchanged by #561.)
31
    >>> si_setitem = SetItem().__setitem__
32
    >>> si_setitem('foo', 'bar')
33
    SetItem setitem 'foo' 'bar'
34
    >>> si_delitem = SetItem().__delitem__
35
    >>> si_delitem('foo')
36
    Traceback (most recent call last):
37
    ...
38
    NotImplementedError: Subscript deletion not supported by special_methods_T561.SetItem
39
    >>> di_setitem = DelItem().__setitem__
40
    >>> di_setitem('foo', 'bar')
41
    Traceback (most recent call last):
42
    ...
43
    NotImplementedError: Subscript assignment not supported by special_methods_T561.DelItem
44
    >>> di_delitem = DelItem().__delitem__
45
    >>> di_delitem('foo')
46
    DelItem delitem 'foo'
47
    >>> sdi_setitem = SetDelItem().__setitem__
48
    >>> sdi_setitem('foo', 'bar')
49
    SetDelItem setitem 'foo' 'bar'
50
    >>> sdi_delitem = SetDelItem().__delitem__
51
    >>> sdi_delitem('foo')
52
    SetDelItem delitem 'foo'
53
    >>> g01 = object.__getattribute__(GetAttr(), '__getattribute__')
54
    >>> g01('attr')
55
    GetAttr getattr 'attr'
56
    >>> try: object.__getattribute__(GetAttribute(), '__getattr__')
57
    ... except AttributeError as err:
58
    ...      assert '__getattr__' in str(err), err
59
    ... else: print("NOT RAISED!")
60
    >>> g11 = object.__getattribute__(GetAttribute(), '__getattribute__')
61
    >>> g11('attr')
62
    GetAttribute getattribute 'attr'
63
    >>> # If you define either set or delete, you get wrapper objects
64
    >>> # for both methods.  (This behavior is unchanged by #561.)
65
    >>> s_set = Set().__set__
66
    >>> s_set('instance', 'val')
67
    Set set 'instance' 'val'
68
    >>> s_delete = Set().__delete__
69
    >>> s_delete('instance')
70
    Traceback (most recent call last):
71
    ...
72
    NotImplementedError: __delete__
73
    >>> d_set = Delete().__set__
74
    >>> d_set('instance', 'val')
75
    Traceback (most recent call last):
76
    ...
77
    NotImplementedError: __set__
78
    >>> d_delete = Delete().__delete__
79
    >>> d_delete('instance')
80
    Delete delete 'instance'
81
    >>> sd_set = SetDelete().__set__
82
    >>> sd_set('instance', 'val')
83
    SetDelete set 'instance' 'val'
84
    >>> sd_delete = SetDelete().__delete__
85
    >>> sd_delete('instance')
86
    SetDelete delete 'instance'
87
    >>> # If you define __long__, you get a wrapper object for __int__.
88
    >>> # (This behavior is unchanged by #561.)
89
    >>> Li = Long().__int__
90
    >>> Li()
91
    Long __long__
92
    >>> vs0 = VerySpecial(0)
93
    VS __init__ 0
94
    >>> vs0_index = vs0.__index__
95
    >>> vs0_index()
96
    VS __index__ 0
97
    >>> set_name = SetName()
98
    >>> assert "SetName 'SetName' 'attr'" == set_name.attr, set_name.attr
99
"""
100

101
cdef extern from *:
102
    # type specs require a bug fix in Py3.8+ for some of these tests.
103
    const int CYTHON_USE_TYPE_SPECS
104

105
if not CYTHON_USE_TYPE_SPECS or sys.version_info >= (3,8):
106
    __doc__ += u"""
107
    >>> # If you define either setattr or delattr, you get wrapper objects
108
    >>> # for both methods.  (This behavior is unchanged by #561.)
109
    >>> sa_setattr = SetAttr().__setattr__
110
    >>> sa_setattr('foo', 'bar')
111
    SetAttr setattr 'foo' 'bar'
112
    >>> sa_delattr = SetAttr().__delattr__
113
    >>> sa_delattr('foo')  # doctest: +ELLIPSIS
114
    Traceback (most recent call last):
115
    ...
116
    AttributeError: 'special_methods_T561.SetAttr' object has no attribute 'foo'...
117
    >>> da_setattr = DelAttr().__setattr__
118
    >>> da_setattr('foo', 'bar')  # doctest: +ELLIPSIS
119
    Traceback (most recent call last):
120
    ...
121
    AttributeError: 'special_methods_T561.DelAttr' object has no attribute 'foo'...
122
    >>> da_delattr = DelAttr().__delattr__
123
    >>> da_delattr('foo')
124
    DelAttr delattr 'foo'
125
    >>> sda_setattr = SetDelAttr().__setattr__
126
    >>> sda_setattr('foo', 'bar')
127
    SetDelAttr setattr 'foo' 'bar'
128
    >>> sda_delattr = SetDelAttr().__delattr__
129
    >>> sda_delattr('foo')
130
    SetDelAttr delattr 'foo'
131
"""
132

133

134
cdef class VerySpecial:
135
    """
136
    >>> vs0 = VerySpecial(0)
137
    VS __init__ 0
138
    >>> vs1 = VerySpecial(1)
139
    VS __init__ 1
140

141
    >>> vs0_add = vs0.__add__
142
    >>> vs0_add(vs1)
143
    VS __add__ 0 1
144
    >>> vs0_sub = vs0.__sub__
145
    >>> vs0_sub(vs1)
146
    VS __sub__ 0 1
147
    >>> vs0_mul = vs0.__mul__
148
    >>> vs0_mul(vs1)
149
    VS __mul__ 0 1
150
    >>> vs0_mod = vs0.__mod__
151
    >>> vs0_mod(vs1)
152
    VS __mod__ 0 1
153
    >>> vs0_divmod = vs0.__divmod__
154
    >>> vs0_divmod(vs1)
155
    VS __divmod__ 0 1
156
    >>> vs0_pow = vs0.__pow__
157
    >>> vs0_pow(vs1)
158
    VS __pow__ pow(0, 1, None)
159
    >>> vs0_pow(vs1, 13)
160
    VS __pow__ pow(0, 1, 13)
161
    >>> vs0_neg = vs0.__neg__
162
    >>> vs0_neg()
163
    VS __neg__ 0
164
    >>> vs0_pos = vs0.__pos__
165
    >>> vs0_pos()
166
    VS __pos__ 0
167
    >>> vs0_abs = vs0.__abs__
168
    >>> vs0_abs()
169
    VS __abs__ 0
170
    >>> vs0_invert = vs0.__invert__
171
    >>> vs0_invert()
172
    VS __invert__ 0
173
    >>> vs0_lshift = vs0.__lshift__
174
    >>> vs0_lshift(vs1)
175
    VS __lshift__ 0 << 1
176
    >>> vs0_rshift = vs0.__rshift__
177
    >>> vs0_rshift(vs1)
178
    VS __rshift__ 0 >> 1
179
    >>> vs0_and = vs0.__and__
180
    >>> vs0_and(vs1)
181
    VS __and__ 0 & 1
182
    >>> vs0_xor = vs0.__xor__
183
    >>> vs0_xor(vs1)
184
    VS __xor__ 0 ^ 1
185
    >>> vs0_or = vs0.__or__
186
    >>> vs0_or(vs1)
187
    VS __or__ 0 | 1
188
    >>> vs0_int = vs0.__int__
189
    >>> vs0_int()
190
    VS __int__ 0
191
    >>> vs0_float = vs0.__float__
192
    >>> vs0_float()
193
    VS __float__ 0
194
    >>> vs0_iadd = vs0.__iadd__
195
    >>> vs0_iadd(vs1)
196
    VS __iadd__ 0 += 1
197
    >>> vs0_isub = vs0.__isub__
198
    >>> vs0_isub(vs1)
199
    VS __isub__ 0 -= 1
200
    >>> vs0_imul = vs0.__imul__
201
    >>> vs0_imul(vs1)
202
    VS __imul__ 0 *= 1
203
    >>> vs0_imod = vs0.__imod__
204
    >>> vs0_imod(vs1)
205
    VS __imod__ 0 %= 1
206
    >>> vs0_ipow = vs0.__ipow__
207
    >>> vs0_ipow(vs1)
208
    VS __ipow__ 0 1
209
    >>> vs0_ilshift = vs0.__ilshift__
210
    >>> vs0_ilshift(vs1)
211
    VS __ilshift__ 0 <<= 1
212
    >>> vs0_irshift = vs0.__irshift__
213
    >>> vs0_irshift(vs1)
214
    VS __irshift__ 0 >>= 1
215
    >>> vs0_iand = vs0.__iand__
216
    >>> vs0_iand(vs1)
217
    VS __iand__ 0 &= 1
218
    >>> vs0_ixor = vs0.__ixor__
219
    >>> vs0_ixor(vs1)
220
    VS __ixor__ 0 ^= 1
221
    >>> vs0_ior = vs0.__ior__
222
    >>> vs0_ior(vs1)
223
    VS __ior__ 0 |= 1
224
    >>> vs0_floordiv = vs0.__floordiv__
225
    >>> vs0_floordiv(vs1)
226
    VS __floordiv__ 0 / 1
227
    >>> vs0_truediv = vs0.__truediv__
228
    >>> vs0_truediv(vs1)
229
    VS __truediv__ 0 / 1
230
    >>> vs0_ifloordiv = vs0.__ifloordiv__
231
    >>> vs0_ifloordiv(vs1)
232
    VS __ifloordiv__ 0 /= 1
233
    >>> vs0_itruediv = vs0.__itruediv__
234
    >>> vs0_itruediv(vs1)
235
    VS __itruediv__ 0 /= 1
236

237
    # If you define an arithmetic method, you get wrapper objects for
238
    # the reversed version as well.  (This behavior is unchanged by #561.)
239
    >>> vs0_radd = vs0.__radd__
240
    >>> vs0_radd(vs1)
241
    VS __add__ 1 0
242
    >>> vs0_rsub = vs0.__rsub__
243
    >>> vs0_rsub(vs1)
244
    VS __sub__ 1 0
245
    >>> vs0_rmul = vs0.__rmul__
246
    >>> vs0_rmul(vs1)
247
    VS __mul__ 1 0
248
    >>> vs0_rmod = vs0.__rmod__
249
    >>> vs0_rmod(vs1)
250
    VS __mod__ 1 0
251
    >>> vs0_rdivmod = vs0.__rdivmod__
252
    >>> vs0_rdivmod(vs1)
253
    VS __divmod__ 1 0
254
    >>> vs0_rpow = vs0.__rpow__
255
    >>> vs0_rpow(vs1)
256
    VS __pow__ pow(1, 0, None)
257
    >>> vs0_rlshift = vs0.__rlshift__
258
    >>> vs0_rlshift(vs1)
259
    VS __lshift__ 1 << 0
260
    >>> vs0_rrshift = vs0.__rrshift__
261
    >>> vs0_rrshift(vs1)
262
    VS __rshift__ 1 >> 0
263
    >>> vs0_rand = vs0.__rand__
264
    >>> vs0_rand(vs1)
265
    VS __and__ 1 & 0
266
    >>> vs0_rxor = vs0.__rxor__
267
    >>> vs0_rxor(vs1)
268
    VS __xor__ 1 ^ 0
269
    >>> vs0_ror = vs0.__ror__
270
    >>> vs0_ror(vs1)
271
    VS __or__ 1 | 0
272
    >>> vs0_rfloordiv = vs0.__rfloordiv__
273
    >>> vs0_rfloordiv(vs1)
274
    VS __floordiv__ 1 / 0
275
    >>> vs0_rtruediv = vs0.__rtruediv__
276
    >>> vs0_rtruediv(vs1)
277
    VS __truediv__ 1 / 0
278
    >>> vs0_getitem = vs0.__getitem__
279
    >>> vs0_getitem('foo')
280
    VS __getitem__ 0['foo']
281
    >>> vs0_contains = vs0.__contains__
282
    >>> vs0_contains(vs1)
283
    VS __contains__ 0 1
284
    False
285
    >>> vs0_len = vs0.__len__
286
    >>> vs0_len()
287
    VS __len__ 0
288
    0
289
    >>> vs0_repr = vs0.__repr__
290
    >>> vs0_repr()
291
    VS __repr__ 0
292
    >>> vs0_hash = vs0.__hash__
293
    >>> vs0_hash()
294
    VS __hash__ 0
295
    1000
296
    >>> vs0_call = vs0.__call__
297
    >>> vs0_call(vs1)
298
    VS __call__ 0(1)
299
    >>> vs0_str = vs0.__str__
300
    >>> vs0_str()
301
    VS __str__ 0
302

303
    # If you define __richcmp__, you get all of __lt__, __le__,
304
    # __eq__, __ne__, __gt__, __ge__ (this behavior is unchanged by #561).
305
    # (you don't get a __richcmp__ method, because it doesn't have a
306
    # Python signature)
307
    >>> vs0_lt = vs0.__lt__
308
    >>> vs0_lt(vs1)
309
    VS richcmp 0 1 (kind=0)
310
    >>> vs0_le = vs0.__le__
311
    >>> vs0_le(vs1)
312
    VS richcmp 0 1 (kind=1)
313
    >>> vs0_eq = vs0.__eq__
314
    >>> vs0_eq(vs1)
315
    VS richcmp 0 1 (kind=2)
316
    >>> vs0_ne = vs0.__ne__
317
    >>> vs0_ne(vs1)
318
    VS richcmp 0 1 (kind=3)
319
    >>> vs0_gt = vs0.__gt__
320
    >>> vs0_gt(vs1)
321
    VS richcmp 0 1 (kind=4)
322
    >>> vs0_ge = vs0.__ge__
323
    >>> vs0_ge(vs1)
324
    VS richcmp 0 1 (kind=5)
325
    >>> vs0_iter = vs0.__iter__
326
    >>> vs0_iter()
327
    VS __iter__ 0
328
    >>> vs0_next = vs0.__next__
329
    >>> vs0_next()
330
    VS next/__next__ 0
331

332
    >>> vs0_get = vs0.__get__
333
    >>> vs0_get('instance', 'owner')
334
    VS __get__ 0 'instance' 'owner'
335
    >>> vs0_init = vs0.__init__
336
    >>> vs0_init(0)
337
    VS __init__ 0
338
    """
339
    cdef readonly int value
340

341
    def __init__(self, v):
342
        self.value = v
343
        print "VS __init__ %d" % self.value
344

345
    def __add__(self, other):
346
        print "VS __add__ %d %d" % (self.value, other.value)
347

348
    def __sub__(self, other):
349
        print "VS __sub__ %d %d" % (self.value, other.value)
350

351
    def __mul__(self, other):
352
        print "VS __mul__ %d %d" % (self.value, other.value)
353

354
    def __div__(self, other):
355
        print "VS __div__ %d %d" % (self.value, other.value)
356

357
    def __mod__(self, other):
358
        print "VS __mod__ %d %d" % (self.value, other.value)
359

360
    def __divmod__(self, other):
361
        print "VS __divmod__ %d %d" % (self.value, other.value)
362

363
    def __pow__(self, other, mod):
364
        print "VS __pow__ pow(%d, %d, %r)" % (self.value, other.value, mod)
365

366
    def __lshift__(self, other):
367
        print "VS __lshift__ %d << %d" % (self.value, other.value)
368

369
    def __rshift__(self, other):
370
        print "VS __rshift__ %d >> %d" % (self.value, other.value)
371

372
    def __and__(self, other):
373
        print "VS __and__ %d & %d" % (self.value, other.value)
374

375
    def __xor__(self, other):
376
        print "VS __xor__ %d ^ %d" % (self.value, other.value)
377

378
    def __or__(self, other):
379
        print "VS __or__ %d | %d" % (self.value, other.value)
380

381
    def __floordiv__(self, other):
382
        print "VS __floordiv__ %d / %d" % (self.value, other.value)
383

384
    def __truediv__(self, other):
385
        print "VS __truediv__ %d / %d" % (self.value, other.value)
386

387
    def __neg__(self):
388
        print "VS __neg__ %d" % self.value
389

390
    def __pos__(self):
391
        print "VS __pos__ %d" % self.value
392

393
    def __abs__(self):
394
        print "VS __abs__ %d" % self.value
395

396
    def __nonzero__(self):
397
        print "VS __nonzero__ %d" % self.value
398

399
    def __invert__(self):
400
        print "VS __invert__ %d" % self.value
401

402
    def __int__(self):
403
        print "VS __int__ %d" % self.value
404

405
    def __long__(self):
406
        print "VS __long__ %d" % self.value
407

408
    def __float__(self):
409
        print "VS __float__ %d" % self.value
410

411
    def __oct__(self):
412
        print "VS __oct__ %d" % self.value
413

414
    def __hex__(self):
415
        print "VS __hex__ %d" % self.value
416

417
    def __iadd__(self, other):
418
        print "VS __iadd__ %d += %d" % (self.value, other.value)
419

420
    def __isub__(self, other):
421
        print "VS __isub__ %d -= %d" % (self.value, other.value)
422

423
    def __imul__(self, other):
424
        print "VS __imul__ %d *= %d" % (self.value, other.value)
425

426
    def __idiv__(self, other):
427
        print "VS __idiv__ %d /= %d" % (self.value, other.value)
428

429
    def __imod__(self, other):
430
        print "VS __imod__ %d %%= %d" % (self.value, other.value)
431

432
    def __ipow__(self, other):
433
        # We must declare mod as an argument, but we must not touch it
434
        # or we'll get a segfault.  See #562
435
        print "VS __ipow__ %d %d" % (self.value, other.value)
436

437
    def __ilshift__(self, other):
438
        print "VS __ilshift__ %d <<= %d" % (self.value, other.value)
439

440
    def __irshift__(self, other):
441
        print "VS __irshift__ %d >>= %d" % (self.value, other.value)
442

443
    def __iand__(self, other):
444
        print "VS __iand__ %d &= %d" % (self.value, other.value)
445

446
    def __ixor__(self, other):
447
        print "VS __ixor__ %d ^= %d" % (self.value, other.value)
448

449
    def __ior__(self, other):
450
        print "VS __ior__ %d |= %d" % (self.value, other.value)
451

452
    def __ifloordiv__(self, other):
453
        print "VS __ifloordiv__ %d /= %d" % (self.value, other.value)
454

455
    def __itruediv__(self, other):
456
        print "VS __itruediv__ %d /= %d" % (self.value, other.value)
457

458
    def __index__(self):
459
        print "VS __index__ %d" % self.value
460

461
    def __getitem__(self, index):
462
        print "VS __getitem__ %d[%r]" % (self.value, index)
463

464
    def __contains__(self, other):
465
        print "VS __contains__ %d %d" % (self.value, other.value)
466

467
    def __len__(self):
468
        print "VS __len__ %d" % (self.value)
469

470
    def __cmp__(self, other):
471
        print "VS __cmp__ %d %d" % (self.value, other.value)
472

473
    def __repr__(self):
474
        print "VS __repr__ %d" % self.value
475

476
    def __hash__(self):
477
        print "VS __hash__ %d" % self.value
478
        return self.value + 1000
479

480
    def __call__(self, other):
481
        print "VS __call__ %d(%d)" % (self.value, other.value)
482

483
    def __str__(self):
484
        print "VS __str__ %d" % self.value
485

486
    def __richcmp__(self, other, kind):
487
        print "VS richcmp %d %d (kind=%r)" % (self.value, other.value, kind)
488

489
    def __iter__(self):
490
        print "VS __iter__ %d" % self.value
491

492
    def __next__(self):
493
        print "VS next/__next__ %d" % self.value
494

495
    def __get__(self, inst, own):
496
        print "VS __get__ %d %r %r" % (self.value, inst, own)
497

498
cdef class SetItem:
499
    def __setitem__(self, index, value):
500
        print "SetItem setitem %r %r" % (index, value)
501

502
cdef class DelItem:
503
    def __delitem__(self, index):
504
        print "DelItem delitem %r" % index
505

506
cdef class SetDelItem:
507
    def __setitem__(self, index, value):
508
        print "SetDelItem setitem %r %r" % (index, value)
509

510
    def __delitem__(self, index):
511
        print "SetDelItem delitem %r" % index
512

513
cdef class GetAttr:
514
    def __getattr__(self, attr):
515
        print "GetAttr getattr %r" % attr
516

517
cdef class GetAttribute:
518
    def __getattribute__(self, attr):
519
        print "GetAttribute getattribute %r" % attr
520

521
cdef class SetAttr:
522
    def __setattr__(self, attr, val):
523
        print "SetAttr setattr %r %r" % (attr, val)
524

525
cdef class DelAttr:
526
    def __delattr__(self, attr):
527
        print "DelAttr delattr %r" % attr
528

529
cdef class SetDelAttr:
530
    def __setattr__(self, attr, val):
531
        print "SetDelAttr setattr %r %r" % (attr, val)
532

533
    def __delattr__(self, attr):
534
        print "SetDelAttr delattr %r" % attr
535

536
cdef class Set:
537
    def __set__(self, inst, val):
538
        print "Set set %r %r" % (inst, val)
539

540
cdef class Delete:
541
    def __delete__(self, inst):
542
        print "Delete delete %r" % inst
543

544
cdef class SetDelete:
545
    def __set__(self, inst, val):
546
        print "SetDelete set %r %r" % (inst, val)
547

548
    def __delete__(self, inst):
549
        print "SetDelete delete %r" % inst
550

551
cdef class Long:
552
    def __long__(self):
553
        print "Long __long__"
554

555
class _SetName:
556

557
    def __init__(self):
558
        self.set_name = None
559

560
    def __set_name__(self, owner, name):
561
        self.set_name = "SetName %r %r" % (owner.__name__, name)
562

563
    def __get__(self, inst, own):
564
        return self.set_name
565

566
cdef class SetName:
567
    attr = _SetName()
568

569
cdef class GetAttrGetItemRedirect:
570
    """
571
    >>> o = GetAttrGetItemRedirect()
572

573
    >>> assert o.item == o['item']
574
    >>> source, item_value = o.item
575
    >>> assert source == 'item', source
576

577
    >>> assert o['attr'] == o.attr
578
    >>> source, attr_value = o['attr']
579
    >>> assert source == 'attr', source
580

581
    >>> assert item_value is attr_value, repr((item_value, attr_value))
582
    """
583
    cdef object obj
584
    def __cinit__(self):
585
        self.obj = object()
586

587
    def __getattr__(self, name):
588
        if name == 'item':
589
            return self[name]
590
        return ('attr', self.obj)
591

592
    def __getitem__(self, key):
593
        if key == 'attr':
594
            return getattr(self, key)
595
        return ('item', self.obj)
596

597

598
# test unbound method usage in subtypes
599

600
cdef class VerySpecialSubType(VerySpecial):
601
    """
602
    >>> vs0 = VerySpecialSubType(0)
603
    VS __init__ 0
604
    >>> vs1 = VerySpecialSubType(1)
605
    VS __init__ 1
606

607
    >>> vs0_add = vs0.__add__
608
    >>> vs0_add(vs1)
609
    VS __add__ 0 1
610
    >>> vs0_sub = vs0.__sub__
611
    >>> vs0_sub(vs1)
612
    VS __sub__ 0 1
613
    >>> vs0_mul = vs0.__mul__
614
    >>> vs0_mul(vs1)
615
    VS __mul__ 0 1
616
    >>> vs0_mod = vs0.__mod__
617
    >>> vs0_mod(vs1)
618
    VS __mod__ 0 1
619
    >>> vs0_divmod = vs0.__divmod__
620
    >>> vs0_divmod(vs1)
621
    VS __divmod__ 0 1
622
    >>> vs0_pow = vs0.__pow__
623
    >>> vs0_pow(vs1)
624
    VS __pow__ pow(0, 1, None)
625
    >>> vs0_pow(vs1, 13)
626
    VS __pow__ pow(0, 1, 13)
627
    >>> vs0_neg = vs0.__neg__
628
    >>> vs0_neg()
629
    VS __neg__ 0
630
    >>> vs0_pos = vs0.__pos__
631
    >>> vs0_pos()
632
    VS __pos__ 0
633
    >>> vs0_abs = vs0.__abs__
634
    >>> vs0_abs()
635
    VS __abs__ 0
636
    >>> vs0_invert = vs0.__invert__
637
    >>> vs0_invert()
638
    VS __invert__ 0
639
    >>> vs0_lshift = vs0.__lshift__
640
    >>> vs0_lshift(vs1)
641
    VS __lshift__ 0 << 1
642
    >>> vs0_rshift = vs0.__rshift__
643
    >>> vs0_rshift(vs1)
644
    VS __rshift__ 0 >> 1
645
    >>> vs0_and = vs0.__and__
646
    >>> vs0_and(vs1)
647
    VS __and__ 0 & 1
648
    >>> vs0_xor = vs0.__xor__
649
    >>> vs0_xor(vs1)
650
    VS __xor__ 0 ^ 1
651
    >>> vs0_or = vs0.__or__
652
    >>> vs0_or(vs1)
653
    VS __or__ 0 | 1
654
    >>> vs0_int = vs0.__int__
655
    >>> vs0_int()
656
    VS __int__ 0
657
    >>> vs0_float = vs0.__float__
658
    >>> vs0_float()
659
    VS __float__ 0
660
    >>> vs0_iadd = vs0.__iadd__
661
    >>> vs0_iadd(vs1)
662
    VS __iadd__ 0 += 1
663
    >>> vs0_isub = vs0.__isub__
664
    >>> vs0_isub(vs1)
665
    VS __isub__ 0 -= 1
666
    >>> vs0_imul = vs0.__imul__
667
    >>> vs0_imul(vs1)
668
    VS __imul__ 0 *= 1
669
    >>> vs0_imod = vs0.__imod__
670
    >>> vs0_imod(vs1)
671
    VS __imod__ 0 %= 1
672
    >>> vs0_ipow = vs0.__ipow__
673
    >>> vs0_ipow(vs1)
674
    VS __ipow__ 0 1
675
    >>> vs0_ilshift = vs0.__ilshift__
676
    >>> vs0_ilshift(vs1)
677
    VS __ilshift__ 0 <<= 1
678
    >>> vs0_irshift = vs0.__irshift__
679
    >>> vs0_irshift(vs1)
680
    VS __irshift__ 0 >>= 1
681
    >>> vs0_iand = vs0.__iand__
682
    >>> vs0_iand(vs1)
683
    VS __iand__ 0 &= 1
684
    >>> vs0_ixor = vs0.__ixor__
685
    >>> vs0_ixor(vs1)
686
    VS __ixor__ 0 ^= 1
687
    >>> vs0_ior = vs0.__ior__
688
    >>> vs0_ior(vs1)
689
    VS __ior__ 0 |= 1
690
    >>> vs0_floordiv = vs0.__floordiv__
691
    >>> vs0_floordiv(vs1)
692
    VS __floordiv__ 0 / 1
693
    >>> vs0_truediv = vs0.__truediv__
694
    >>> vs0_truediv(vs1)
695
    VS __truediv__ 0 / 1
696
    >>> vs0_ifloordiv = vs0.__ifloordiv__
697
    >>> vs0_ifloordiv(vs1)
698
    VS __ifloordiv__ 0 /= 1
699
    >>> vs0_itruediv = vs0.__itruediv__
700
    >>> vs0_itruediv(vs1)
701
    VS __itruediv__ 0 /= 1
702

703
    # If you define an arithmetic method, you get wrapper objects for
704
    # the reversed version as well.  (This behavior is unchanged by #561.)
705
    >>> vs0_radd = vs0.__radd__
706
    >>> vs0_radd(vs1)
707
    VS __add__ 1 0
708
    >>> vs0_rsub = vs0.__rsub__
709
    >>> vs0_rsub(vs1)
710
    VS __sub__ 1 0
711
    >>> vs0_rmul = vs0.__rmul__
712
    >>> vs0_rmul(vs1)
713
    VS __mul__ 1 0
714
    >>> vs0_rmod = vs0.__rmod__
715
    >>> vs0_rmod(vs1)
716
    VS __mod__ 1 0
717
    >>> vs0_rdivmod = vs0.__rdivmod__
718
    >>> vs0_rdivmod(vs1)
719
    VS __divmod__ 1 0
720
    >>> vs0_rpow = vs0.__rpow__
721
    >>> vs0_rpow(vs1)
722
    VS __pow__ pow(1, 0, None)
723
    >>> vs0_rlshift = vs0.__rlshift__
724
    >>> vs0_rlshift(vs1)
725
    VS __lshift__ 1 << 0
726
    >>> vs0_rrshift = vs0.__rrshift__
727
    >>> vs0_rrshift(vs1)
728
    VS __rshift__ 1 >> 0
729
    >>> vs0_rand = vs0.__rand__
730
    >>> vs0_rand(vs1)
731
    VS __and__ 1 & 0
732
    >>> vs0_rxor = vs0.__rxor__
733
    >>> vs0_rxor(vs1)
734
    VS __xor__ 1 ^ 0
735
    >>> vs0_ror = vs0.__ror__
736
    >>> vs0_ror(vs1)
737
    VS __or__ 1 | 0
738
    >>> vs0_rfloordiv = vs0.__rfloordiv__
739
    >>> vs0_rfloordiv(vs1)
740
    VS __floordiv__ 1 / 0
741
    >>> vs0_rtruediv = vs0.__rtruediv__
742
    >>> vs0_rtruediv(vs1)
743
    VS __truediv__ 1 / 0
744
    >>> vs0_getitem = vs0.__getitem__
745
    >>> vs0_getitem('foo')
746
    VS __getitem__ 0['foo']
747
    >>> vs0_contains = vs0.__contains__
748
    >>> vs0_contains(vs1)
749
    VS __contains__ 0 1
750
    False
751
    >>> vs0_len = vs0.__len__
752
    >>> vs0_len()
753
    VS __len__ 0
754
    0
755
    >>> vs0_repr = vs0.__repr__
756
    >>> vs0_repr()
757
    VS __repr__ 0
758
    >>> vs0_hash = vs0.__hash__
759
    >>> vs0_hash()
760
    VS __hash__ 0
761
    1000
762
    >>> vs0_call = vs0.__call__
763
    >>> vs0_call(vs1)
764
    VS __call__ 0(1)
765
    >>> vs0_str = vs0.__str__
766
    >>> vs0_str()
767
    VS __str__ 0
768
    >>> vs0_lt = vs0.__lt__
769
    >>> vs0_lt(vs1)
770
    VS richcmp 0 1 (kind=0)
771
    >>> vs0_le = vs0.__le__
772
    >>> vs0_le(vs1)
773
    VS richcmp 0 1 (kind=1)
774
    >>> vs0_eq = vs0.__eq__
775
    >>> vs0_eq(vs1)
776
    VS richcmp 0 1 (kind=2)
777
    >>> vs0_ne = vs0.__ne__
778
    >>> vs0_ne(vs1)
779
    VS richcmp 0 1 (kind=3)
780
    >>> vs0_gt = vs0.__gt__
781
    >>> vs0_gt(vs1)
782
    VS richcmp 0 1 (kind=4)
783
    >>> vs0_ge = vs0.__ge__
784
    >>> vs0_ge(vs1)
785
    VS richcmp 0 1 (kind=5)
786
    >>> vs0_iter = vs0.__iter__
787
    >>> vs0_iter()
788
    VS __iter__ 0
789
    >>> vs0_next = vs0.__next__
790
    >>> vs0_next()
791
    VS next/__next__ 0
792
    >>> vs0_get = vs0.__get__
793
    >>> vs0_get('instance', 'owner')
794
    VS __get__ 0 'instance' 'owner'
795
    >>> vs0_init = vs0.__init__
796
    >>> vs0_init(0)
797
    VS __init__ 0
798
    """
799
    def __init__(self, v):
800
        VerySpecial.__init__(self, v)
801

802
    def __add__(self, other):
803
        return VerySpecial.__add__(self, other)
804

805
    def __sub__(self, other):
806
        return VerySpecial.__sub__(self, other)
807

808
    def __mul__(self, other):
809
        return VerySpecial.__mul__(self, other)
810

811
    def __div__(self, other):
812
        return VerySpecial.__div__(self, other)
813

814
    def __mod__(self, other):
815
        return VerySpecial.__mod__(self, other)
816

817
    def __divmod__(self, other):
818
        return VerySpecial.__divmod__(self, other)
819

820
    def __pow__(self, other, mod):
821
        return VerySpecial.__pow__(self, other, mod)
822

823
    def __lshift__(self, other):
824
        return VerySpecial.__lshift__(self, other)
825

826
    def __rshift__(self, other):
827
        return VerySpecial.__rshift__(self, other)
828

829
    def __and__(self, other):
830
        return VerySpecial.__and__(self, other)
831

832
    def __xor__(self, other):
833
        return VerySpecial.__xor__(self, other)
834

835
    def __or__(self, other):
836
        return VerySpecial.__or__(self, other)
837

838
    def __floordiv__(self, other):
839
        return VerySpecial.__floordiv__(self, other)
840

841
    def __truediv__(self, other):
842
        return VerySpecial.__truediv__(self, other)
843

844
    def __neg__(self):
845
        return VerySpecial.__neg__(self)
846

847
    def __pos__(self):
848
        return VerySpecial.__pos__(self)
849

850
    def __abs__(self):
851
        return VerySpecial.__abs__(self)
852

853
    def __nonzero__(self):
854
        return VerySpecial.__nonzero__(self)
855

856
    def __invert__(self):
857
        return VerySpecial.__invert__(self)
858

859
    def __int__(self):
860
        return VerySpecial.__int__(self)
861

862
    def __long__(self):
863
        return VerySpecial.__long__(self)
864

865
    def __float__(self):
866
        return VerySpecial.__float__(self)
867

868
    def __oct__(self):
869
        return VerySpecial.__oct__(self)
870

871
    def __hex__(self):
872
        return VerySpecial.__hex__(self)
873

874
    def __iadd__(self, other):
875
        return VerySpecial.__iadd__(self, other)
876

877
    def __isub__(self, other):
878
        return VerySpecial.__isub__(self, other)
879

880
    def __imul__(self, other):
881
        return VerySpecial.__imul__(self, other)
882

883
    def __idiv__(self, other):
884
        return VerySpecial.__idiv__(self, other)
885

886
    def __imod__(self, other):
887
        return VerySpecial.__imod__(self, other)
888

889
    def __ipow__(self, other):
890
        return VerySpecial.__ipow__(self, other)
891

892
    def __ilshift__(self, other):
893
        return VerySpecial.__ilshift__(self, other)
894

895
    def __irshift__(self, other):
896
        return VerySpecial.__irshift__(self, other)
897

898
    def __iand__(self, other):
899
        return VerySpecial.__iand__(self, other)
900

901
    def __ixor__(self, other):
902
        return VerySpecial.__ixor__(self, other)
903

904
    def __ior__(self, other):
905
        return VerySpecial.__ior__(self, other)
906

907
    def __ifloordiv__(self, other):
908
        return VerySpecial.__ifloordiv__(self, other)
909

910
    def __itruediv__(self, other):
911
        return VerySpecial.__itruediv__(self, other)
912

913
    def __index__(self):
914
        return VerySpecial.__index__(self)
915

916
    def __getitem__(self, index):
917
        return VerySpecial.__getitem__(self, index)
918

919
    def __contains__(self, other):
920
        return VerySpecial.__contains__(self, other)
921

922
    def __len__(self):
923
        return VerySpecial.__len__(self)
924

925
    def __cmp__(self, other):
926
        return VerySpecial.__cmp__(self, other)
927

928
    def __repr__(self):
929
        return VerySpecial.__repr__(self)
930

931
    def __hash__(self):
932
        return VerySpecial.__hash__(self)
933

934
    def __call__(self, arg):
935
        return VerySpecial.__call__(self, arg)
936

937
    def __str__(self):
938
        return VerySpecial.__str__(self)
939

940
# there is no __richcmp__ at the Python level
941
#    def __richcmp__(self, other, kind):
942
#        return VerySpecial.__richcmp__(self, other, kind)
943

944
    def __iter__(self):
945
        return VerySpecial.__iter__(self)
946

947
    def __next__(self):
948
        return VerySpecial.__next__(self)
949

950
    def __get__(self, inst, own):
951
        return VerySpecial.__get__(self, inst, own)
952

953

954
cdef class ReverseMethodsExist:
955
    """
956
    reverse methods (such as __radd__) don't work in Cython <3. However, if they
957
    are defined then it should be possible to look them up explicitly instead of
958
    looking up autogenerated wrapper (which points to the forward method)
959

960
    >>> o = ReverseMethodsExist()
961
    >>> o + o
962
    'add'
963
    >>> o.__add__(o)
964
    'add'
965
    >>> o.__radd__(o)
966
    'radd'
967
    >>> o.__rsub__(o)
968
    'rsub'
969
    """
970
    def __add__(self, other):
971
        return "add"
972
    def __radd__(self, other):
973
        return "radd"
974
    def __rsub__(self, other):
975
        return "rsub"
976

977

978
cdef class ArgumentTypeConversions:
979
    """
980
    The user can set the signature of special method arguments so that
981
    it doesn't match the C signature. This just tests that a few
982
    variations work
983

984
    >>> obj = ArgumentTypeConversions()
985
    >>> obj[1]
986
    1
987
    >>> obj["not a number!"]
988
    Traceback (most recent call last):
989
    ...
990
    TypeError: an integer is required
991
    >>> obj < obj
992
    In comparison 0
993
    True
994
    >>> obj == obj
995
    In comparison 2
996
    False
997

998
    Here I'm not sure how reproducible the flags are between Python versions.
999
    Therefore I'm just checking that they end with ".0"
1000
    >>> memoryview(obj)  # doctest:+ELLIPSIS
1001
    Traceback (most recent call last):
1002
    ...
1003
    RuntimeError: From __getbuffer__ with flags ....0
1004
    """
1005
    # force conversion of object to int
1006
    def __getitem__(self, int x):
1007
        return x
1008

1009
    # force conversion of comparison (int) to object
1010
    def __richcmp__(self, other, object comparison):
1011
        print "In comparison", comparison
1012
        return not bool(comparison)
1013

1014
    # force conversion of flags (int) to double
1015
    def __getbuffer__(self, Py_buffer *buffer, double flags):
1016
        raise RuntimeError("From __getbuffer__ with flags {}".format(flags))
1017

1018

1019
cdef class TwoArgPow:
1020
    """
1021
    >>> print(TwoArgPow('a')**2)
1022
    a**2
1023
    >>> print(pow(TwoArgPow('a'), 3))
1024
    a**3
1025
    >>> pow(TwoArgPow('a'), 'x', 'y')
1026
    Traceback (most recent call last):
1027
        ...
1028
    TypeError: special_methods_T561.TwoArgPow.__pow__() takes 3 arguments but 2 were given
1029
    """
1030
    cdef str name
1031

1032
    def __init__(self, name):
1033
        self.name = name
1034

1035
    def __pow__(self, other):
1036
        return f"{self.name}**{other}"
1037

1038

1039
cdef class TwoArgRPow:
1040
    """
1041
    >>> print(2**TwoArgRPow('a'))
1042
    a**2
1043
    >>> print(pow(3, TwoArgRPow('a')))
1044
    a**3
1045
    >>> pow('x', TwoArgRPow('a'), 'y')
1046
    Traceback (most recent call last):
1047
        ...
1048
    TypeError: special_methods_T561.TwoArgRPow.__rpow__() takes 3 arguments but 2 were given
1049
    """
1050
    cdef str name
1051

1052
    def __init__(self, name):
1053
        self.name = name
1054

1055
    def __rpow__(self, other):
1056
        return f"{self.name}**{other}"
1057

1058

1059
def ipow(a, b, c):
1060
    # As far as DW can tell, calling through this C API call is the
1061
    # only way to actually use ternary __ipow__
1062
    return PyNumber_InPlacePower(a, b, c)
1063

1064

1065
cdef class TwoArgIPow:
1066
    """
1067
    >>> a = TwoArgIPow('a')
1068
    >>> a**=2
1069
    >>> print(a)
1070
    a**2
1071
    """
1072
    cdef str name
1073

1074
    def __init__(self, name):
1075
        self.name = name
1076

1077
    def __ipow__(self, other):
1078
        return f"{self.name}**{other}"
1079

1080
if sys.version_info >= (3, 8):
1081
    # Due to a bug this check can't usefully work in Python <3.8
1082
    __doc__ += """
1083
>>> ipow(TwoArgIPow('a'), 'x', 'y')
1084
Traceback (most recent call last):
1085
    ...
1086
TypeError: special_methods_T561.TwoArgIPow.__ipow__() takes 3 arguments but 2 were given
1087
    """
1088

1089

1090
cdef class TwoOrThreeArgPow:
1091
    """
1092
    >>> print(TwoOrThreeArgPow('a')**2)
1093
    a**2[None]
1094
    >>> print(pow(TwoOrThreeArgPow('a'), 3))
1095
    a**3[None]
1096
    >>> print(pow(TwoOrThreeArgPow('a'), 'x', 'y'))
1097
    a**x[y]
1098
    """
1099
    cdef str name
1100

1101
    def __init__(self, name):
1102
        self.name = name
1103

1104
    def __pow__(self, other, base=None):
1105
        return f"{self.name}**{other}[{base}]"
1106

1107

1108
cdef class TwoOrThreeArgRPow:
1109
    """
1110
    >>> print(2**TwoOrThreeArgRPow('a'))
1111
    a**2[None]
1112
    >>> print(pow(3, TwoOrThreeArgRPow('a')))
1113
    a**3[None]
1114
    >>> print(pow('x', TwoOrThreeArgRPow('a'), 'y'))
1115
    a**x[y]
1116
    """
1117
    cdef str name
1118

1119
    def __init__(self, name):
1120
        self.name = name
1121

1122
    def __rpow__(self, other, base=None):
1123
        return f"{self.name}**{other}[{base}]"
1124

1125
# See special_methods_T561_py38 for some extra tests for ipow
1126

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.