1
+ # Copyright (c) 2018, Oracle and/or its affiliates.
2
+ #
3
+ # The Universal Permissive License (UPL), Version 1.0
4
+ #
5
+ # Subject to the condition set forth below, permission is hereby granted to any
6
+ # person obtaining a copy of this software, associated documentation and/or data
7
+ # (collectively the "Software"), free of charge and under any and all copyright
8
+ # rights in the Software, and any and all patent rights owned or freely
9
+ # licensable by each licensor hereunder covering either (i) the unmodified
10
+ # Software as contributed to or provided by such licensor, or (ii) the Larger
11
+ # Works (as defined below), to deal in both
12
+ #
13
+ # (a) the Software, and
14
+ # (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
15
+ # one is included with the Software (each a "Larger Work" to which the
16
+ # Software is contributed by such licensors),
17
+ #
18
+ # without restriction, including without limitation the rights to copy, create
19
+ # derivative works of, display, perform, and distribute the Software and make,
20
+ # use, sell, offer for sale, import, export, have made, and have sold the
21
+ # Software and the Larger Work(s), and to sublicense the foregoing rights on
22
+ # either these or other terms.
23
+ #
24
+ # This license is subject to the following condition:
25
+ #
26
+ # The above copyright notice and either this complete permission notice or at a
27
+ # minimum a reference to the UPL must be included in all copies or substantial
28
+ # portions of the Software.
29
+ #
30
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36
+ # SOFTWARE.
37
+
38
+ import sys
39
+ from . import CPyExtTestCase , CPyExtFunction , CPyExtFunctionOutVars , unhandled_error_compare , GRAALPYTHON
40
+ __dir__ = __file__ .rpartition ("/" )[0 ]
41
+
42
+
43
+ class FrozenSetSubclass (frozenset ):
44
+ pass
45
+
46
+
47
+ class SetSubclass (set ):
48
+ pass
49
+
50
+
51
+ default_typecheck_args = lambda : (
52
+ (set (),),
53
+ (set ([1 ,2 ,3 ]),),
54
+ (set ({'a' , 'b' }),),
55
+ (set ([None ]),),
56
+ (frozenset (),),
57
+ (frozenset ([1 ,2 ,3 ]),),
58
+ (frozenset ({'a' , 'b' }),),
59
+ (frozenset ([None ]),),
60
+ (FrozenSetSubclass (),),
61
+ (SetSubclass ([None ]),),
62
+ ({'a' : "hello" , 'b' : "world" },),
63
+ ('a' ,),
64
+ (1 ,),
65
+ (None ,),
66
+ )
67
+
68
+ class TestPySet (CPyExtTestCase ):
69
+ def compile_module (self , name ):
70
+ type (self ).mro ()[1 ].__dict__ ["test_%s" % name ].create_module (name )
71
+ super (TestPySet , self ).compile_module (name )
72
+
73
+ test_PySet_New = CPyExtFunction (
74
+ lambda args : set (args [0 ]),
75
+ lambda : (
76
+ ([1 , 2 , 3 ],),
77
+ ({'a' : "hello" , 'b' : "world" },),
78
+ ({'a' , 'b' },),
79
+ ('a' ,),
80
+ (1 ,),
81
+ ),
82
+ resultspec = "O" ,
83
+ argspec = 'O' ,
84
+ arguments = ["PyObject* iterable" ],
85
+ cmpfunc = unhandled_error_compare
86
+ )
87
+
88
+ test_PyFrozenSet_New = CPyExtFunction (
89
+ lambda args : frozenset (args [0 ]),
90
+ lambda : (
91
+ ([1 , 2 , 3 ],),
92
+ ({'a' : "hello" , 'b' : "world" },),
93
+ ({'a' , 'b' },),
94
+ ('a' ,),
95
+ (1 ,),
96
+ ),
97
+ resultspec = "O" ,
98
+ argspec = 'O' ,
99
+ arguments = ["PyObject* iterable" ],
100
+ cmpfunc = unhandled_error_compare
101
+ )
102
+
103
+ test_PyFrozenSet_Check = CPyExtFunction (
104
+ lambda args : isinstance (args [0 ], frozenset ),
105
+ default_typecheck_args ,
106
+ resultspec = "i" ,
107
+ argspec = 'O' ,
108
+ arguments = ["PyObject* o" ],
109
+ cmpfunc = unhandled_error_compare
110
+ )
111
+
112
+ test_PyFrozenSet_CheckExact = CPyExtFunction (
113
+ lambda args : type (args [0 ]) is frozenset ,
114
+ default_typecheck_args ,
115
+ resultspec = "i" ,
116
+ argspec = 'O' ,
117
+ arguments = ["PyObject* o" ],
118
+ cmpfunc = unhandled_error_compare
119
+ )
120
+
121
+ test_PyAnySet_CheckExact = CPyExtFunction (
122
+ lambda args : type (args [0 ]) is set or type (args [0 ]) is frozenset ,
123
+ default_typecheck_args ,
124
+ resultspec = "i" ,
125
+ argspec = 'O' ,
126
+ arguments = ["PyObject* o" ],
127
+ cmpfunc = unhandled_error_compare
128
+ )
129
+
130
+ test_PyAnySet_Check = CPyExtFunction (
131
+ lambda args : isinstance (args [0 ], set ) or isinstance (args [0 ], frozenset ),
132
+ default_typecheck_args ,
133
+ resultspec = "i" ,
134
+ argspec = 'O' ,
135
+ arguments = ["PyObject* o" ],
136
+ cmpfunc = unhandled_error_compare
137
+ )
138
+
139
+ test_PySet_Check = CPyExtFunction (
140
+ lambda args : isinstance (args [0 ], set ),
141
+ default_typecheck_args ,
142
+ resultspec = "i" ,
143
+ argspec = 'O' ,
144
+ arguments = ["PyObject* o" ],
145
+ cmpfunc = unhandled_error_compare
146
+ )
147
+
0 commit comments