@@ -86,6 +86,40 @@ def _reference_next(args):
86
86
raise SystemError
87
87
88
88
89
+ def _reference_size (args ):
90
+ seq = args [0 ]
91
+ if isinstance (seq , dict ):
92
+ return - 1
93
+ if not hasattr (seq , '__len__' ):
94
+ raise TypeError ()
95
+ return len (seq )
96
+
97
+
98
+ def _reference_getitem (args ):
99
+ seq = args [0 ]
100
+ idx = args [1 ]
101
+ if not hasattr (seq , '__getitem__' ):
102
+ raise TypeError
103
+ return seq .__getitem__ (idx )
104
+
105
+
106
+ def _reference_setitem (args ):
107
+ seq = args [0 ]
108
+ idx = args [1 ]
109
+ value = args [2 ]
110
+ if not hasattr (seq , '__setitem__' ):
111
+ raise TypeError
112
+ seq .__setitem__ (idx , value )
113
+ return seq
114
+
115
+
116
+ def _reference_fast (args ):
117
+ obj = args [0 ]
118
+ if isinstance (obj , tuple ) or isinstance (obj , list ):
119
+ return obj
120
+ return list (obj )
121
+
122
+
89
123
class NoNumber ():
90
124
pass
91
125
@@ -110,6 +144,15 @@ def __float__(self):
110
144
return 2.71828
111
145
112
146
147
+ class DummySequence ():
148
+ def __getitem__ (self , idx ):
149
+ return idx * 10
150
+
151
+
152
+ class DummyListSubclass (list ):
153
+ pass
154
+
155
+
113
156
def _default_bin_arith_args ():
114
157
return (
115
158
(0 ,0 ),
@@ -137,10 +180,10 @@ def _default_bin_arith_args():
137
180
)
138
181
139
182
140
- class TestPyNumber (CPyExtTestCase ):
183
+ class TestAbstract (CPyExtTestCase ):
141
184
def compile_module (self , name ):
142
185
type (self ).mro ()[1 ].__dict__ ["test_%s" % name ].create_module (name )
143
- super (TestPyNumber , self ).compile_module (name )
186
+ super (TestAbstract , self ).compile_module (name )
144
187
145
188
146
189
test_PyNumber_Check = CPyExtFunction (
@@ -431,11 +474,6 @@ def compile_module(self, name):
431
474
cmpfunc = unhandled_error_compare
432
475
)
433
476
434
- class TestPySequence (CPyExtTestCase ):
435
- def compile_module (self , name ):
436
- type (self ).mro ()[1 ].__dict__ ["test_%s" % name ].create_module (name )
437
- super (TestPySequence , self ).compile_module (name )
438
-
439
477
test_PySequence_Fast_GET_SIZE = CPyExtFunction (
440
478
lambda args : len (args [0 ]),
441
479
lambda : (
@@ -531,3 +569,161 @@ def compile_module(self, name):
531
569
cmpfunc = unhandled_error_compare
532
570
)
533
571
572
+ test_PySequence_Check = CPyExtFunction (
573
+ lambda args : not isinstance (args [0 ], dict ) and hasattr (args [0 ], '__getitem__' ),
574
+ lambda : (
575
+ (tuple (),),
576
+ ((1 ,2 ,3 ),),
577
+ ((None ,),),
578
+ ([],),
579
+ (['a' ,'b' ,'c' ],),
580
+ ([None ],),
581
+ (dict (),),
582
+ (set (),),
583
+ ({'a' , 'b' },),
584
+ ({'a' :0 , 'b' :1 },),
585
+ (DummySequence (),),
586
+ (DummyListSubclass (),),
587
+ ),
588
+ resultspec = "i" ,
589
+ argspec = 'O' ,
590
+ arguments = ["PyObject* sequence" ],
591
+ )
592
+
593
+ test_PySequence_Size = CPyExtFunction (
594
+ _reference_size ,
595
+ lambda : (
596
+ (tuple (),),
597
+ ((1 ,2 ,3 ),),
598
+ ((None ,),),
599
+ ([],),
600
+ (['a' ,'b' ,'c' ],),
601
+ ([None ],),
602
+ (set (),),
603
+ (DummyListSubclass (),),
604
+ ),
605
+ resultspec = "n" ,
606
+ argspec = 'O' ,
607
+ arguments = ["PyObject* sequence" ],
608
+ cmpfunc = unhandled_error_compare
609
+ )
610
+
611
+ # 'PySequence_Length' is just a redefinition of 'PySequence_Size'
612
+ test_PySequence_Length = test_PySequence_Size
613
+
614
+ test_PySequence_GetItem = CPyExtFunction (
615
+ _reference_getitem ,
616
+ lambda : (
617
+ (tuple (), 10 ),
618
+ ((1 ,2 ,3 ), 2 ),
619
+ ((None ,), 0 ),
620
+ ([], 10 ),
621
+ (['a' ,'b' ,'c' ], 2 ),
622
+ ([None ], 0 ),
623
+ (set (), 0 ),
624
+ ({'a' , 'b' }, 0 ),
625
+ (DummyListSubclass (), 1 ),
626
+ ),
627
+ resultspec = "O" ,
628
+ argspec = 'On' ,
629
+ arguments = ["PyObject* sequence" , "Py_ssize_t idx" ],
630
+ cmpfunc = unhandled_error_compare
631
+ )
632
+
633
+ test_PySequence_SetItem = CPyExtFunction (
634
+ _reference_setitem ,
635
+ lambda : (
636
+ (tuple (), 0 , 'a' ),
637
+ ((1 ,2 ,3 ), 2 , 99 ),
638
+ ((None ,), 1 , None ),
639
+ ([], 10 , 1 ),
640
+ (['a' ,'b' ,'c' ], 2 , 'z' ),
641
+ ),
642
+ code = ''' PyObject* wrap_PySequence_SetItem(PyObject* sequence, Py_ssize_t idx, PyObject* value) {
643
+ if (PySequence_SetItem(sequence, idx, value) < 0) {
644
+ return NULL;
645
+ }
646
+ return sequence;
647
+ }
648
+ ''' ,
649
+ resultspec = "O" ,
650
+ argspec = 'OnO' ,
651
+ arguments = ["PyObject* sequence" , "Py_ssize_t idx" , "PyObject* value" ],
652
+ callfunction = "wrap_PySequence_SetItem" ,
653
+ cmpfunc = unhandled_error_compare
654
+ )
655
+
656
+ test_PySequence_Tuple = CPyExtFunction (
657
+ lambda args : tuple (args [0 ]),
658
+ lambda : (
659
+ (tuple (), ),
660
+ ((1 ,2 ,3 ), ),
661
+ ((None ,), ),
662
+ ([], ),
663
+ (['a' ,'b' ,'c' ],),
664
+ ({'a' ,'b' ,'c' },),
665
+ ({'a' : 0 ,'b' : 1 ,'c' : 2 },),
666
+ (None ,),
667
+ (0 ,),
668
+ ),
669
+ resultspec = "O" ,
670
+ argspec = 'O' ,
671
+ arguments = ["PyObject* sequence" ],
672
+ cmpfunc = unhandled_error_compare
673
+ )
674
+
675
+ test_PySequence_Fast = CPyExtFunction (
676
+ _reference_fast ,
677
+ lambda : (
678
+ (tuple (), "should not be an error" ),
679
+ ((1 ,2 ,3 ), "should not be an error" ),
680
+ ((None ,), "should not be an error" ),
681
+ ([], "should not be an error" ),
682
+ (['a' ,'b' ,'c' ],"should not be an error" ),
683
+ ({'a' ,'b' ,'c' }, "should not be an error" ),
684
+ ({'a' : 0 ,'b' : 1 ,'c' : 2 }, "should not be an error" ),
685
+ (None , "None cannot be a sequence" ),
686
+ (0 , "int cannot be a sequence" ),
687
+ ),
688
+ resultspec = "O" ,
689
+ argspec = 'Os' ,
690
+ arguments = ["PyObject* sequence" , "char* error_msg" ],
691
+ cmpfunc = unhandled_error_compare
692
+ )
693
+
694
+ test_PyMapping_GetItemString = CPyExtFunction (
695
+ lambda args : args [0 ][args [1 ]],
696
+ lambda : (
697
+ (tuple (), "hello" ),
698
+ ((1 ,2 ,3 ), "1" ),
699
+ (['a' ,'b' ,'c' ],"nothing" ),
700
+ ({'a' ,'b' ,'c' }, "a" ),
701
+ ({'a' : 0 ,'b' : 1 ,'c' : 2 }, "nothing" ),
702
+ ({'a' : 0 ,'b' : 1 ,'c' : 2 }, "c" ),
703
+ ),
704
+ resultspec = "O" ,
705
+ argspec = 'Os' ,
706
+ arguments = ["PyObject* mapping" , "char* keyStr" ],
707
+ cmpfunc = unhandled_error_compare
708
+ )
709
+
710
+ test_PyIndex_Check = CPyExtFunction (
711
+ lambda args : hasattr (args [0 ], "__index__" ),
712
+ lambda : (
713
+ (1 , ),
714
+ ("not a number" , ),
715
+ (tuple (), ),
716
+ (dict (), ),
717
+ (list (), ),
718
+ (DummyFloatable (), ),
719
+ (DummyFloatSubclass (), ),
720
+ (DummyIntable (), ),
721
+ (DummyIntSubclass (), ),
722
+ (NoNumber (), ),
723
+ ),
724
+ resultspec = "i" ,
725
+ argspec = 'O' ,
726
+ arguments = ["PyObject* obj" ],
727
+ cmpfunc = unhandled_error_compare
728
+ )
729
+
0 commit comments