static PyObject *t_set_add(t_set *self, PyObject *args) { PyObject *value; int setDirty = 1, result; if (!PyArg_ParseTuple(args, "O|i", &value, &setDirty)) return NULL; if (self->itemvalue.flags & V_PURE) result = PySet_Add(self->set, value); else { value = _prepareValue(self, value); if (!value) return NULL; result = PySet_Add(self->set, value); Py_DECREF(value); } if (result < 0) return NULL; if (setDirty && _t_itemvalue__setDirty((t_itemvalue *) self, 0) < 0) return NULL; Py_RETURN_NONE; }
static PyObject *convertFrom_QSet_0101QAbstractState(void *sipCppV, PyObject *sipTransferObj) { QSet<QAbstractState*> *sipCpp = reinterpret_cast<QSet<QAbstractState*> *>(sipCppV); #line 319 "/home/vikky/Desktop/DVCS/stuff/scrapy/soft/PyQt-x11-gpl-4.11.4/sip/QtCore/qset.sip" // Create the set. PyObject *s; if ((s = PySet_New(NULL)) == NULL) return NULL; // Set the set elements. QSet<QAbstractState *>::const_iterator it = sipCpp->constBegin(); QSet<QAbstractState *>::const_iterator end = sipCpp->constEnd(); while (it != end) { PyObject *tobj; // The explicit (void *) cast allows QAbstractState to be const. if ((tobj = sipConvertFromType((void *)*it, sipType_QAbstractState, sipTransferObj)) == NULL) { Py_DECREF(s); return NULL; } PySet_Add(s, tobj); ++it; } return s; #line 165 "/home/vikky/Desktop/DVCS/stuff/scrapy/soft/PyQt-x11-gpl-4.11.4/QtCore/sipQtCoreQSet0101QAbstractState.cpp" }
static void py_bind_tensor_types(const std::vector<PyTensorType>& tensor_types) { auto torch_module = THPObjectPtr(PyImport_ImportModule("torch")); if (!torch_module) throw python_error(); auto tensor_classes = THPObjectPtr(PyObject_GetAttrString(torch_module.get(), "_tensor_classes")); if (!tensor_classes) throw python_error(); for (auto& tensor_type : tensor_types) { auto name = std::string(tensor_type.name); auto idx = name.rfind("."); auto type_name = name.substr(idx + 1); auto module_name = name.substr(0, idx); auto module_obj = THPObjectPtr(PyImport_ImportModule(module_name.c_str())); if (!module_obj) throw python_error(); PyObject* type_obj = (PyObject*)&tensor_type; Py_INCREF(type_obj); if (PyModule_AddObject(module_obj.get(), type_name.c_str(), type_obj) < 0) { throw python_error(); } if (PySet_Add(tensor_classes.get(), type_obj) < 0) { throw python_error(); } } }
static PyObject *pair_length(PyObject *self, PyObject *_noargs) { PyObject *seen; PyObject *tmp; long length = 0; if (SibNil_Check(self)) { return PyLong_FromLong(length); } seen = PySet_New(NULL); for(; SibPair_CheckExact(self); self = SibPair_CDR(self)) { tmp = PyLong_FromVoidPtr(self); if (PySet_Contains(seen, tmp)) { Py_DECREF(tmp); self = NULL; break; } else { PySet_Add(seen, tmp); Py_DECREF(tmp); length++; } } if (self && ! SibNil_Check(self)) length++; Py_DECREF(seen); return PyLong_FromLong(length); }
int WeakSet_Add(PyObject *_self,PyObject *key){ WeakSet *self = (WeakSet *)_self; int r = WeakSet_Contains((PyObject *)self,key); if (r) return r; // This also propegates errors. remover_closure *closure = PyMem_Malloc(sizeof(remover_closure)); closure->setref = self->selfref; Py_INCREF(closure->setref); PyObject *remover = CFunction_new(WeakSet_remover, WeakSet_remover_dealloc, closure); PyObject *ref = PyWeakref_NewRef((PyObject *)key,remover); if (!ref) goto bail; r = PySet_Add(self->set,ref); if (r) goto bail; r = 0; goto done; bail: r = -1; done: Py_XDECREF(remover); Py_XDECREF(ref); return r; }
PyObject *to_python<types::set<T>>::convert(types::set<T> const &v) { PyObject *obj = PySet_New(nullptr); for (auto const &e : v) PySet_Add(obj, ::to_python(e)); return obj; }
static void pwalk_fillpos(PyObject *pair, PyObject *seen, PyObject *pos) { // checked PyObject *pair_id; SibPair *sp; for (; SibPair_CheckExact(pair); pair = SibPair_CDR(pair)) { sp = (SibPair *) pair; pair_id = PyLong_FromVoidPtr(pair); if (PySet_Contains(seen, pair_id)) { Py_DECREF(pair_id); break; } else { PySet_Add(seen, pair_id); Py_DECREF(pair_id); } if (sp->position) { pos = sp->position; } else { Py_INCREF(pos); sp->position = pos; } if (SibPair_CheckExact(SibPair_CAR(sp))) { pwalk_fillpos(SibPair_CAR(sp), seen, pos); } } }
long SibPair_IsRecursive(PyObject *self) { // checked if (SibNil_Check(self)) return 0; PyObject *seen = PySet_New(NULL); PyObject *pair_id; long result = 0; for ( ; SibPair_CheckExact(self); self = SibPair_CDR(self)) { pair_id = PyLong_FromVoidPtr(self); if (PySet_Contains(seen, pair_id)) { /* seen it, therefore recursive */ Py_DECREF(pair_id); result = 1; break; } else { PySet_Add(seen, pair_id); Py_DECREF(pair_id); } } Py_DECREF(seen); return result; }
long SibPair_IsProper(PyObject *self) { // checked if (SibNil_Check(self)) return 1; PyObject *seen = PySet_New(NULL); PyObject *pair_id; long result = 0; for ( ; SibPair_CheckExact(self); self = SibPair_CDR(self)) { pair_id = PyLong_FromVoidPtr(self); if (PySet_Contains(seen, pair_id)) { /* seen it, therefore recursive */ Py_DECREF(pair_id); result = 1; break; } else { PySet_Add(seen, pair_id); Py_DECREF(pair_id); } } /* it's either recursive and thus proper, or the last item needs to have been a nil, or it's improper */ Py_DECREF(seen); return result || SibNil_Check(self); }
static int _look_for_code_object(PyObject *o, void *all_codes) { if (PyCode_Check(o) && !PySet_Contains((PyObject *)all_codes, o)) { Py_ssize_t i; PyCodeObject *co = (PyCodeObject *)o; if (emit_code_object(co) < 0) return -1; if (PySet_Add((PyObject *)all_codes, o) < 0) return -1; /* as a special case, recursively look for and add code objects found in the co_consts. The problem is that code objects are not created as GC-aware in CPython, so we need to hack like this to hope to find most of them. */ i = PyTuple_Size(co->co_consts); while (i > 0) { --i; if (_look_for_code_object(PyTuple_GET_ITEM(co->co_consts, i), all_codes) < 0) return -1; } } return 0; }
static void print_exception_recursive(PyObject *f, PyObject *value, PyObject *seen) { int err = 0, res; PyObject *cause, *context; if (seen != NULL) { /* Exception chaining */ PyObject *value_id = PyLong_FromVoidPtr(value); if (value_id == NULL || PySet_Add(seen, value_id) == -1) PyErr_Clear(); else if (PyExceptionInstance_Check(value)) { PyObject *check_id = NULL; cause = PyException_GetCause(value); context = PyException_GetContext(value); if (cause) { check_id = PyLong_FromVoidPtr(cause); if (check_id == NULL) { res = -1; } else { res = PySet_Contains(seen, check_id); Py_DECREF(check_id); } if (res == -1) PyErr_Clear(); if (res == 0) { print_exception_recursive( f, cause, seen); err |= PyFile_WriteString( cause_message, f); } } else if (context && !((PyBaseExceptionObject *)value)->suppress_context) { check_id = PyLong_FromVoidPtr(context); if (check_id == NULL) { res = -1; } else { res = PySet_Contains(seen, check_id); Py_DECREF(check_id); } if (res == -1) PyErr_Clear(); if (res == 0) { print_exception_recursive( f, context, seen); err |= PyFile_WriteString( context_message, f); } } Py_XDECREF(context); Py_XDECREF(cause); } Py_XDECREF(value_id); } print_exception(f, value); if (err != 0) PyErr_Clear(); }
/** add an element. * @throw val_err */ void add(const obj& o) { if(_p){ int r = PySet_Add(_p, o.p()); if(r != -1) return; } throw val_err("set add failed"); }
static int _look_for_code_object(PyObject *o, void *all_codes) { if (PyCode_Check(o) && !PySet_Contains((PyObject *)all_codes, o)) { if (emit_code_object((PyCodeObject *)o) < 0) return -1; if (PySet_Add((PyObject *)all_codes, o) < 0) return -1; } return 0; }
static int foreach_libblock_id_user_map_callback( void *user_data, ID *self_id, ID **id_p, int UNUSED(cb_flag)) { IDUserMapData *data = user_data; if (*id_p) { if (data->types_bitmap) { if (!id_check_type(*id_p, data->types_bitmap)) { return IDWALK_RET_NOP; } } if ((GS(self_id->name) == ID_OB) && (id_p == (ID **)&((Object *)self_id)->proxy_from)) { /* We skip proxy_from here, since it some internal pointer which is not irrelevant info for py/API level. */ return IDWALK_RET_NOP; } else if ((GS(self_id->name) == ID_KE) && (id_p == (ID **)&((Key *)self_id)->from)) { /* We skip from here, since it some internal pointer which is not irrelevant info for py/API level. */ return IDWALK_RET_NOP; } /* pyrna_struct_hash() uses ptr.data only, * but pyrna_struct_richcmp() uses also ptr.type, * so we need to create a valid PointerRNA here... */ PyObject *key = data->py_id_key_lookup_only; RNA_id_pointer_create(*id_p, &((BPy_StructRNA *)key)->ptr); PyObject *set; if ((set = PyDict_GetItem(data->user_map, key)) == NULL) { /* limit to key's added already */ if (data->is_subset) { return IDWALK_RET_NOP; } /* Cannot use our placeholder key here! */ key = pyrna_id_CreatePyObject(*id_p); set = PySet_New(NULL); PyDict_SetItem(data->user_map, key, set); Py_DECREF(set); Py_DECREF(key); } if (data->py_id_curr == NULL) { data->py_id_curr = pyrna_id_CreatePyObject(data->id_curr); } PySet_Add(set, data->py_id_curr); } return IDWALK_RET_NOP; }
static PyObject* Cache_cached_get(Cache* self) { PyObject* cached_set = PySet_New(NULL); for(int i=0; i<self->sets*self->ways; i++) { // For each cached cacheline expand to all cached addresses: for(int j=0; j<self->cl_size; j++) { // PySys_WriteStdout("%i %i %i %i\n", self->sets, self->ways, i, self->placement[i].cl_id); PyObject* addr = PyLong_FromUnsignedLong(self->placement[i].cl_id*self->cl_size+j); PySet_Add(cached_set, addr); Py_DECREF(addr); } } return cached_set; }
/* * Convert a list of Value nodes containing the column name as a string to a * pyset of python unicode strings. */ PyObject * valuesToPySet(List *targetlist) { PyObject *result = PySet_New(0); ListCell *lc; foreach(lc, targetlist) { Value *value = (Value *) lfirst(lc); PyObject *pyString = PyString_FromString(strVal(value)); PySet_Add(result, pyString); Py_DECREF(pyString); }
PyObject* setTest(PyObject* self, PyObject* args) { char* plumText = "plum"; PyObject* set; if (!PyArg_ParseTuple(args, "O!", &PySet_Type, &set)) return NULL; puts("Set parsed!"); printf("length: %i\n", PySet_GET_SIZE(set)); PySet_Add(set, PyString_FromString(plumText)); printf("length after add: %i\n", PySet_GET_SIZE(set)); //PyList_SET_ITEM(list, i, PyString_FromString(modText)); //PyList_Append(list, PyString_FromString(appendText)); Py_RETURN_NONE; }
void TestPyOtherSide::testSetToList() { // Test if a Python set is converted to a list PyObject *set = PySet_New(NULL); QVERIFY(set != NULL); PyObject *o = NULL; o = PyLong_FromLong(123); QVERIFY(o != NULL); QVERIFY(PySet_Add(set, o) == 0); o = PyLong_FromLong(321); QVERIFY(o != NULL); QVERIFY(PySet_Add(set, o) == 0); o = PyLong_FromLong(444); QVERIFY(o != NULL); QVERIFY(PySet_Add(set, o) == 0); // This will not be added (no duplicates in a set) o = PyLong_FromLong(123); QVERIFY(o != NULL); QVERIFY(PySet_Add(set, o) == 0); // At this point, we should have 3 items (123, 321 and 444) QVERIFY(PySet_Size(set) == 3); QVariant v = convertPyObjectToQVariant(set); QVERIFY(v.canConvert(QMetaType::QVariantList)); QList<QVariant> l = v.toList(); QVERIFY(l.size() == 3); QVERIFY(l.contains(123)); QVERIFY(l.contains(321)); QVERIFY(l.contains(444)); }
static PyObject *Video_device_get_info(Video_device *self) { ASSERT_OPEN; struct v4l2_capability caps; if(my_ioctl(self->fd, VIDIOC_QUERYCAP, &caps)) { return NULL; } PyObject *set = PySet_New(NULL); if(!set) { return NULL; } struct capability *capability = capabilities; while((void *)capability < (void *)capabilities + sizeof(capabilities)) { if(caps.capabilities & capability->id) { PyObject *s = PyString_FromString(capability->name); if(!s) { Py_DECREF(set); return NULL; } PySet_Add(set, s); } capability++; } return Py_BuildValue("sssO", caps.driver, caps.card, caps.bus_info, set); }
static PyObject *pfoll_iternext(PyObject *self) { SibPairFollower *s = (SibPairFollower *) self; PyObject *current; PyObject *curr_id; PyObject *result = NULL; current = s->current; if (! current) { /* then it's done */ return NULL; } if (s->seen) { /* we're set up to keep a record of what pairs we've already seen, to prevent recursion */ curr_id = PyLong_FromVoidPtr(current); if (PySet_Contains(s->seen, curr_id)) { /* if we've already seen this one, we're done */ Py_DECREF(curr_id); Py_CLEAR(s->current); return NULL; } else { /* otherwise mark it as seen, and continue */ PySet_Add(s->seen, curr_id); Py_DECREF(curr_id); } } /* get ready for the next */ if (SibPair_CheckExact(current)) { s->current = SibPair_CDR(current); Py_INCREF(s->current); } else { s->current = NULL; } /* at this point, we've stolen the old s->current ref as current, and s->current has been modified to point to something else */ if (s->just_items) { /* if we're in items mode, then we want the CAR if current is a pair, otherwise if current is non-nil we return it. */ if (SibPair_CheckExact(current)) { result = SibPair_CAR(current); Py_INCREF(result); Py_DECREF(current); } else if (SibNil_Check(current)) { result = NULL; Py_DECREF(current); } else { result = current; } } else { result = current; } return result; }
/* inspect a message, update the seen set * return 1 if we should pass this message on to the caller * return 0 if we should drop it * return -1 on internal error (exception has been raised) */ static int filter_message(modesreader *self, PyObject *o) { modesmessage *message = (modesmessage *)o; if (message->df == DF_MODEAC) { return self->want_modeac_messages; /* no address in mode a/c, no further filtering possible */ } if (!message->valid) { return self->want_invalid_messages; /* don't process further, contents are dubious */ } if (self->seen != NULL && self->seen != Py_None) { if (message->df == 11 || message->df == 17 || message->df == 18) { /* note that we saw this aircraft, even if the message is filtered. * only do this for CRC-checked messages as we get a lot of noise * otherwise. */ if (PySet_Add(self->seen, message->address) < 0) { return -1; } } } if (message->timestamp == 0 && !self->want_zero_timestamps) { return 0; } if (message->timestamp == MAGIC_MLAT_TIMESTAMP && !self->want_mlat_messages) { return 0; } if ((self->default_filter == NULL || self->default_filter == Py_None) && (self->specific_filter == NULL || self->specific_filter == Py_None)) { /* no filters installed, match everything */ return 1; } /* check per-type filters */ if (self->default_filter != NULL && self->default_filter != Py_None) { int rv; PyObject *entry = PySequence_GetItem(self->default_filter, message->df); if (entry == NULL) return -1; rv = PyObject_IsTrue(entry); Py_DECREF(entry); if (rv != 0) return rv; } if (self->specific_filter != NULL && self->specific_filter != Py_None) { int rv; PyObject *entry = PySequence_GetItem(self->specific_filter, message->df); if (entry == NULL) return -1; if (entry == Py_None) { rv = 0; } else { rv = PySequence_Contains(entry, message->address); } Py_DECREF(entry); if (rv != 0) return rv; } return 0; }
int Context_add_object(Context* cx, PyObject* val) { return PySet_Add((PyObject*) cx->objects, val); }
PyObject* semaphore_acquire(SemaphoreObject *self, PyObject *blocking, long timeout) { PyObject *res = NULL; PyObject *current = NULL; PyObject *timer = NULL; LoopObject *loop = NULL; DEBUG("self:%p timeout:%ld counter:%d", self, timeout, self->counter); if (self->counter > 0) { self->counter--; DEBUG("counter decr self:%p", self); Py_RETURN_TRUE; } else if(PyObject_Not(blocking)) { Py_RETURN_FALSE; } loop = get_event_loop(); if (loop == NULL) { return NULL; } current = greenlet_getcurrent(); Py_XDECREF(current); if (current == NULL) { return NULL; } timer = loop_set_timeout(loop, timeout, NULL); if (timer == NULL) { return NULL; } if (PySet_Add(self->waiters, current) == -1) { ((TimerObject*)timer)->cancelled = 1; return NULL; } while (self->counter <= 0) { res = loop_switch(loop); Py_XDECREF(res); if (res == NULL) { if (PyErr_ExceptionMatches(TimeoutException)) { RDEBUG("catch TimeoutException self:%p", self); if (fetch_timer() == timer) { PyErr_Clear(); res = Py_False; goto fin; } } goto fin; } } self->counter--; res = Py_True; fin: ((TimerObject*)timer)->cancelled = 1; if (PySet_Discard(self->waiters, current) == -1) { res = NULL; } Py_DECREF(timer); Py_XINCREF(res); return res; }