/* {{{ session_close */ int Session_close(PYLIBSSH2_SESSION *self) { PRINTFUNCNAME int rc; char *reason = "end"; PyObject *item; if(self->opened) { while(PySet_Size(self->sftps)) { item = PySet_Pop(self->sftps); Sftp_shutdown((PYLIBSSH2_SFTP*)item); Py_XDECREF(item); } while(PySet_Size(self->channels)) { item = PySet_Pop(self->channels); Channel_close((PYLIBSSH2_CHANNEL*)item); Py_XDECREF(item); } while(PySet_Size(self->listeners)) { item = PySet_Pop(self->listeners); Py_XDECREF(item); } Py_BEGIN_ALLOW_THREADS rc = libssh2_session_disconnect(self->session, reason); Py_END_ALLOW_THREADS self->opened = 0; return rc; } return 0; }
PyObject* semaphore_release(SemaphoreObject *self) { PyObject *m = NULL, *handle; LoopObject *loop; DEBUG("self:%p", self); loop = get_event_loop(); if (loop == NULL) { return NULL; } self->counter++; if (PySet_Size(self->waiters) > 0) { m = PyObject_GetAttrString((PyObject *)self, "_notify_waiter"); if (m == NULL) { return NULL; } handle = loop_schedule_call(loop, 0, m, NULL, NULL); Py_XDECREF(m); Py_XDECREF(handle); if (handle == NULL) { return NULL; } } Py_RETURN_NONE; }
static PyObject* SemaphoreObject_notify_waiter(SemaphoreObject *self, PyObject *args) { PyObject *waiter, *res; if (PySet_Size(self->waiters) > 0 && self->counter >0) { waiter = PySet_Pop(self->waiters); if (waiter == NULL) { return NULL; } res = greenlet_switch(waiter, NULL, NULL); Py_XDECREF(res); Py_DECREF(waiter); if (res == NULL) { return NULL; } } 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 *bpy_lib_write(PyObject *UNUSED(self), PyObject *args, PyObject *kwds) { static const char *kwlist[] = { "filepath", "datablocks", /* optional */ "relative_remap", "fake_user", "compress", NULL, }; /* args */ const char *filepath; char filepath_abs[FILE_MAX]; PyObject *datablocks = NULL; bool use_relative_remap = false, use_fake_user = false, use_compress = false; if (!PyArg_ParseTupleAndKeywords( args, kwds, "sO!|$O&O&O&:write", (char **)kwlist, &filepath, &PySet_Type, &datablocks, PyC_ParseBool, &use_relative_remap, PyC_ParseBool, &use_fake_user, PyC_ParseBool, &use_compress)) { return NULL; } Main *bmain_src = G.main; int write_flags = 0; if (use_relative_remap) { write_flags |= G_FILE_RELATIVE_REMAP; } if (use_compress) { write_flags |= G_FILE_COMPRESS; } BLI_strncpy(filepath_abs, filepath, FILE_MAX); BLI_path_abs(filepath_abs, G.main->name); BKE_blendfile_write_partial_begin(bmain_src); /* array of ID's and backup any data we modify */ struct { ID *id; /* original values */ short id_flag; short id_us; } *id_store_array, *id_store; int id_store_len = 0; PyObject *ret; /* collect all id data from the set and store in 'id_store_array' */ { Py_ssize_t pos, hash; PyObject *key; id_store_array = MEM_mallocN(sizeof(*id_store_array) * PySet_Size(datablocks), __func__); id_store = id_store_array; pos = hash = 0; while (_PySet_NextEntry(datablocks, &pos, &key, &hash)) { if (!pyrna_id_FromPyObject(key, &id_store->id)) { PyErr_Format(PyExc_TypeError, "Expected and ID type, not %.200s", Py_TYPE(key)->tp_name); ret = NULL; goto finally; } else { id_store->id_flag = id_store->id->flag; id_store->id_us = id_store->id->us; if (use_fake_user) { id_store->id->flag |= LIB_FAKEUSER; } id_store->id->us = 1; BKE_blendfile_write_partial_tag_ID(id_store->id, true); id_store_len += 1; id_store++; } } } /* write blend */ int retval = 0; ReportList reports; BKE_reports_init(&reports, RPT_STORE); retval = BKE_blendfile_write_partial(bmain_src, filepath_abs, write_flags, &reports); /* cleanup state */ BKE_blendfile_write_partial_end(bmain_src); if (retval) { BKE_reports_print(&reports, RPT_ERROR_ALL); BKE_reports_clear(&reports); ret = Py_None; Py_INCREF(ret); } else { if (BPy_reports_to_error(&reports, PyExc_IOError, true) == 0) { PyErr_SetString(PyExc_IOError, "Unknown error writing library data"); } ret = NULL; } finally: /* clear all flags for ID's added to the store (may run on error too) */ id_store = id_store_array; for (int i = 0; i < id_store_len; id_store++, i++) { if (use_fake_user) { if ((id_store->id_flag & LIB_FAKEUSER) == 0) { id_store->id->flag &= ~LIB_FAKEUSER; } } id_store->id->us = id_store->id_us; BKE_blendfile_write_partial_tag_ID(id_store->id, false); } MEM_freeN(id_store_array); return ret; }
static Py_ssize_t t_set_seq_length(t_set *self) { return PySet_Size(self->set); }
static PyObject * set_size(PyObject *self, PyObject *so) { return Py_BuildValue("i", PySet_Size(so)); }