summaryrefslogtreecommitdiff
path: root/tests/run/special_methods_T561.pyx
blob: 5eb9dddfc79d551becd0a04984d60d8c00eb5e11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
# mode: run
# ticket: t561
# ticket: t3

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

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

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

# Regarding ticket 3, we should additionally test that unbound method
# calls to these special methods (e.g. ExtType.__init__()) do not use
# a runtime lookup indirection.

import sys

__doc__ = u"""
    >>> # If you define either setitem or delitem, you get wrapper objects
    >>> # for both methods.  (This behavior is unchanged by #561.)
    >>> si_setitem = SetItem().__setitem__
    >>> si_setitem('foo', 'bar')
    SetItem setitem 'foo' 'bar'
    >>> si_delitem = SetItem().__delitem__
    >>> si_delitem('foo')
    Traceback (most recent call last):
    ...
    NotImplementedError: Subscript deletion not supported by special_methods_T561.SetItem
    >>> di_setitem = DelItem().__setitem__
    >>> di_setitem('foo', 'bar')
    Traceback (most recent call last):
    ...
    NotImplementedError: Subscript assignment not supported by special_methods_T561.DelItem
    >>> di_delitem = DelItem().__delitem__
    >>> di_delitem('foo')
    DelItem delitem 'foo'
    >>> sdi_setitem = SetDelItem().__setitem__
    >>> sdi_setitem('foo', 'bar')
    SetDelItem setitem 'foo' 'bar'
    >>> sdi_delitem = SetDelItem().__delitem__
    >>> sdi_delitem('foo')
    SetDelItem delitem 'foo'
    >>> g01 = object.__getattribute__(GetAttr(), '__getattribute__')
    >>> g01('attr')
    GetAttr getattr 'attr'
    >>> g10 = object.__getattribute__(GetAttribute(), '__getattr__')
    Traceback (most recent call last):
    ...
    AttributeError: 'special_methods_T561.GetAttribute' object has no attribute '__getattr__'
    >>> g11 = object.__getattribute__(GetAttribute(), '__getattribute__')
    >>> g11('attr')
    GetAttribute getattribute 'attr'
    >>> # If you define either set or delete, you get wrapper objects
    >>> # for both methods.  (This behavior is unchanged by #561.)
    >>> s_set = Set().__set__
    >>> s_set('instance', 'val')
    Set set 'instance' 'val'
    >>> s_delete = Set().__delete__
    >>> s_delete('instance')
    Traceback (most recent call last):
    ...
    NotImplementedError: __delete__
    >>> d_set = Delete().__set__
    >>> d_set('instance', 'val')
    Traceback (most recent call last):
    ...
    NotImplementedError: __set__
    >>> d_delete = Delete().__delete__
    >>> d_delete('instance')
    Delete delete 'instance'
    >>> sd_set = SetDelete().__set__
    >>> sd_set('instance', 'val')
    SetDelete set 'instance' 'val'
    >>> sd_delete = SetDelete().__delete__
    >>> sd_delete('instance')
    SetDelete delete 'instance'
    >>> # If you define __long__, you get a wrapper object for __int__.
    >>> # (This behavior is unchanged by #561.)
    >>> Li = Long().__int__
    >>> Li()
    Long __long__
"""
if sys.version_info >= (2,5):
    __doc__ += u"""\
    >>> vs0 = VerySpecial(0)
    VS __init__ 0
    >>> vs0_index = vs0.__index__
    >>> vs0_index()
    VS __index__ 0
"""

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

if not CYTHON_USE_TYPE_SPECS or sys.version_info >= (3,8):
    __doc__ += u"""
    >>> # If you define either setattr or delattr, you get wrapper objects
    >>> # for both methods.  (This behavior is unchanged by #561.)
    >>> sa_setattr = SetAttr().__setattr__
    >>> sa_setattr('foo', 'bar')
    SetAttr setattr 'foo' 'bar'
    >>> sa_delattr = SetAttr().__delattr__
    >>> sa_delattr('foo')
    Traceback (most recent call last):
    ...
    AttributeError: 'special_methods_T561.SetAttr' object has no attribute 'foo'
    >>> da_setattr = DelAttr().__setattr__
    >>> da_setattr('foo', 'bar')
    Traceback (most recent call last):
    ...
    AttributeError: 'special_methods_T561.DelAttr' object has no attribute 'foo'
    >>> da_delattr = DelAttr().__delattr__
    >>> da_delattr('foo')
    DelAttr delattr 'foo'
    >>> sda_setattr = SetDelAttr().__setattr__
    >>> sda_setattr('foo', 'bar')
    SetDelAttr setattr 'foo' 'bar'
    >>> sda_delattr = SetDelAttr().__delattr__
    >>> sda_delattr('foo')
    SetDelAttr delattr 'foo'
"""


