Debian Codegraph @

pypy/module/cpyext/setobject.py

raw symbols SCIP
1 from pypy.interpreter.error import OperationError, oefmt
2 from rpython.rtyper.lltypesystem import rffi, lltype
3 from pypy.module.cpyext.api import (
4 cpython_api, Py_ssize_t, Py_ssize_tP, CANNOT_FAIL, build_type_checkers,
5 PyObjectFields, cpython_struct, bootstrap_function, slot_function)
6 from pypy.module.cpyext.pyobject import (PyObject, PyObjectP,
7 make_ref, from_ref, as_pyobj, create_ref, make_typedescr, incref, decref)
8 from pypy.module.cpyext.object import _dealloc
9 from pypy.module.cpyext.pyerrors import PyErr_BadInternalCall
10 from pypy.objspace.std.setobject import W_SetObject, W_FrozensetObject, newset
11
12 PySetObjectStruct = lltype.ForwardReference()
13 PySetObject = lltype.Ptr(PySetObjectStruct)
14 PySetObjectFields = PyObjectFields + \
15 (("_tmplist", PyObject),)
16 cpython_struct("PySetObject", PySetObjectFields, PySetObjectStruct)
17
18 @bootstrap_function
19 def init_setobject(space):
20 "Type description of PySetObject"
21 make_typedescr(space.w_set.layout.typedef,
22 basestruct=PySetObject.TO,
23 attach=set_attach,
24 dealloc=set_dealloc)
25 make_typedescr(space.w_frozenset.layout.typedef, # same as 'set'
26 basestruct=PySetObject.TO,
27 attach=set_attach,
28 dealloc=set_dealloc)
29
30 def set_attach(space, py_obj, w_obj, w_userdata=None):
31 """
32 Fills a newly allocated PySetObject with the given set object.
33 """
34 py_set = rffi.cast(PySetObject, py_obj)
35 py_set.c__tmplist = lltype.nullptr(PyObject.TO)
36
37 @slot_function([PyObject], lltype.Void)
38 def set_dealloc(space, py_obj):
39 py_set = rffi.cast(PySetObject, py_obj)
40 decref(space, py_set.c__tmplist)
41 py_set.c__tmplist = lltype.nullptr(PyObject.TO)
42 _dealloc(space, py_obj)
43
44 PySet_Check, PySet_CheckExact = build_type_checkers("Set")
45 PyFrozenSet_Check, PyFrozenSet_CheckExact = build_type_checkers("FrozenSet")
46
47 @cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL)
48 def PyAnySet_Check(space, w_obj):
49 """Return true if obj is a set object, a frozenset object, or an
50 instance of a subtype."""
51 return (space.isinstance_w(w_obj, space.gettypefor(W_SetObject)) or
52 space.isinstance_w(w_obj, space.gettypefor(W_FrozensetObject)))
53
54 @cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL)
55 def PyAnySet_CheckExact(space, w_obj):
56 """Return true if obj is a set object or a frozenset object but
57 not an instance of a subtype."""
58 w_obj_type = space.type(w_obj)
59 return (space.is_w(w_obj_type, space.gettypefor(W_SetObject)) or
60 space.is_w(w_obj_type, space.gettypefor(W_FrozensetObject)))
61
62 @cpython_api([PyObject], PyObject)
63 def PySet_New(space, w_iterable):
64 """Return a new set containing objects returned by the iterable. The
65 iterable may be NULL to create a new empty set. Return the new set on
66 success or NULL on failure. Raise TypeError if iterable is not
67 actually iterable. The constructor is also useful for copying a set
68 (c=set(s))."""
69 if w_iterable is None:
70 return space.call_function(space.w_set)
71 else:
72 return space.call_function(space.w_set, w_iterable)
73
74 @cpython_api([PyObject, PyObject], rffi.INT_real, error=-1)
75 def PySet_Add(space, w_s, w_obj):
76 """Add key to a set instance. Does not apply to frozenset
77 instances. Return 0 on success or -1 on failure. Raise a TypeError if
78 the key is unhashable. Raise a MemoryError if there is no room to grow.
79 Raise a SystemError if set is an not an instance of set or its
80 subtype.
81
82 Now works with instances of frozenset or its subtypes.
83 Like PyTuple_SetItem() in that it can be used to fill-in the
84 values of brand new frozensets before they are exposed to other code."""
85 if not PySet_Check(space, w_s):
86 if isinstance(w_s, W_FrozensetObject) and w_s.cpyext_add_frozen(w_obj):
87 return 0
88 PyErr_BadInternalCall(space)
89 space.call_method(space.w_set, 'add', w_s, w_obj)
90 return 0
91
92 @cpython_api([PyObject, PyObject], rffi.INT_real, error=-1)
93 def PySet_Discard(space, w_s, w_obj):
94 """Return 1 if found and removed, 0 if not found (no action taken), and -1 if an
95 error is encountered. Does not raise KeyError for missing keys. Raise a
96 TypeError if the key is unhashable. Unlike the Python discard()
97 method, this function does not automatically convert unhashable sets into
98 temporary frozensets. Raise PyExc_SystemError if set is an not an
99 instance of set or its subtype."""
100 if not PySet_Check(space, w_s):
101 PyErr_BadInternalCall(space)
102 try:
103 space.call_method(space.w_set, 'remove', w_s, w_obj)
104 except OperationError as e:
105 if e.match(space, space.w_KeyError):
106 return 0
107 raise
108 return 1
109
110
111 @cpython_api([PyObject], PyObject)
112 def PySet_Pop(space, w_set):
113 """Return a new reference to an arbitrary object in the set, and removes the
114 object from the set. Return NULL on failure. Raise KeyError if the
115 set is empty. Raise a SystemError if set is an not an instance of
116 set or its subtype."""
117 return space.call_method(space.w_set, "pop", w_set)
118
119 @cpython_api([PyObject], rffi.INT_real, error=-1)
120 def PySet_Clear(space, w_set):
121 """Empty an existing set of all elements."""
122 space.call_method(space.w_set, 'clear', w_set)
123 return 0
124
125 @cpython_api([rffi.VOIDP], Py_ssize_t, error=CANNOT_FAIL)
126 def PySet_GET_SIZE(space, w_s):
127 """Macro form of PySet_Size() without error checking."""
128 return space.int_w(space.len(w_s))
129
130 @cpython_api([PyObject], Py_ssize_t, error=-1)
131 def PySet_Size(space, ref):
132 """Return the length of a set or frozenset object. Equivalent to
133 len(anyset). Raises a PyExc_SystemError if anyset is not a set, frozenset,
134 or an instance of a subtype."""
135 if not PyAnySet_Check(space, ref):
136 raise oefmt(space.w_TypeError, "expected set object")
137 return PySet_GET_SIZE(space, ref)
138
139 @cpython_api([PyObject, PyObject], rffi.INT_real, error=-1)
140 def PySet_Contains(space, w_obj, w_key):
141 """Return 1 if found, 0 if not found, and -1 if an error is encountered. Unlike
142 the Python __contains__() method, this function does not automatically
143 convert unhashable sets into temporary frozensets. Raise a TypeError if
144 the key is unhashable. Raise PyExc_SystemError if anyset is not a
145 set, frozenset, or an instance of a subtype."""
146 w_res = space.contains(w_obj, w_key)
147 return space.int_w(w_res)
148
149 @cpython_api([PyObject], PyObject)
150 def PyFrozenSet_New(space, w_iterable):
151 """Return a new frozenset containing objects returned by the iterable.
152 The iterable may be NULL to create a new empty frozenset. Return the new
153 set on success or NULL on failure. Raise TypeError if iterable is
154 not actually iterable.
155
156 Now guaranteed to return a brand-new frozenset. Formerly,
157 frozensets of zero-length were a singleton. This got in the way of
158 building-up new frozensets with PySet_Add()."""
159 if w_iterable is None:
160 return space.call_function(space.w_frozenset)
161 else:
162 return space.call_function(space.w_frozenset, w_iterable)
163
164 @cpython_api([PyObject, Py_ssize_tP, PyObjectP, Py_ssize_tP], rffi.INT_real, error=-1)
165 def _PySet_NextEntry(space, w_set, ppos, pkey, phash):
166 if w_set is None or not PyAnySet_Check(space, w_set):
167 PyErr_BadInternalCall(space)
168 return -1
169 if not pkey:
170 PyErr_BadInternalCall(space)
171 return -1
172 pos = ppos[0]
173 py_obj = as_pyobj(space, w_set)
174 py_set = rffi.cast(PySetObject, py_obj)
175 if pos == 0:
176 # Store the current item list in the PySetObject.
177 # w_keys must use the object strategy in order to keep the keys alive
178 w_keys = space.newlist(space.listview(w_set))
179 w_keys.switch_to_object_strategy()
180 oldlist = py_set.c__tmplist
181 py_set.c__tmplist = create_ref(space, w_keys)
182 incref(space, py_set.c__tmplist)
183 decref(space, oldlist)
184 else:
185 if not py_set.c__tmplist:
186 # pos should have been 0, cannot fail so return 0
187 return 0;
188 w_keys = from_ref(space, py_set.c__tmplist)
189 ppos[0] += 1
190 if pos >= space.len_w(w_keys):
191 decref(space, py_set.c__tmplist)
192 py_set.c__tmplist = lltype.nullptr(PyObject.TO)
193 return 0
194 w_key = space.listview(w_keys)[pos]
195 pkey[0] = as_pyobj(space, w_key)
196 if phash:
197 phash[0] = space.hash_w(w_key)
198 return 1
199
200 @cpython_api([PyObject, Py_ssize_tP, PyObjectP], rffi.INT_real, error=-1)
201 def _PySet_Next(space, w_set, ppos, pkey):
202 null = lltype.nullptr(Py_ssize_tP.TO)
203 return _PySet_NextEntry(space, w_set, ppos, pkey, null)