-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathclasses.py
More file actions
1786 lines (1139 loc) · 30.7 KB
/
classes.py
File metadata and controls
1786 lines (1139 loc) · 30.7 KB
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
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# User-defined methods, both instance methods and class methods, can be called in many non-standard ways
# i.e. differently from simply `c.f()` or `C.f()`. For example, a user-defined `__await__` method on a
# class `C` will be called by the syntactic construct `await c` when `c` is an instance of `C`.
#
# These tests should cover all the class calls that we hope to support.
# It is based on https://docs.python.org/3/reference/datamodel.html, and headings refer there.
#
# All functions starting with "test_" should run and execute `print("OK")` one or more times.
# This can be checked by running validTest.py.
import asyncio
def SINK1(x):
pass
def SINK2(x):
pass
def SINK3(x):
pass
def SINK4(x):
pass
def OK():
print("OK")
# object.__new__(cls[, ...])
class With_new:
def __new__(cls):
SINK1(cls) # Flow not found
OK() # Call not found
return super().__new__(cls)
def test_new():
with_new = With_new()
# object.__init__(self[, ...])
class With_init:
def __init__(self):
SINK1(self)
OK()
def test_init():
with_init = With_init()
# object.__del__(self)
class With_del:
def __del__(self):
SINK1(self) # Flow not found
OK() # Call not found
def test_del():
with_del = With_del()
del with_del
# object.__repr__(self)
class With_repr:
def __repr__(self):
SINK1(self) # Flow not found
OK() # Call not found
return "With_repr()"
def test_repr():
with_repr = With_repr()
repr(with_repr)
# object.__str__(self)
class With_str:
def __str__(self):
SINK1(self) # Flow not found
OK() # Call not found
return "Awesome"
def test_str():
with_str = With_str()
str(with_str)
# object.__bytes__(self)
class With_bytes:
def __bytes__(self):
SINK1(self) # Flow not found
OK() # Call not found
return b"Awesome"
def test_bytes():
with_bytes = With_bytes()
bytes(with_bytes)
# object.__format__(self, format_spec)
class With_format:
def __format__(self, format_spec):
SINK2(format_spec) # Flow not found
SINK1(self) # Flow not found
OK() # Call not found
return "Awesome"
def test_format():
with_format = With_format()
arg2 = ""
format(with_format, arg2)
def test_format_str():
with_format = With_format()
"{0}".format(with_format)
def test_format_fstr():
with_format = With_format()
f"{with_format}"
# object.__lt__(self, other)
class With_lt:
def __lt__(self, other):
SINK2(other) # Flow not found
SINK1(self) # Flow not found
OK() # Call not found
return ""
def test_lt():
with_lt = With_lt()
arg2 = with_lt
with_lt < arg2
# object.__le__(self, other)
class With_le:
def __le__(self, other):
SINK2(other) # Flow not found
SINK1(self) # Flow not found
OK() # Call not found
return ""
def test_le():
with_le = With_le()
arg2 = with_le
with_le <= arg2
# object.__eq__(self, other)
class With_eq:
def __eq__(self, other):
SINK2(other) # Flow not found
SINK1(self) # Flow not found
OK() # Call not found
return ""
def test_eq():
with_eq = With_eq()
with_eq == with_eq
# object.__ne__(self, other)
class With_ne:
def __ne__(self, other):
SINK2(other) # Flow not found
SINK1(self) # Flow not found
OK() # Call not found
return ""
def test_ne():
with_ne = With_ne()
with_ne != with_ne
# object.__gt__(self, other)
class With_gt:
def __gt__(self, other):
SINK2(other) # Flow not found
SINK1(self) # Flow not found
OK() # Call not found
return ""
def test_gt():
with_gt = With_gt()
arg2 = with_gt
with_gt > arg2
# object.__ge__(self, other)
class With_ge:
def __ge__(self, other):
SINK2(other) # Flow not found
SINK1(self) # Flow not found
OK() # Call not found
return ""
def test_ge():
with_ge = With_ge()
arg2 = with_ge
with_ge >= arg2
# object.__hash__(self)
class With_hash:
def __hash__(self):
SINK1(self) # Flow not found
OK() # Call not found
return 0
def test_hash():
with_hash = With_hash()
hash(with_hash)
def test_hash_set():
with_hash = With_hash()
len(set([with_hash]))
def test_hash_frozenset():
with_hash = With_hash()
len(frozenset([with_hash]))
def test_hash_dict():
with_hash = With_hash()
len(dict({with_hash: 0}))
# object.__bool__(self)
class With_bool:
def __bool__(self):
SINK1(self) # Flow not found
OK() # Call not found
return True
def test_bool():
with_bool = With_bool()
bool(with_bool)
def test_bool_if():
with_bool = With_bool()
if with_bool:
pass
# 3.3.2. Customizing attribute access
# object.__getattr__(self, name)
class With_getattr:
def __getattr__(self, name):
SINK2(name) # Flow not found
SINK1(self) # Flow not found
OK() # Call not found
return ""
def test_getattr():
with_getattr = With_getattr()
with_getattr.arg2
# object.__getattribute__(self, name)
class With_getattribute:
def __getattribute__(self, name):
SINK2(name) # Flow not found
SINK1(self) # Flow not found
OK() # Call not found
return ""
def test_getattribute():
with_getattribute = With_getattribute()
with_getattribute.arg2
# object.__setattr__(self, name, value)
class With_setattr:
def __setattr__(self, name, value):
SINK3(value) # Flow not found
SINK2(name) # Flow not found
SINK1(self) # Flow not found
OK() # Call not found
def test_setattr():
with_setattr = With_setattr()
arg3 = ""
with_setattr.arg2 = arg3
# object.__delattr__(self, name)
class With_delattr:
def __delattr__(self, name):
SINK2(name) # Flow not found
SINK1(self) # Flow not found
OK() # Call not found
def test_delattr():
with_delattr = With_delattr()
del with_delattr.arg2
# object.__dir__(self)
class With_dir:
def __dir__(self):
SINK1(self) # Flow not found
OK() # Call not found
return []
def test_dir():
with_dir = With_dir()
dir(with_dir)
# 3.3.2.2. Implementing Descriptors
class Owner:
pass
# object.__get__(self, instance, owner=None)
class With_get:
def __get__(self, instance, owner=None):
SINK3(owner) # Flow not testsed, use class `Owner` as source to test
SINK2(instance) # Flow not found
SINK1(self) # Flow not found
OK() # Call not found
return ""
def test_get():
class arg3:
pass
with_get = With_get()
arg3.attr = with_get
arg2 = arg3()
arg2.attr
# object.__set__(self, instance, value)
class With_set:
def __set__(self, instance, value):
SINK3(value) # Flow not found
SINK2(instance) # Flow not found
SINK1(self) # Flow not found
OK() # Call not found
def test_set():
with_set = With_set()
Owner.attr = with_set
arg2 = Owner()
arg3 = ""
arg2.attr = arg3
# object.__delete__(self, instance)
class With_delete:
def __delete__(self, instance):
SINK2(instance) # Flow not found
SINK1(self) # Flow not found
OK() # Call not found
def test_delete():
with_delete = With_delete()
Owner.attr = with_delete
arg2 = Owner()
del arg2.attr
# object.__set_name__(self, owner, name)
class With_set_name:
def __set_name__(self, owner, name):
SINK3(name) # Flow not found
SINK2(owner) # Flow not found
SINK1(self) # Flow not found
OK() # Call not found
def test_set_name():
with_set_name = With_set_name()
type("arg2", (object,), dict(arg3=with_set_name))
# 3.3.2.4. __slots__ // We are not testing the suppression of -weakref_ and _dict_ here
# object.__slots__
# __weakref__
# __dict__
# 3.3.3. Customizing class creation
# classmethod object.__init_subclass__(cls)
class With_init_subclass:
def __init_subclass__(cls):
SINK1(cls) # Flow not found
OK() # Call not found
def test_init_subclass():
type("Subclass", (With_init_subclass,), {})
# 3.3.3.1. Metaclasses
# By default, classes are constructed using type(). The class body is executed in a new namespace and the class name is bound locally to the result of type(name, bases, namespace).
# 3.3.3.2. Resolving MRO entries
# __mro_entries__
# 3.3.3.4. Preparing the class namespace
# metaclass.__prepare__(name, bases, **kwds)
class With_prepare(type):
def __prepare__(name, bases, **kwds):
SINK3(kwds) # Flow not tested
SINK2(bases) # Flow not tested
SINK1(name) # Flow not found
OK() # Call not found
return kwds
def test_prepare():
class arg1(metaclass=With_prepare):
pass
# 3.3.4. Customizing instance and subclass checks
# class.__instancecheck__(self, instance)
class With_instancecheck:
def __instancecheck__(self, instance):
SINK2(instance) # Flow not found
SINK1(self) # Flow not found
OK() # Call not found
return True
def test_instancecheck():
with_instancecheck = With_instancecheck()
arg2 = ""
isinstance(arg2, with_instancecheck)
# class.__subclasscheck__(self, subclass)
class With_subclasscheck:
def __subclasscheck__(self, subclass):
SINK2(subclass) # Flow not found
SINK1(self) # Flow not found
OK() # Call not found
return True
def test_subclasscheck():
with_subclasscheck = With_subclasscheck()
arg2 = object
issubclass(arg2, with_subclasscheck)
# 3.3.5. Emulating generic types
# classmethod object.__class_getitem__(cls, key)
class With_class_getitem:
def __class_getitem__(cls, key):
SINK2(key) # Flow not found
SINK1(cls) # Flow not found
OK() # Call not found
return object
def test_class_getitem():
arg2 = int
with_class_getitem = With_class_getitem[arg2]()
# 3.3.6. Emulating callable objects
# object.__call__(self[, args...])
class With_call:
def __call__(self):
SINK1(self) # Flow not found
OK() # Call not found
def test_call():
with_call = With_call()
with_call()
# 3.3.7. Emulating container types
# object.__len__(self)
class With_len:
def __len__(self):
SINK1(self) # Flow not found
OK() # Call not found
return 0
def test_len():
with_len = With_len()
len(with_len)
def test_len_bool():
with_len = With_len()
bool(with_len)
def test_len_if():
with_len = With_len()
if with_len:
pass
# object.__length_hint__(self)
class With_length_hint:
def __length_hint__(self):
SINK1(self) # Flow not found
OK() # Call not found
return 0
def test_length_hint():
import operator
with_length_hint = With_length_hint()
operator.length_hint(with_length_hint)
# object.__getitem__(self, key)
class With_getitem:
def __getitem__(self, key):
SINK2(key)
SINK1(self)
OK()
return ""
def test_getitem():
with_getitem = With_getitem()
arg2 = 0
with_getitem[arg2]
# object.__setitem__(self, key, value)
class With_setitem:
def __setitem__(self, key, value):
SINK3(value)
SINK2(key)
SINK1(self)
OK()
def test_setitem():
with_setitem = With_setitem()
arg2 = 0
arg3 = ""
with_setitem[arg2] = arg3
# object.__delitem__(self, key)
class With_delitem:
def __delitem__(self, key):
SINK2(key)
SINK1(self)
OK()
def test_delitem():
with_delitem = With_delitem()
arg2 = 0
del with_delitem[arg2]
# object.__missing__(self, key)
class With_missing(dict):
def __missing__(self, key):
SINK2(key) # Flow not found
SINK1(self) # Flow not found
OK() # Call not found
return ""
def test_missing():
with_missing = With_missing()
arg2 = 0
with_missing[arg2]
# object.__iter__(self)
class With_iter:
def __iter__(self):
SINK1(self) # Flow not found
OK() # Call not found
return [].__iter__()
def test_iter():
with_iter = With_iter()
[x for x in with_iter]
# object.__reversed__(self)
class With_reversed:
def __reversed__(self):
SINK1(self) # Flow not found
OK() # Call not found
return [].__iter__
def test_reversed():
with_reversed = With_reversed()
reversed(with_reversed)
# object.__contains__(self, item)
class With_contains:
def __contains__(self, item):
SINK2(item) # Flow not found
SINK1(self) # Flow not found
OK() # Call not found
return True
def test_contains():
with_contains = With_contains()
arg2 = 0
arg2 in with_contains
# 3.3.8. Emulating numeric types
# object.__add__(self, other)
class With_add:
def __add__(self, other):
SINK2(other)
SINK1(self)
OK()
return self
def test_add():
with_add = With_add()
arg2 = with_add
with_add + arg2
# object.__sub__(self, other)
class With_sub:
def __sub__(self, other):
SINK2(other)
SINK1(self)
OK()
return self
def test_sub():
with_sub = With_sub()
arg2 = with_sub
with_sub - arg2
# object.__mul__(self, other)
class With_mul:
def __mul__(self, other):
SINK2(other)
SINK1(self)
OK()
return self
def test_mul():
with_mul = With_mul()
arg2 = with_mul
with_mul * arg2
# object.__matmul__(self, other)
class With_matmul:
def __matmul__(self, other):
SINK2(other)
SINK1(self)
OK()
return self
def test_matmul():
with_matmul = With_matmul()
arg2 = with_matmul
with_matmul @ arg2
# object.__truediv__(self, other)
class With_truediv:
def __truediv__(self, other):
SINK2(other)
SINK1(self)
OK()
return self
def test_truediv():
with_truediv = With_truediv()
arg2 = with_truediv
with_truediv / arg2
# object.__floordiv__(self, other)
class With_floordiv:
def __floordiv__(self, other):
SINK2(other)
SINK1(self)
OK()
return self
def test_floordiv():
with_floordiv = With_floordiv()
arg2 = with_floordiv
with_floordiv // arg2
# object.__mod__(self, other)
class With_mod:
def __mod__(self, other):
SINK2(other)
SINK1(self)
OK()
return self
def test_mod():
with_mod = With_mod()
arg2 = with_mod
with_mod % arg2
# object.__divmod__(self, other)
class With_divmod:
def __divmod__(self, other):
SINK2(other) # Flow not found
SINK1(self) # Flow not found
OK() # Call not found
return self
def test_divmod():
with_divmod = With_divmod()
arg2 = With_divmod
divmod(with_divmod, arg2)
# object.__pow__(self, other[, modulo])
class With_pow:
def __pow__(self, other):
SINK2(other)
SINK1(self)
OK()
return self
def test_pow():
with_pow = With_pow()
arg2 = with_pow
pow(with_pow, arg2) # Call not found
def test_pow_op():
with_pow = With_pow()
arg2 = with_pow
with_pow ** arg2
# object.__lshift__(self, other)
class With_lshift:
def __lshift__(self, other):
SINK2(other)
SINK1(self)
OK()
return self
def test_lshift():
with_lshift = With_lshift()
arg2 = with_lshift
with_lshift << arg2
# object.__rshift__(self, other)
class With_rshift:
def __rshift__(self, other):
SINK2(other)
SINK1(self)
OK()
return self
def test_rshift():
with_rshift = With_rshift()
arg2 = with_rshift
with_rshift >> arg2
# object.__and__(self, other)
class With_and:
def __and__(self, other):
SINK2(other)
SINK1(self)
OK()
return self
def test_and():
with_and = With_and()
arg2 = with_and
with_and & arg2
# object.__xor__(self, other)
class With_xor:
def __xor__(self, other):
SINK2(other)
SINK1(self)
OK()
return self
def test_xor():
with_xor = With_xor()
arg2 = with_xor
with_xor ^ arg2
# object.__or__(self, other)
class With_or:
def __or__(self, other):
SINK2(other)
SINK1(self)
OK()
return self
def test_or():
with_or = With_or()
arg2 = with_or
with_or | arg2
# object.__radd__(self, other)
class With_radd:
def __radd__(self, other):
SINK2(other) # Flow not found
SINK1(self) # Flow not found
OK() # Call not found
return self
def test_radd():
with_radd = With_radd()
arg2 = ""
arg2 + with_radd
# object.__rsub__(self, other)
class With_rsub:
def __rsub__(self, other):
SINK2(other) # Flow not found
SINK1(self) # Flow not found
OK() # Call not found
return self
def test_rsub():
with_rsub = With_rsub()
arg2 = ""
arg2 - with_rsub
# object.__rmul__(self, other)