cdef class VerySpecial:
    """
    >>> vs0 = VerySpecial(0)
    VS __init__ 0
    >>> vs1 = VerySpecial(1)
    VS __init__ 1

    >>> vs0_add = vs0.__add__
    >>> vs0_add(vs1)
    VS __add__ 0 1
    >>> vs0_sub = vs0.__sub__
    >>> vs0_sub(vs1)
    VS __sub__ 0 1
    >>> vs0_mul = vs0.__mul__
    >>> vs0_mul(vs1)
    VS __mul__ 0 1
    >>> vs0_mod = vs0.__mod__
    >>> vs0_mod(vs1)
    VS __mod__ 0 1
    >>> vs0_divmod = vs0.__divmod__
    >>> vs0_divmod(vs1)
    VS __divmod__ 0 1
    >>> vs0_pow = vs0.__pow__
    >>> vs0_pow(vs1)
    VS __pow__ pow(0, 1, None)
    >>> vs0_pow(vs1, 13)
    VS __pow__ pow(0, 1, 13)
    >>> vs0_neg = vs0.__neg__
    >>> vs0_neg()
    VS __neg__ 0
    >>> vs0_pos = vs0.__pos__
    >>> vs0_pos()
    VS __pos__ 0
    >>> vs0_abs = vs0.__abs__
    >>> vs0_abs()
    VS __abs__ 0
    >>> vs0_invert = vs0.__invert__
    >>> vs0_invert()
    VS __invert__ 0
    >>> vs0_lshift = vs0.__lshift__
    >>> vs0_lshift(vs1)
    VS __lshift__ 0 << 1
    >>> vs0_rshift = vs0.__rshift__
    >>> vs0_rshift(vs1)
    VS __rshift__ 0 >> 1
    >>> vs0_and = vs0.__and__
    >>> vs0_and(vs1)
    VS __and__ 0 & 1
    >>> vs0_xor = vs0.__xor__
    >>> vs0_xor(vs1)
    VS __xor__ 0 ^ 1
    >>> vs0_or = vs0.__or__
    >>> vs0_or(vs1)
    VS __or__ 0 | 1
    >>> vs0_int = vs0.__int__
    >>> vs0_int()
    VS __int__ 0
    >>> vs0_float = vs0.__float__
    >>> vs0_float()
    VS __float__ 0
    >>> vs0_iadd = vs0.__iadd__
    >>> vs0_iadd(vs1)
    VS __iadd__ 0 += 1
    >>> vs0_isub = vs0.__isub__
    >>> vs0_isub(vs1)
    VS __isub__ 0 -= 1
    >>> vs0_imul = vs0.__imul__
    >>> vs0_imul(vs1)
    VS __imul__ 0 *= 1
    >>> vs0_imod = vs0.__imod__
    >>> vs0_imod(vs1)
    VS __imod__ 0 %= 1
    >>> vs0_ipow = vs0.__ipow__
    >>> vs0_ipow(vs1)
    VS __ipow__ 0 1
    >>> vs0_ilshift = vs0.__ilshift__
    >>> vs0_ilshift(vs1)
    VS __ilshift__ 0 <<= 1
    >>> vs0_irshift = vs0.__irshift__
    >>> vs0_irshift(vs1)
    VS __irshift__ 0 >>= 1
    >>> vs0_iand = vs0.__iand__
    >>> vs0_iand(vs1)
    VS __iand__ 0 &= 1
    >>> vs0_ixor = vs0.__ixor__
    >>> vs0_ixor(vs1)
    VS __ixor__ 0 ^= 1
    >>> vs0_ior = vs0.__ior__
    >>> vs0_ior(vs1)
    VS __ior__ 0 |= 1
    >>> vs0_floordiv = vs0.__floordiv__
    >>> vs0_floordiv(vs1)
    VS __floordiv__ 0 / 1
    >>> vs0_truediv = vs0.__truediv__
    >>> vs0_truediv(vs1)
    VS __truediv__ 0 / 1
    >>> vs0_ifloordiv = vs0.__ifloordiv__
    >>> vs0_ifloordiv(vs1)
    VS __ifloordiv__ 0 /= 1
    >>> vs0_itruediv = vs0.__itruediv__
    >>> vs0_itruediv(vs1)
    VS __itruediv__ 0 /= 1

    # If you define an arithmetic method, you get wrapper objects for
    # the reversed version as well.  (This behavior is unchanged by #561.)
    >>> vs0_radd = vs0.__radd__
    >>> vs0_radd(vs1)
    VS __add__ 1 0
    >>> vs0_rsub = vs0.__rsub__
    >>> vs0_rsub(vs1)
    VS __sub__ 1 0
    >>> vs0_rmul = vs0.__rmul__
    >>> vs0_rmul(vs1)
    VS __mul__ 1 0
    >>> vs0_rmod = vs0.__rmod__
    >>> vs0_rmod(vs1)
    VS __mod__ 1 0
    >>> vs0_rdivmod = vs0.__rdivmod__
    >>> vs0_rdivmod(vs1)
    VS __divmod__ 1 0
    >>> vs0_rpow = vs0.__rpow__
    >>> vs0_rpow(vs1)
    VS __pow__ pow(1, 0, None)
    >>> vs0_rlshift = vs0.__rlshift__
    >>> vs0_rlshift(vs1)
    VS __lshift__ 1 << 0
    >>> vs0_rrshift = vs0.__rrshift__
    >>> vs0_rrshift(vs1)
    VS __rshift__ 1 >> 0
    >>> vs0_rand = vs0.__rand__
    >>> vs0_rand(vs1)
    VS __and__ 1 & 0
    >>> vs0_rxor = vs0.__rxor__
    >>> vs0_rxor(vs1)
    VS __xor__ 1 ^ 0
    >>> vs0_ror = vs0.__ror__
    >>> vs0_ror(vs1)
    VS __or__ 1 | 0
    >>> vs0_rfloordiv = vs0.__rfloordiv__
    >>> vs0_rfloordiv(vs1)
    VS __floordiv__ 1 / 0
    >>> vs0_rtruediv = vs0.__rtruediv__
    >>> vs0_rtruediv(vs1)
    VS __truediv__ 1 / 0
    >>> vs0_getitem = vs0.__getitem__
    >>> vs0_getitem('foo')
    VS __getitem__ 0['foo']
    >>> vs0_contains = vs0.__contains__
    >>> vs0_contains(vs1)
    VS __contains__ 0 1
    False
    >>> vs0_len = vs0.__len__
    >>> vs0_len()
    VS __len__ 0
    0
    >>> vs0_repr = vs0.__repr__
    >>> vs0_repr()
    VS __repr__ 0
    >>> vs0_hash = vs0.__hash__
    >>> vs0_hash()
    VS __hash__ 0
    1000
    >>> vs0_call = vs0.__call__
    >>> vs0_call(vs1)
    VS __call__ 0(1)
    >>> vs0_str = vs0.__str__
    >>> vs0_str()
    VS __str__ 0

    # If you define __richcmp__, you get all of __lt__, __le__,
    # __eq__, __ne__, __gt__, __ge__ (this behavior is unchanged by #561).
    # (you don't get a __richcmp__ method, because it doesn't have a
    # Python signature)
    >>> vs0_lt = vs0.__lt__
    >>> vs0_lt(vs1)
    VS richcmp 0 1 (kind=0)
    >>> vs0_le = vs0.__le__
    >>> vs0_le(vs1)
    VS richcmp 0 1 (kind=1)
    >>> vs0_eq = vs0.__eq__
    >>> vs0_eq(vs1)
    VS richcmp 0 1 (kind=2)
    >>> vs0_ne = vs0.__ne__
    >>> vs0_ne(vs1)
    VS richcmp 0 1 (kind=3)
    >>> vs0_gt = vs0.__gt__
    >>> vs0_gt(vs1)
    VS richcmp 0 1 (kind=4)
    >>> vs0_ge = vs0.__ge__
    >>> vs0_ge(vs1)
    VS richcmp 0 1 (kind=5)
    >>> vs0_iter = vs0.__iter__
    >>> vs0_iter()
    VS __iter__ 0
    >>> vs0_next = vs0.__next__
    >>> vs0_next()
    VS next/__next__ 0

    >>> vs0_get = vs0.__get__
    >>> vs0_get('instance', 'owner')
    VS __get__ 0 'instance' 'owner'
    >>> vs0_init = vs0.__init__
    >>> vs0_init(0)
    VS __init__ 0
    """
    cdef readonly int value

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

