static int already_warned(PyObject *registry, PyObject *key, int should_set) { PyObject *already_warned; if (key == NULL) return -1; already_warned = PyDict_GetItem(registry, key); if (already_warned != NULL) { int rc = PyObject_IsTrue(already_warned); if (rc != 0) return rc; } /* This warning wasn't found in the registry, set it. */ if (should_set) return PyDict_SetItem(registry, key, Py_True); return 0; }
PyObject* rtopHash(VALUE rHash) { PyObject *pDict,*pKey,*pVal; VALUE rKeys; VALUE rKey, rVal; int i; pDict = PyDict_New(); rKeys = rb_funcall(rHash, rb_intern("keys"), 0); for(i = 0; i < RARRAY_LEN(rKeys); i++) { rKey = rb_ary_entry(rKeys, i); rVal = rb_hash_aref(rHash, rKey); PyDict_SetItem(pDict, rtopObject(rKey, 1), rtopObject(rVal, 0)); } return pDict; }
//------------------------------------------------------------------------ //<inline(py_name)> //------------------------------------------------------------------------ PyObject *get_debug_names(ea_t ea1, ea_t ea2) { // Get debug names ea_name_vec_t names; PYW_GIL_CHECK_LOCKED_SCOPE(); Py_BEGIN_ALLOW_THREADS; get_debug_names(&names, ea1, ea2); Py_END_ALLOW_THREADS; PyObject *dict = Py_BuildValue("{}"); if ( dict != NULL ) { for ( ea_name_vec_t::iterator it=names.begin(); it != names.end(); ++it ) { PyDict_SetItem(dict, Py_BuildValue(PY_BV_EA, bvea_t(it->ea)), PyString_FromString(it->name.c_str())); } } return dict; }
static PyObject* tags_statistics_to_dict(libtocc::TagStatisticsCollection* statistics) { PyObject* result = PyDict_New(); if (statistics->size() <= 0) { // Returning an empty dict. return result; } libtocc::TagStatisticsCollection::Iterator iterator(statistics); for (; !iterator.is_finished(); iterator.next()) { libtocc::TagStatistics item = iterator.get(); PyDict_SetItem(result, PyUnicode_FromString(item.get_tag()), PyLong_FromLong(item.get_assigned_files())); } return result; }
static ExpatStatus builder_NamespaceDecl(void *arg, PyObject *prefix, PyObject *uri) { ParserState *state = (ParserState *)arg; #ifdef DEBUG_PARSER fprintf(stderr, "--- builder_NamespaceDecl(prefix="); PyObject_Print(prefix, stderr, 0); fprintf(stderr, ", uri="); PyObject_Print(uri, stderr, 0); fprintf(stderr, ")\n"); #endif if (uri == Py_None) uri = empty_string; if (PyDict_SetItem(state->new_namespaces, prefix, uri) < 0) return EXPAT_STATUS_ERROR; return EXPAT_STATUS_OK; }
// ----------------------------------------------------------------------------- PyObject* graph_all_pairs_shortest_path(PyObject* self, PyObject* _) { INIT_SELF_GRAPH(); std::map<Node*, ShortestPathMap*> all_pairs = so->_graph->all_pairs_shortest_path(); PyObject *res = PyDict_New(); for(std::map<Node*, ShortestPathMap*>::iterator it = all_pairs.begin(); it != all_pairs.end(); it++) { Node* source_node = it->first; ShortestPathMap* path = it->second; PyObject* pypath = pathmap_to_dict(path); PyObject* pysource = dynamic_cast<GraphDataPyObject*>(source_node->_value)->data; PyDict_SetItem(res, pysource, pypath); Py_DECREF(pypath); delete path; } return res; }
PyObject * test(PyObject *self, PyObject *args) { PyObject *dict = NULL; PyObject *key = NULL; PyObject *value = NULL; dict = PyDict_New(); if (!dict) { goto error; } key = PyLong_FromLong(500); if (!key) { goto error; } value = PyLong_FromLong(1000); if (!value) { goto error; } if (-1 == PyDict_SetItem(dict, key, value)) { goto error; } /* The successful call added refs on both "key" and "value", owned by the dictionary. We must now drop our references on them: */ Py_DECREF(key); Py_DECREF(value); return dict; error: Py_XDECREF(dict); Py_XDECREF(key); Py_XDECREF(value); return NULL; }
static PyObject *set_intersection(PyObject *module, PyObject *args) { PyObject *a, *b, *set, *item; if (!PyArg_ParseTuple(args, "OO:Intersection", &a, &b)) return NULL; if (PyObject_IsTrue(a) == 0 || PyObject_IsTrue(b) == 0) /* either a or b are empty so the intersection is empty as well */ return PyList_New(0); set = PyDict_New(); if (set == NULL) return NULL; a = make_dict(a); if (a == NULL) { Py_DECREF(set); return NULL; } b = PyObject_GetIter(b); if (b == NULL) { Py_DECREF(a); Py_DECREF(set); return NULL; } while ((item = PyIter_Next(b)) != NULL) { if (PyDict_GetItem(a, item) != NULL) { if (PyDict_SetItem(set, item, Py_True) == -1) { Py_DECREF(item); Py_DECREF(b); Py_DECREF(a); Py_DECREF(set); return NULL; } } Py_DECREF(item); } Py_DECREF(b); Py_DECREF(a); return make_ordered_set(set); }
void PyString_InternInPlace(PyObject **p) { LOG("> PyString_InternInPlace\n"); { register PyStringObject *s = (PyStringObject *)(*p); PyObject *t; if (s == NULL || !PyString_Check(s)) Py_FatalError("PyString_InternInPlace: strings only please!"); if (interned == NULL) { interned = PyDict_New(); if (interned == NULL) { return; } } if ((t = PyDict_GetItem(interned, (PyObject *)s)) !=NULL) { Py_INCREF(t); Py_DECREF(*p); *p = t; return; } /* Ensure that only true string objects appear in the intern dict */ if (!PyString_CheckExact(s)) { t = PyString_FromStringAndSize(PyString_AS_STRING(s), PyString_GET_SIZE(s)); if (t == NULL) { /* ERROR */ return; } } else { t = (PyObject*) s; Py_INCREF(t); } if (PyDict_SetItem(interned, t, t) == 0) { ((PyObject *)t)->ob_refcnt-=2; PyString_CHECK_INTERNED(t) = SSTATE_INTERNED_MORTAL; Py_DECREF(*p); *p = t; return; } Py_DECREF(t); }}
static PyObject * do_mkdict(char **p_format, va_list *p_va, int endchar, int n) { PyObject *d; int i; if (n < 0) return NULL; if ((d = PyDict_New()) == NULL) return NULL; for (i = 0; i < n; i+= 2) { PyObject *k, *v; int err; k = do_mkvalue(p_format, p_va); if (k == NULL) { Py_DECREF(d); return NULL; } v = do_mkvalue(p_format, p_va); if (v == NULL) { Py_DECREF(k); Py_DECREF(d); return NULL; } err = PyDict_SetItem(d, k, v); Py_DECREF(k); Py_DECREF(v); if (err < 0) { Py_DECREF(d); return NULL; } } if (d != NULL && **p_format != endchar) { Py_DECREF(d); d = NULL; PyErr_SetString(PyExc_SystemError, "Unmatched paren in format"); } else if (endchar) ++*p_format; return d; }
int PEVENT_PLUGIN_LOADER(struct pevent *pevent) { PyObject *globals, *m, *py_pevent, *str, *res; char **plugin_list; /* Only load plugins if they exist */ plugin_list = trace_util_find_plugin_files(".py"); if (!plugin_list) return 0; trace_util_free_plugin_files(plugin_list); Py_Initialize(); m = PyImport_AddModule("__main__"); globals = PyModule_GetDict(m); res = PyRun_String(pypath, Py_file_input, globals, globals); if (!res) { PyErr_Print(); return -1; } else Py_DECREF(res); str = PyString_FromString("pevent"); if (!str) return -ENOMEM; py_pevent = PyLong_FromUnsignedLong((unsigned long)pevent); if (!py_pevent) return -ENOMEM; if (PyDict_SetItem(globals, str, py_pevent)) fprintf(stderr, "failed to insert pevent\n"); Py_DECREF(py_pevent); Py_DECREF(str); trace_util_load_plugins(pevent, ".py", load_plugin, globals); return 0; }
DEFINEFN void psyco_stats_reset(void) { /* reset all stats */ int i = 0; PyObject *key, *value, *d; stats_printf(("stats: reset\n")); /* reset the charge of all PyCodeStats, keep only the used ones */ RECLIMIT_SAFE_ENTER(); d = PyDict_New(); if (d == NULL) OUT_OF_MEMORY(); while (PyDict_Next(codestats_dict, &i, &key, &value)) { PyCodeStats* cs = (PyCodeStats*) key; if (cs->st_mergepoints) { /* clear the charge and keep alive */ cs->st_charge = 0.0f; if (PyDict_SetItem(d, key, value)) OUT_OF_MEMORY(); } } RECLIMIT_SAFE_LEAVE(); Py_DECREF(codestats_dict); codestats_dict = d; charge_total = 0.0; charge_prelimit = 0.0f; /* reset the time measure in all threads */ { #if MEASURE_ALL_THREADS PyInterpreterState* istate = PyThreadState_Get()->interp; PyThreadState* tstate; for (tstate=istate->tstate_head; tstate; tstate=tstate->next) { (void) get_measure(tstate); } #else (void) get_measure(NULL); #endif } }
static PyObject* WrapCore(PyObject *oldCap, bool owned) { auto_pyobject cap = PyObject_CallFunctionObjArgs(GetCapsuleClass(), oldCap, NULL); auto_pyobject cls = PyObject_CallMethod(*cap, "get_class", ""); auto_pyobject addr = GetPointer(oldCap); // look up cached object auto_pyobject cache_cls = PyObject_GetItem(GetCache(), *cls); Assert(*cache_cls); PyObject* obj = NULL; obj = PyObject_GetItem(*cache_cls, *addr); if (obj) { /* cache hit */ } else { if (!PyErr_ExceptionMatches(PyExc_KeyError)) return NULL; /* cache miss */ PyErr_Clear(); if (!owned) { auto_pyobject hasDtor = PyObject_CallMethod(*cls, "_has_dtor", ""); if (PyObject_IsTrue(*hasDtor)) { auto_pyobject name = GetName(oldCap); auto_pyobject key = PyTuple_Pack(2, *name, *addr); auto_pyobject val = PyObject_GetAttrString(*cls, "_delete_"); int ok = PyDict_SetItem(GetAddrDtorDict(), *key, *val); Assert(ok != -1); } } obj = PyObject_CallMethod(*cap, "instantiate", ""); int ok = PyObject_SetItem(*cache_cls, *addr, obj); Assert(ok != -1); } Assert(obj); return obj; }
/* * Utility function to extract an FrEvent's parameters into a Python dictionary */ static PyObject *extract_event_dict(FrEvent *event) { PyObject *event_dict = NULL; size_t j; /* each FrEvent will be stored in a dict */ event_dict = PyDict_New(); if (!event_dict) return NULL; /* guarantee these parameters exist */ PyDict_SetItemString(event_dict, "name", PyString_FromString(event->name)); PyDict_SetItemString(event_dict, "comment", PyString_FromString(event->comment)); PyDict_SetItemString(event_dict, "inputs", PyString_FromString(event->inputs)); PyDict_SetItemString(event_dict, "GTimeS", PyLong_FromUnsignedLong(event->GTimeS)); PyDict_SetItemString(event_dict, "GTimeN", PyLong_FromUnsignedLong(event->GTimeN)); PyDict_SetItemString(event_dict, "timeBefore", PyFloat_FromDouble(event->timeBefore)); PyDict_SetItemString(event_dict, "timeAfter", PyFloat_FromDouble(event->timeAfter)); PyDict_SetItemString(event_dict, "eventStatus", PyLong_FromUnsignedLong(event->eventStatus)); PyDict_SetItemString(event_dict, "amplitude", PyFloat_FromDouble(event->amplitude)); PyDict_SetItemString(event_dict, "probability", PyFloat_FromDouble(event->probability)); PyDict_SetItemString(event_dict, "statistics", PyString_FromString(event->statistics)); /* additional parameters */ for (j = 0; j < event->nParam; j++) { PyDict_SetItem(event_dict, PyString_FromString(event->parameterNames[j]), PyFloat_FromDouble(event->parameters[j])); } return event_dict; }
void weechat_python_hashtable_map_cb (void *data, struct t_hashtable *hashtable, const char *key, const char *value) { PyObject *dict, *dict_key, *dict_value; /* make C compiler happy */ (void) hashtable; dict = (PyObject *)data; dict_key = Py_BuildValue ("s", key); dict_value = Py_BuildValue ("s", value); PyDict_SetItem (dict, dict_key, dict_value); Py_DECREF (dict_key); Py_DECREF (dict_value); }
/* def _getcache(self, provided, name): cache = self._cache.get(provided) if cache is None: cache = {} self._cache[provided] = cache if name: c = cache.get(name) if c is None: c = {} cache[name] = c cache = c return cache */ static PyObject * _subcache(PyObject *cache, PyObject *key) { PyObject *subcache; subcache = PyDict_GetItem(cache, key); if (subcache == NULL) { int status; subcache = PyDict_New(); if (subcache == NULL) return NULL; status = PyDict_SetItem(cache, key, subcache); Py_DECREF(subcache); if (status < 0) return NULL; } return subcache; }
PyObject* make_callback_dict(obj_list plugin_list){ PyObject* dict = PyDict_New(); int index = 0; while(index++ < obj_list_len(plugin_list)){ PyObject* cur = obj_list_get(plugin_list, index); //Might need to add self reference to args, dunno PyObject* tuple = PyTuple_New((Py_ssize_t)0); if(!tuple){ printf("Couldn't make tuple ?\n"); PyErr_Print(); } PyObject* getName = PyObject_GetAttrString(cur, "getName"); if(!getName){ printf("Couldn't get the getName method...\n"); PyErr_Print(); } PyObject* plugin_name = PyObject_Call(getName, tuple, NULL); if(!plugin_name){ printf("Couldn't call the getName method...\n"); PyErr_Print(); } Py_DECREF(tuple); Py_DECREF(getName); PyObject* cur_plugin_write = PyObject_GetAttrString(cur, "addMessage"); if(!cur_plugin_write){ printf("Couldn't get addMessage method...\n"); PyErr_Print(); } if(!PyMethod_Check(cur_plugin_write) && !PyFunction_Check(cur_plugin_write)){ printf("Not function or method...\n"); } printf("Does it have the __call__ attribute %d\n", PyObject_HasAttrString(cur_plugin_write, "__call__")); PyDict_SetItem(dict, plugin_name, cur_plugin_write); Py_DECREF(plugin_name); Py_DECREF(cur_plugin_write); } return dict; }
static BSON_INLINE void cbson_loads_set_item (PyObject *obj, const char *key, PyObject *value) { PyObject *keyobj; if (PyDict_Check(obj)) { if (*key == '_' && !strcmp(key, "_id")) { keyobj = gStr_id; Py_INCREF(keyobj); } else if (!(keyobj = PyUnicode_DecodeUTF8(key, strlen(key), "strict"))) { keyobj = PyString_FromString(key); } if (keyobj) { PyDict_SetItem(obj, keyobj, value); Py_DECREF(keyobj); } } else { PyList_Append(obj, value); } }
DEFINEFN PyObject* psyco_thread_dict() { PyObject* dict = PyThreadState_GetDict(); PyObject* result; bool err; if (dict == NULL) return NULL; result = PyDict_GetItem(dict, thread_dict_key); if (result == NULL) { result = PyDict_New(); if (result == NULL) return NULL; err = PyDict_SetItem(dict, thread_dict_key, result); Py_DECREF(result); /* one reference left in 'dict' */ if (err) result = NULL; } return result; }
PYCURL_INTERNAL int my_setattro(PyObject **dict, PyObject *name, PyObject *v) { if( *dict == NULL ) { *dict = PyDict_New(); if( *dict == NULL ) return -1; } if (v != NULL) return PyDict_SetItem(*dict, name, v); else { int v = PyDict_DelItem(*dict, name); if (v != 0) { /* need to convert KeyError to AttributeError */ if (PyErr_ExceptionMatches(PyExc_KeyError)) { PyErr_SetString(PyExc_AttributeError, "trying to delete a non-existing attribute"); } } return v; } }
static PyObject * cnter_exp(cnterobject *dd) { Py_ssize_t i; PyObject *key, *value; i = 0; while (PyDict_Next((PyObject*)dd, &i, &key, &value)) { int ok; PyObject *newValue = PyFloat_FromDouble(sloppy_exp(PyFloat_AsDouble(value))); ok = PyDict_SetItem((PyObject*)dd, key, newValue); Py_DECREF(newValue); if (ok < 0) return NULL; } dd->default_value = sloppy_exp(dd->default_value); Py_INCREF(Py_None); return Py_None; }
DEFINEFN PyObject* psyco_stats_dump(void) { PyObject* d = PyDict_New(); int i = 0; PyObject *key, *value; if (d == NULL) return NULL; while (PyDict_Next(codestats_dict, &i, &key, &value)) { PyCodeStats* cs = (PyCodeStats*) key; PyObject* o = PyFloat_FromDouble(cs->st_charge); extra_assert(PyCStruct_Check(key)); extra_assert(PyCode_Check(cs->cs_key)); if (o == NULL || PyDict_SetItem(d, cs->cs_key, o)) { Py_DECREF(d); return NULL; } } stats_dump(); return d; }
PyObject *SCA_PythonKeyboard::pyattr_get_active_events(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) { SCA_PythonKeyboard* self = static_cast<SCA_PythonKeyboard*>(self_v); PyDict_Clear(self->m_event_dict); for (int i = SCA_IInputDevice::KX_BEGINKEY; i <= SCA_IInputDevice::KX_ENDKEY; i++) { const SCA_InputEvent & inevent = self->m_keyboard->GetEventValue((SCA_IInputDevice::KX_EnumInputs)i); if (inevent.m_status != SCA_InputEvent::KX_NO_INPUTSTATUS) { PyObject *key = PyLong_FromLong(i); PyObject *value = PyLong_FromLong(inevent.m_status); PyDict_SetItem(self->m_event_dict, key, value); Py_DECREF(key); Py_DECREF(value); } } Py_INCREF(self->m_event_dict); return self->m_event_dict; }
PyObject* read_block_type_table(Reader* r) { PyObject* block_type_table = PyDict_New(); FAIL_IF(block_type_table == NULL); uint32_t block_type_count; READ(block_type_count); for (uint32_t i = 0; i < block_type_count; ++i) { struct { uint16_t old_id; uint8_t shape; uint8_t name_len; } x; READ(x); PyObject* name = read_string(r, x.name_len); FAIL_IF(name == NULL); PyObject* id_obj = PyLong_FromLong(x.old_id); if (id_obj == NULL) { Py_DECREF(name); goto fail; } if (PyDict_SetItem(block_type_table, id_obj, name) < 0) { Py_DECREF(name); Py_DECREF(id_obj); goto fail; } Py_DECREF(name); Py_DECREF(id_obj); } return block_type_table; fail: SET_EXC(); Py_XDECREF(block_type_table); return NULL; }
static PyObject *Scopeable_update(PyObject *self, PyObject *args) { PyObject *newDict, *defaults, *key, *value; int pos; if (!PyArg_ParseTuple(args, "OO", &self, &newDict)) { return NULL; } if (!PyDict_Check(newDict)) { PyErr_SetString(PyExc_TypeError, "a dictionary is expected for update"); return NULL; } defaults=_defaults(self); if (PyObject_IsTrue(defaults)) { pos=0; while (PyDict_Next(newDict, &pos, &key, &value)) { PyDict_SetItem(defaults, key, value); } _resetMash(self); } Py_DECREF(defaults); Py_INCREF(Py_None); return Py_None; }
static int fudgepyc_typesAddModuleAttrs ( PyObject * module, const char * dictname, AttrDef * attrs ) { PyObject * intobj = 0, * strobj = 0, * namedict; AttrDef * attrdef; if ( ! ( namedict = PyDict_New ( ) ) ) return -1; for ( attrdef = attrs; attrdef->name; ++attrdef ) { if ( PyModule_AddIntConstant ( module, attrdef->name, attrdef->id ) ) goto clean_and_fail; intobj = PyInt_FromLong ( attrdef->id ); strobj = PyString_FromString ( attrdef->str ); if ( ! ( intobj && strobj ) ) goto clean_and_fail_loop; if ( PyDict_SetItem ( namedict, intobj, strobj ) ) goto clean_and_fail_loop; Py_DECREF( intobj ); Py_DECREF( strobj ); } return PyModule_AddObject ( module, dictname, namedict ); clean_and_fail_loop: Py_XDECREF( intobj ); Py_XDECREF( strobj ); clean_and_fail: Py_XDECREF( namedict ); return -1; }
static PyObject* from_table_kobject(K x){ PyObject* result ; Py_ssize_t i, length; PyObject *keys ; PyObject *vals ; keys = from_any_kobject( kK(x->k)[0] ); vals = from_columns_kobject( kK(x->k)[1] ); result = PyDict_New(); if( PyList_CheckExact(keys)) { length = PyList_Size(keys); for(i = 0; i != length; ++i) { PyDict_SetItem(result, PyList_GetItem(keys, i), PyList_GetItem(vals, i)); } } Py_DECREF(keys); Py_DECREF(vals); return result; }
/* -------------------------------------------------------------------------- \ SetPyDict Set Elemements from a Python Dictonary int PyDict_SetItem(PyObject *p, PyObject *key, PyObject *val) ============== Insert value into the dictionary p with a key of key. key must be hashable; if it isn’t, TypeError will be raised. Return 0 on success or -1 on failure. \ -------------------------------------------------------------------------- */ SEXP r_set_py_dict(SEXP dict_name, SEXP key, SEXP value){ SEXP success; PyObject *py_dict, *py_key, *py_value; const char *c_dict_name = R_TO_C_STRING(dict_name); // the key is more complex than before since allowed values are // int, long, float, unicode, str and tuple py_key = r_to_py(key); py_value = r_to_py(value); py_dict = py_get_py_obj(c_dict_name); if (PyDict_Check(py_dict)){ // PyDict_SetItem returns 0 on success or -1 on failure. success = c_to_r_integer(PyDict_SetItem(py_dict, py_key, py_value)); }else{ success = c_to_r_integer(-2); } Py_XDECREF(py_dict); return success; }
static char * match(const char *pattern, const char *data, PyObject *results, PyObject *mapped_results) { Py_ssize_t i, x, k, v; i = 0; x = 0; while (pattern[i] && data[x]) { if (pattern[i] == '{' && pattern[i + 1] == '{') ++i; else if (pattern[i] == '}' && pattern[i + 1] == '}') ++i; else if (pattern[i] == '{') { k = charspan(&pattern[i], '}'); if (pattern[i + k] != '}') return "end of string while looking for end of qualifier"; v = charspan(&data[x], pattern[i + k + 1]); if (data[x + v] != pattern[i + k + 1]) return "end of string while matching"; if (k == 1) PyList_Append(results, Py_BuildValue("s#", &data[x], v)); else PyDict_SetItem(mapped_results, Py_BuildValue("s#", &pattern[i + 1], k - 1), Py_BuildValue("s#", &data[x], v)); i += k; x += v - 1; } else if (pattern[i] != data[x]) break ; ++i; ++x; } if (pattern[i] != data[x]) return "mis-match in values"; return NULL; }
static PyObject* usbobserver_get_mounted_filesystems(PyObject *self, PyObject *args) { struct statfs *buf, t; int num, i; PyObject *ans, *key, *val; num = getfsstat(NULL, 0, MNT_NOWAIT); if (num == -1) { PyErr_SetString(PyExc_RuntimeError, "Initial call to getfsstat failed"); return NULL; } ans = PyDict_New(); if (ans == NULL) return PyErr_NoMemory(); buf = (struct statfs*)calloc(num, sizeof(struct statfs)); if (buf == NULL) return PyErr_NoMemory(); num = getfsstat(buf, num*sizeof(struct statfs), MNT_NOWAIT); if (num == -1) { PyErr_SetString(PyExc_RuntimeError, "Call to getfsstat failed"); return NULL; } for (i = 0 ; i < num; i++) { t = buf[i]; key = PyBytes_FromString(t.f_mntfromname); val = PyBytes_FromString(t.f_mntonname); if (key != NULL && val != NULL) { PyDict_SetItem(ans, key, val); } NUKE(key); NUKE(val); } free(buf); return ans; }