MCPcopy Create free account
hub / github.com/Distributive-Network/PythonMonkey / JSObjectKeysProxy_intersect

Method JSObjectKeysProxy_intersect

src/JSObjectKeysProxy.cc:232–316  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

230}
231
232PyObject *JSObjectKeysProxyMethodDefinitions::JSObjectKeysProxy_intersect(JSObjectKeysProxy *self, PyObject *other) {
233 PyObject *result;
234 PyObject *it;
235 PyObject *key;
236 Py_ssize_t len_self;
237 int rv;
238
239 // Python interpreter swaps parameters when dict view is on right side of &
240 if (!PyDictViewSet_Check(self)) {
241 PyObject *tmp = other;
242 other = (PyObject *)self;
243 self = (JSObjectKeysProxy *)tmp;
244 }
245
246 if (PyObject_TypeCheck(self, &JSObjectKeysProxyType)) {
247 len_self = JSObjectKeysProxyMethodDefinitions::JSObjectKeysProxy_length(self);
248 }
249 else {
250 len_self = dictview_len((_PyDictViewObject *)self);
251 }
252
253 // if other is a set and self is smaller than other, reuse set intersection logic
254 if (PySet_Check(other) && len_self <= PyObject_Size(other)) {
255 return PyObject_CallMethod(other, "intersection", "O", self);
256 }
257
258 // if other is another dict view, and it is bigger than self, swap them
259 if (PyDictViewSet_Check(other)) {
260 Py_ssize_t len_other = dictview_len((_PyDictViewObject *)other);
261 if (len_other > len_self) {
262 PyObject *tmp = other;
263 other = (PyObject *)self;
264 self = (JSObjectKeysProxy *)tmp;
265 }
266 }
267
268 /* at this point, two things should be true
269 1. self is a dictview
270 2. if other is a dictview then it is smaller than self */
271 result = PySet_New(NULL);
272 if (result == NULL) {
273 return NULL;
274 }
275
276 it = PyObject_GetIter(other);
277 if (it == NULL) {
278 Py_DECREF(result);
279 return NULL;
280 }
281
282 while ((key = PyIter_Next(it)) != NULL) {
283 if (PyObject_TypeCheck(self, &JSObjectKeysProxyType)) {
284 rv = JSObjectKeysProxyMethodDefinitions::JSObjectKeysProxy_contains(self, key);
285 }
286 else {
287 if (((_PyDictViewObject *)self)->dv_dict == NULL) {
288 rv = 0;
289 } else {

Callers

nothing calls this directly

Calls 1

dictview_lenFunction · 0.85

Tested by

no test coverage detected