cdef class Long:
    def __long__(self):
        print "Long __long__"

cdef class GetAttrGetItemRedirect:
    """
    >>> o = GetAttrGetItemRedirect()

    >>> assert o.item == o['item']
    >>> source, item_value = o.item
    >>> assert source == 'item', source

    >>> assert o['attr'] == o.attr
    >>> source, attr_value = o['attr']
    >>> assert source == 'attr', source

    >>> assert item_value is attr_value, repr((item_value, attr_value))
    """
    cdef object obj
    def __cinit__(self):
        self.obj = object()

    def __getattr__(self, name):
        if name == 'item':
            return self[name]
        return ('attr', self.obj)

    def __getitem__(self, key):
        if key == 'attr':
            return getattr(self, key)
        return ('item', self.obj)


# test unbound method usage in subtypes

cdef class VerySpecialSubType(VerySpecial):
    """
    >>> vs0 = VerySpecialSubType(0)
    VS __init__ 0
    >>> vs1 = VerySpecialSubType(1)
    VS __init__ 1

    >>> vs0_add = vs0.__add__
    >>> vs0_add(vs1)
    VS __add__ 0 1
    >>> vs0_sub = vs0.__sub__
    >>> vs0_sub(vs1)
    VS __sub__ 0 1
    >>> vs0_mul = vs0.__mul__
    >>> vs0_mul(vs1)
    VS __mul__ 0 1
    >>> vs0_mod = vs0.__mod__
    >>> vs0_mod(vs1)
    VS __mod__ 0 1
    >>> vs0_divmod = vs0.__divmod__
    >>> vs0_divmod(vs1)
    VS __divmod__ 0 1
    >>> vs0_pow = vs0.__pow__
    >>> vs0_pow(vs1)
    VS __pow__ pow(0, 1, None)
    >>> vs0_pow(vs1, 13)
    VS __pow__ pow(0, 1, 13)
    >>> vs0_neg = vs0.__neg__
    >>> vs0_neg()
    VS __neg__ 0
    >>> vs0_pos = vs0.__pos__
    >>> vs0_pos()
    VS __pos__ 0
    >>> vs0_abs = vs0.__abs__
    >>> vs0_abs()
    VS __abs__ 0
    >>> vs0_invert = vs0.__invert__
    >>> vs0_invert()
    VS __invert__ 0
    >>> vs0_lshift = vs0.__lshift__
    >>> vs0_lshift(vs1)
    VS __lshift__ 0 << 1
    >>> vs0_rshift = vs0.__rshift__
    >>> vs0_rshift(vs1)
    VS __rshift__ 0 >> 1
    >>> vs0_and = vs0.__and__
    >>> vs0_and(vs1)
    VS __and__ 0 & 1
    >>> vs0_xor = vs0.__xor__
    >>> vs0_xor(vs1)
    VS __xor__ 0 ^ 1
    >>> vs0_or = vs0.__or__
    >>> vs0_or(vs1)
    VS __or__ 0 | 1
    >>> vs0_int = vs0.__int__
    >>> vs0_int()
    VS __int__ 0
    >>> vs0_float = vs0.__float__
    >>> vs0_float()
    VS __float__ 0
    >>> vs0_iadd = vs0.__iadd__
    >>> vs0_iadd(vs1)
    VS __iadd__ 0 += 1
    >>> vs0_isub = vs0.__isub__
    >>> vs0_isub(vs1)
    VS __isub__ 0 -= 1
    >>> vs0_imul = vs0.__imul__
    >>> vs0_imul(vs1)
    VS __imul__ 0 *= 1
    >>> vs0_imod = vs0.__imod__
    >>> vs0_imod(vs1)
    VS __imod__ 0 %= 1
    >>> vs0_ipow = vs0.__ipow__
    >>> vs0_ipow(vs1)
    VS __ipow__ 0 1
    >>> vs0_ilshift = vs0.__ilshift__
    >>> vs0_ilshift(vs1)
    VS __ilshift__ 0 <<= 1
    >>> vs0_irshift = vs0.__irshift__
    >>> vs0_irshift(vs1)
    VS __irshift__ 0 >>= 1
    >>> vs0_iand = vs0.__iand__
    >>> vs0_iand(vs1)
    VS __iand__ 0 &= 1
    >>> vs0_ixor = vs0.__ixor__
    >>> vs0_ixor(vs1)
    VS __ixor__ 0 ^= 1
    >>> vs0_ior = vs0.__ior__
    >>> vs0_ior(vs1)
    VS __ior__ 0 |= 1
    >>> vs0_floordiv = vs0.__floordiv__
    >>> vs0_floordiv(vs1)
    VS __floordiv__ 0 / 1
    >>> vs0_truediv = vs0.__truediv__
    >>> vs0_truediv(vs1)
    VS __truediv__ 0 / 1
    >>> vs0_ifloordiv = vs0.__ifloordiv__
    >>> vs0_ifloordiv(vs1)
    VS __ifloordiv__ 0 /= 1
    >>> vs0_itruediv = vs0.__itruediv__
    >>> vs0_itruediv(vs1)
    VS __itruediv__ 0 /= 1

    # If you define an arithmetic method, you get wrapper objects for
    # the reversed version as well.  (This behavior is unchanged by #561.)
    >>> vs0_radd = vs0.__radd__
    >>> vs0_radd(vs1)
    VS __add__ 1 0
    >>> vs0_rsub = vs0.__rsub__
    >>> vs0_rsub(vs1)
    VS __sub__ 1 0
    >>> vs0_rmul = vs0.__rmul__
    >>> vs0_rmul(vs1)
    VS __mul__ 1 0
    >>> vs0_rmod = vs0.__rmod__
    >>> vs0_rmod(vs1)
    VS __mod__ 1 0
    >>> vs0_rdivmod = vs0.__rdivmod__
    >>> vs0_rdivmod(vs1)
    VS __divmod__ 1 0
    >>> vs0_rpow = vs0.__rpow__
    >>> vs0_rpow(vs1)
    VS __pow__ pow(1, 0, None)
    >>> vs0_rlshift = vs0.__rlshift__
    >>> vs0_rlshift(vs1)
    VS __lshift__ 1 << 0
    >>> vs0_rrshift = vs0.__rrshift__
    >>> vs0_rrshift(vs1)
    VS __rshift__ 1 >> 0
    >>> vs0_rand = vs0.__rand__
    >>> vs0_rand(vs1)
    VS __and__ 1 & 0
    >>> vs0_rxor = vs0.__rxor__
    >>> vs0_rxor(vs1)
    VS __xor__ 1 ^ 0
    >>> vs0_ror = vs0.__ror__
    >>> vs0_ror(vs1)
    VS __or__ 1 | 0
    >>> vs0_rfloordiv = vs0.__rfloordiv__
    >>> vs0_rfloordiv(vs1)
    VS __floordiv__ 1 / 0
    >>> vs0_rtruediv = vs0.__rtruediv__
    >>> vs0_rtruediv(vs1)
    VS __truediv__ 1 / 0
    >>> vs0_getitem = vs0.__getitem__
    >>> vs0_getitem('foo')
    VS __getitem__ 0['foo']
    >>> vs0_contains = vs0.__contains__
    >>> vs0_contains(vs1)
    VS __contains__ 0 1
    False
    >>> vs0_len = vs0.__len__
    >>> vs0_len()
    VS __len__ 0
    0
    >>> vs0_repr = vs0.__repr__
    >>> vs0_repr()
    VS __repr__ 0
    >>> vs0_hash = vs0.__hash__
    >>> vs0_hash()
    VS __hash__ 0
    1000
    >>> vs0_call = vs0.__call__
    >>> vs0_call(vs1)
    VS __call__ 0(1)
    >>> vs0_str = vs0.__str__
    >>> vs0_str()
    VS __str__ 0
    >>> vs0_lt = vs0.__lt__
    >>> vs0_lt(vs1)
    VS richcmp 0 1 (kind=0)
    >>> vs0_le = vs0.__le__
    >>> vs0_le(vs1)
    VS richcmp 0 1 (kind=1)
    >>> vs0_eq = vs0.__eq__
    >>> vs0_eq(vs1)
    VS richcmp 0 1 (kind=2)
    >>> vs0_ne = vs0.__ne__
    >>> vs0_ne(vs1)
    VS richcmp 0 1 (kind=3)
    >>> vs0_gt = vs0.__gt__
    >>> vs0_gt(vs1)
    VS richcmp 0 1 (kind=4)
    >>> vs0_ge = vs0.__ge__
    >>> vs0_ge(vs1)
    VS richcmp 0 1 (kind=5)
    >>> vs0_iter = vs0.__iter__
    >>> vs0_iter()
    VS __iter__ 0
    >>> vs0_next = vs0.__next__
    >>> vs0_next()
    VS next/__next__ 0
    >>> vs0_get = vs0.__get__
    >>> vs0_get('instance', 'owner')
    VS __get__ 0 'instance' 'owner'
    >>> vs0_init = vs0.__init__
    >>> vs0_init(0)
    VS __init__ 0
    """
    def __init__(self, v):
        VerySpecial.__init__(self, v)

    def __add__(self, other):
        return VerySpecial.__add__(self, other)

    def __sub__(self, other):
        return VerySpecial.__sub__(self, other)

    def __mul__(self, other):
        return VerySpecial.__mul__(self, other)

    def __div__(self, other):
        return VerySpecial.__div__(self, other)

    def __mod__(self, other):
        return VerySpecial.__mod__(self, other)

    def __divmod__(self, other):
        return VerySpecial.__divmod__(self, other)

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

    def __lshift__(self, other):
        return VerySpecial.__lshift__(self, other)

    def __rshift__(self, other):
        return VerySpecial.__rshift__(self, other)

    def __and__(self, other):
        return VerySpecial.__and__(self, other)

    def __xor__(self, other):
        return VerySpecial.__xor__(self, other)

    def __or__(self, other):
        return VerySpecial.__or__(self, other)

    def __floordiv__(self, other):
        return VerySpecial.__floordiv__(self, other)

    def __truediv__(self, other):
        return VerySpecial.__truediv__(self, other)

    def __neg__(self):
        return VerySpecial.__neg__(self)

    def __pos__(self):
        return VerySpecial.__pos__(self)

    def __abs__(self):
        return VerySpecial.__abs__(self)

    def __nonzero__(self):
        return VerySpecial.__nonzero__(self)

    def __invert__(self):
        return VerySpecial.__invert__(self)

    def __int__(self):
        return VerySpecial.__int__(self)

    def __long__(self):
        return VerySpecial.__long__(self)

    def __float__(self):
        return VerySpecial.__float__(self)

    def __oct__(self):
        return VerySpecial.__oct__(self)

    def __hex__(self):
        return VerySpecial.__hex__(self)

    def __iadd__(self, other):
        return VerySpecial.__iadd__(self, other)

    def __isub__(self, other):
        return VerySpecial.__isub__(self, other)

    def __imul__(self, other):
        return VerySpecial.__imul__(self, other)

    def __idiv__(self, other):
        return VerySpecial.__idiv__(self, other)

    def __imod__(self, other):
        return VerySpecial.__imod__(self, other)

    def __ipow__(self, other):
        return VerySpecial.__ipow__(self, other)

    def __ilshift__(self, other):
        return VerySpecial.__ilshift__(self, other)

    def __irshift__(self, other):
        return VerySpecial.__irshift__(self, other)

    def __iand__(self, other):
        return VerySpecial.__iand__(self, other)

    def __ixor__(self, other):
        return VerySpecial.__ixor__(self, other)

    def __ior__(self, other):
        return VerySpecial.__ior__(self, other)

    def __ifloordiv__(self, other):
        return VerySpecial.__ifloordiv__(self, other)

    def __itruediv__(self, other):
        return VerySpecial.__itruediv__(self, other)

    def __index__(self):
        return VerySpecial.__index__(self)

    def __getitem__(self, index):
        return VerySpecial.__getitem__(self, index)

    def __contains__(self, other):
        return VerySpecial.__contains__(self, other)

    def __len__(self):
        return VerySpecial.__len__(self)

    def __cmp__(self, other):
        return VerySpecial.__cmp__(self, other)

    def __repr__(self):
        return VerySpecial.__repr__(self)

    def __hash__(self):
        return VerySpecial.__hash__(self)

    def __call__(self, arg):
        return VerySpecial.__call__(self, arg)

    def __str__(self):
        return VerySpecial.__str__(self)

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

    def __iter__(self):
        return VerySpecial.__iter__(self)

    def __next__(self):
        return VerySpecial.__next__(self)

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


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

    >>> o = ReverseMethodsExist()
    >>> o + o
    'add'
    >>> o.__add__(o)
    'add'
    >>> o.__radd__(o)
    'radd'
    >>> o.__rsub__(o)
    'rsub'
    """
    def __add__(self, other):
        return "add"
    def __radd__(self, other):
        return "radd"
    def __rsub__(self, other):
        return "rsub"