/* * Return an instance of the dialect type, given a Python instance or kwarg * description of the dialect */ static PyObject * _call_dialect(PyObject *dialect_inst, PyObject *kwargs) { PyObject *ctor_args; PyObject *dialect; ctor_args = Py_BuildValue(dialect_inst ? "(O)" : "()", dialect_inst); if (ctor_args == NULL) return NULL; dialect = PyObject_Call((PyObject *)&Dialect_Type, ctor_args, kwargs); Py_DECREF(ctor_args); return dialect; }
static PyObject * Node_traverse(Node *self, PyObject *args, PyObject *kwargs) { PyObject *f, *nargs, *it, *rc; f = PyTuple_GetItem(args, 0); if (!f) { PyErr_SetString(PyExc_TypeError, "function takes at least 1 argument"); return NULL; } if (!PyCallable_Check(f)) { PyErr_SetString(PyExc_TypeError, "first parameter must be a callable object"); return NULL; } it = PyObject_GetIter(args); nargs = PySequence_Tuple(it); Py_DECREF(it); Py_INCREF(self); if (PyTuple_SetItem(nargs, 0, (PyObject *)self)) goto err; if (!(rc = PyObject_Call(f, nargs, kwargs))) goto err; Py_DECREF(rc); Py_DECREF(nargs); if (NOT_NONE(self->left)) { rc = Node_traverse(self->left, args, kwargs); if (rc) Py_DECREF(rc); else return NULL; } if (NOT_NONE(self->right)) { rc = Node_traverse(self->right, args, kwargs); if (rc) Py_DECREF(rc); else return NULL; } Py_INCREF(Py_None); return Py_None; err: Py_DECREF(nargs); return NULL; }
static void gcc_python_finish_invoking_callback(PyGILState_STATE gstate, int expect_wrapped_data, PyObject *wrapped_gcc_data, void *user_data) { struct callback_closure *closure = (struct callback_closure *)user_data; PyObject *args = NULL; PyObject *result = NULL; location_t saved_loc = input_location; enum plugin_event saved_event; assert(closure); /* We take ownership of wrapped_gcc_data. For some callbacks types it will always be NULL; for others, it's only NULL if an error has occurred: */ if (expect_wrapped_data && !wrapped_gcc_data) { goto cleanup; } if (cfun) { /* Temporarily override input_location to the top of the function: */ input_location = cfun->function_start_locus; } args = gcc_python_closure_make_args(closure, 1, wrapped_gcc_data); if (!args) { goto cleanup; } saved_event = current_event; current_event = closure->event; result = PyObject_Call(closure->callback, args, closure->kwargs); current_event = saved_event; if (!result) { /* Treat an unhandled Python error as a compilation error: */ gcc_python_print_exception("Unhandled Python exception raised within callback"); } // FIXME: the result is ignored cleanup: Py_XDECREF(wrapped_gcc_data); Py_XDECREF(args); Py_XDECREF(result); PyGILState_Release(gstate); input_location = saved_loc; }
static PyObject * csv_reader(PyObject *module, PyObject *args, PyObject *keyword_args) { PyObject * iterator, * dialect = NULL, *ctor_args; ReaderObj * self = PyObject_GC_New(ReaderObj, &Reader_Type); if (!self) return NULL; self->dialect = NULL; self->input_iter = self->fields = NULL; self->fields = NULL; self->input_iter = NULL; self->had_parse_error = 0; self->field = NULL; self->field_size = 0; self->field_len = 0; self->state = START_RECORD; if (!PyArg_UnpackTuple(args, "", 1, 2, &iterator, &dialect)) { Py_DECREF(self); return NULL; } self->input_iter = PyObject_GetIter(iterator); if (self->input_iter == NULL) { PyErr_SetString(PyExc_TypeError, "argument 1 must be an iterator"); Py_DECREF(self); return NULL; } ctor_args = Py_BuildValue(dialect ? "(O)" : "()", dialect); if (ctor_args == NULL) { Py_DECREF(self); return NULL; } self->dialect = (DialectObj *)PyObject_Call((PyObject *)&Dialect_Type, ctor_args, keyword_args); Py_DECREF(ctor_args); if (self->dialect == NULL) { Py_DECREF(self); return NULL; } self->fields = PyList_New(0); if (self->fields == NULL) { Py_DECREF(self); return NULL; } return (PyObject *)self; }
/* Pobieranie obiektu podobnego do pliku i wyświetlanie bajtów w strumieniu stdout */ static PyObject *py_consume_file(PyObject *self, PyObject *args) { PyObject *obj; PyObject *read_meth; PyObject *result = NULL; PyObject *read_args; if (!PyArg_ParseTuple(args,"O", &obj)) { return NULL; } /* Pobieranie metody read przekazanego obiektu */ if ((read_meth = PyObject_GetAttrString(obj, "read")) == NULL) { return NULL; } /* Tworzenie listy argumentów dla metody read() */ read_args = Py_BuildValue("(i)", CHUNK_SIZE); while (1) { PyObject *data; PyObject *enc_data; char *buf; Py_ssize_t len; /* Wywołanie metody read() */ if ((data = PyObject_Call(read_meth, read_args, NULL)) == NULL) { goto final; } /* Wykrywanie końca pliku */ if (PySequence_Length(data) == 0) { Py_DECREF(data); break; } /* Kodowanie znaków Unicode jako bajtów na potrzeby kodu w języku C */ if ((enc_data = PyUnicode_AsEncodedString(data, "utf-8", "strict")) == NULL) { Py_DECREF(data); goto final; } /* Pobieranie danych z bufora */ PyBytes_AsStringAndSize(enc_data, &buf, &len); /* Wyświetlanie w strumieniu stdout (do zastąpienia bardziej użytecznym kodem) */ write(1, buf, len); /* Operacje porządkujące */ Py_DECREF(enc_data); Py_DECREF(data); }
/* Consume a "file-like" object and write bytes to stdout */ static PyObject *py_consume_file(PyObject *self, PyObject *args) { PyObject *obj; PyObject *read_meth; PyObject *result = NULL; PyObject *read_args; if (!PyArg_ParseTuple(args,"O", &obj)) { return NULL; } /* Get the read method of the passed object */ if ((read_meth = PyObject_GetAttrString(obj, "read")) == NULL) { return NULL; } /* Build the argument list to read() */ read_args = Py_BuildValue("(i)", CHUNK_SIZE); while (1) { PyObject *data; PyObject *enc_data; char *buf; Py_ssize_t len; /* Call read() */ if ((data = PyObject_Call(read_meth, read_args, NULL)) == NULL) { goto final; } /* Check for EOF */ if (PySequence_Length(data) == 0) { Py_DECREF(data); break; } /* Encode Unicode as Bytes for C */ if ((enc_data = PyUnicode_AsEncodedString(data, "utf-8", "strict")) == NULL) { Py_DECREF(data); goto final; } /* Extract underlying buffer data */ PyBytes_AsStringAndSize(enc_data, &buf, &len); /* Write to stdout (replace with something more useful) */ write(1, buf, len); /* Cleanup */ Py_DECREF(enc_data); Py_DECREF(data); }
static PyObject *_prepareValue(t_set *self, PyObject *value) { PyObject *item = PyObject_Call(self->itemvalue.owner, Empty_TUPLE, NULL); if (!item) return NULL; value = PyObject_CallMethodObjArgs((PyObject *) self, prepareValue_NAME, item, self->itemvalue.attribute, value, Py_False, NULL); Py_DECREF(item); return value; }
PyObject* pyInfoTree::make_pyobject_from_c_ptr(InfoTree* tree, bool do_copy) { PyObject* keyworded_args = PyDict_New(); On_scope_exit { Py_XDECREF(keyworded_args); }; PyObject* empty_tuple = PyTuple_New(0); On_scope_exit { Py_XDECREF(empty_tuple); }; auto tree_capsule = PyCapsule_New(static_cast<void*>(tree), nullptr, nullptr); On_scope_exit { Py_XDECREF(tree_capsule); }; PyDict_SetItemString(keyworded_args, "infotree_c_ptr", tree_capsule); PyObject* copy = PyLong_FromLong(do_copy); On_scope_exit { Py_XDECREF(copy); }; PyDict_SetItemString(keyworded_args, "copy", copy); PyObject* obj = PyObject_Call((PyObject*)&pyInfoTree::pyType, empty_tuple, keyworded_args); return obj; }
static PyObject* _def_f_getsize(PyObject *self, PyObject *args, PyObject *kwds) { PyObject *retval, *method; method = PyObject_GetAttrString(self, "getsize"); if (!method) return NULL; retval = PyObject_Call(method, args, kwds); Py_DECREF (method); return retval; }
void modena_model_read_substituteModels(modena_model_t *m) { PyObject *pSubstituteModels = PyObject_GetAttrString ( m->pModel, "substituteModels" ); if(!pSubstituteModels){ Modena_PyErr_Print(); } PyObject *pSeq = PySequence_Fast ( pSubstituteModels, "expected a sequence" ); m->substituteModels_size = PySequence_Size(pSubstituteModels); m->substituteModels = malloc(m->substituteModels_size*sizeof(modena_substitute_model_t)); size_t i; for(i = 0; i < m->substituteModels_size; i++) { PyObject *args = PyTuple_New(0); PyObject *kw = Py_BuildValue ( "{s:O}", "model", PyList_GET_ITEM(pSeq, i) ); m->substituteModels[i].model = (modena_model_t *) PyObject_Call ( (PyObject *) &modena_model_tType, args, kw ); Py_DECREF(args); Py_DECREF(kw); if(!m->substituteModels[i].model){ Modena_PyErr_Print(); } m->substituteModels[i].inputs = modena_inputs_new ( m->substituteModels[i].model ); m->substituteModels[i].outputs = modena_outputs_new ( m->substituteModels[i].model ); modena_substitute_model_calculate_maps(&m->substituteModels[i], m); } Py_DECREF(pSeq); Py_DECREF(pSubstituteModels); if(PyErr_Occurred()){ Modena_PyErr_Print(); } }
void KX_LibLoadStatus::RunFinishCallback() { #ifdef WITH_PYTHON if (m_finish_cb) { PyObject* args = Py_BuildValue("(O)", GetProxy()); if (!PyObject_Call(m_finish_cb, args, NULL)) { PyErr_Print(); PyErr_Clear(); } Py_DECREF(args); } #endif }
static PyObject* selTournament(PyObject *self, PyObject *args, PyObject *kwargs){ /* Args[0] / kwArgs['individuals'] : Individual list * Args[1] / kwArgs['k'] : Number of individuals wanted in output * Args[2] / kwArgs['tournsize'] : Tournament size * Return : k selected individuals from input individual list */ PyObject *lListIndv; unsigned int k, lTournSize; static char *lKwlist[] = {"individuals", "k", "tournsize", NULL}; PyArg_ParseTupleAndKeywords(args, kwargs, "Oii", lKwlist, &lListIndv, &k, &lTournSize); // Import the Python random module PyObject *lRandomModule = PyImport_ImportModule("random"); PyObject *lRandomChoiceFunc = PyObject_GetAttrString(lRandomModule, "choice"); PyObject *lListSelect = PyList_New(0); PyObject *lCandidate, *lChallenger, *lCandidateFit, *lChallengerFit, *lTupleArgs; lTupleArgs = Py_BuildValue("(O)", lListIndv); for(unsigned int i=0; i < k; i++){ // We call random.choice with the population as argument lCandidate = PyObject_Call(lRandomChoiceFunc, lTupleArgs, NULL); lCandidateFit = PyObject_GetAttrString(lCandidate, "fitness"); for(unsigned int j=0; j < lTournSize-1; j++){ lChallenger = PyObject_Call(lRandomChoiceFunc, lTupleArgs, NULL); lChallengerFit = PyObject_GetAttrString(lChallenger, "fitness"); // Is the fitness of the aspirant greater? if(PyObject_RichCompareBool(lChallengerFit, lCandidateFit, Py_GT)){ lCandidate = lChallenger; lCandidateFit = lChallengerFit; } } PyList_Append(lListSelect, lCandidate); } return lListSelect; }
static PyObject * keyobject_richcompare(PyObject *ko, PyObject *other, int op) { PyObject *res; PyObject *args; PyObject *x; PyObject *y; PyObject *compare; PyObject *answer; static PyObject *zero; if (zero == NULL) { zero = PyLong_FromLong(0); if (!zero) return NULL; } if (Py_TYPE(other) != &keyobject_type){ PyErr_Format(PyExc_TypeError, "other argument must be K instance"); return NULL; } compare = ((keyobject *) ko)->cmp; assert(compare != NULL); x = ((keyobject *) ko)->object; y = ((keyobject *) other)->object; if (!x || !y){ PyErr_Format(PyExc_AttributeError, "object"); return NULL; } /* Call the user's comparison function and translate the 3-way * result into true or false (or error). */ args = PyTuple_New(2); if (args == NULL) return NULL; Py_INCREF(x); Py_INCREF(y); PyTuple_SET_ITEM(args, 0, x); PyTuple_SET_ITEM(args, 1, y); res = PyObject_Call(compare, args, NULL); Py_DECREF(args); if (res == NULL) return NULL; answer = PyObject_RichCompare(res, zero, op); Py_DECREF(res); return answer; }
static PyObject * obj_create(PyObject *self) { int added; PyObject * args = PyTuple_New(3); if (args == NULL) { return NULL; } PyObject * name = PyUnicode_FromString("Bill"); if (name == NULL) { Py_DECREF(args); return NULL; } added = PyTuple_SetItem(args, 0, name); // PyTuple_SetItem is an exception and it steals the reference, even if it fails. // We will not have to DECREF name anymore. if (added != 0) { Py_DECREF(args); return NULL; } PyObject * number = PyLong_FromLong(7); if (number == NULL) { Py_DECREF(args); return NULL; } added = PyTuple_SetItem(args, 1, number); // We will not have to DECREF number anymore. if (added != 0) { Py_DECREF(args); return NULL; } PyObject * yes = Py_True; Py_INCREF(yes); added = PyTuple_SetItem(args, 2, yes); // We will not have to DECREF yes anymore. if (added != 0) { Py_DECREF(args); return NULL; } PyObject * kwargs = NULL; PyObject * result = PyObject_Call((PyObject *) &NativeType, args, kwargs); Py_DECREF(args); if (result == NULL) { return NULL; } return result; }
double call_func(PyObject *func, double x, double y) { PyObject *args; PyObject *kwargs; PyObject *result = 0; double retval; /* Make sure we own the GIL */ PyGILState_STATE state = PyGILState_Ensure(); /* Verify that func is a proper callable */ if (!PyCallable_Check(func)) { fprintf(stderr,"call_func: expected a callable\n"); goto fail; } /* Build arguments */ args = Py_BuildValue("(dd)", x, y); kwargs = NULL; /* Call the function */ result = PyObject_Call(func, args, kwargs); Py_DECREF(args); Py_XDECREF(kwargs); /* Check for Python exceptions (if any) */ if (PyErr_Occurred()) { PyErr_Print(); goto fail; } /* Verify the result is a float object */ if (!PyFloat_Check(result)) { fprintf(stderr,"call_func: callable didn't return a float\n"); goto fail; } /* Create the return value */ retval = PyFloat_AsDouble(result); Py_DECREF(result); /* Restore previous GIL state and return */ PyGILState_Release(state); return retval; fail: Py_XDECREF(result); PyGILState_Release(state); abort(); }
static void atexit_callfuncs(void) { PyObject *exc_type = NULL, *exc_value, *exc_tb, *r; atexit_callback *cb; PyObject *module; atexitmodule_state *modstate; int i; module = PyState_FindModule(&atexitmodule); if (module == NULL) return; modstate = GET_ATEXIT_STATE(module); if (modstate->ncallbacks == 0) return; for (i = modstate->ncallbacks - 1; i >= 0; i--) { cb = modstate->atexit_callbacks[i]; if (cb == NULL) continue; r = PyObject_Call(cb->func, cb->args, cb->kwargs); Py_XDECREF(r); if (r == NULL) { /* Maintain the last exception, but don't leak if there are multiple exceptions. */ if (exc_type) { Py_DECREF(exc_type); Py_XDECREF(exc_value); Py_XDECREF(exc_tb); } PyErr_Fetch(&exc_type, &exc_value, &exc_tb); if (!PyErr_ExceptionMatches(PyExc_SystemExit)) { PySys_WriteStderr("Error in atexit._run_exitfuncs:\n"); PyErr_NormalizeException(&exc_type, &exc_value, &exc_tb); PyErr_Display(exc_type, exc_value, exc_tb); } } } atexit_cleanup(modstate); if (exc_type) PyErr_Restore(exc_type, exc_value, exc_tb); }
static PyObject * Session_browse_artist(Session * self, PyObject *args, PyObject *kwds) { PyObject *artist, *callback, *userdata = NULL; static char *kwlist[] = { "artist", "callback", "userdata", NULL }; if (!PyArg_ParseTupleAndKeywords (args, kwds, "O!O|O", kwlist, &ArtistType, &artist, &callback, &userdata)) return NULL; args = PyTuple_NewByPreappending((PyObject *)self, args); PyObject *result = PyObject_Call((PyObject *)&ArtistBrowserType, args, kwds); Py_XDECREF(args); return result; }
PyObject* DCPointFromObject(DKPoint* point) { if (point) { PyObject* args = Py_BuildValue("ff", point->x, point->y); PyObject* kwds = PyDict_New(); PyObject* tp = (PyObject*)DCObjectDefaultClass(&objectType); PyObject* self = PyObject_Call(tp, args, kwds); Py_XDECREF(tp); Py_XDECREF(args); Py_XDECREF(kwds); return self; } Py_RETURN_NONE; }
static PyObject * partial_call_impl(partialobject *pto, PyObject *args, PyObject *kwargs) { PyObject *ret, *args2; /* Note: tupleconcat() is optimized for empty tuples */ args2 = PySequence_Concat(pto->args, args); if (args2 == NULL) { return NULL; } assert(PyTuple_Check(args2)); ret = PyObject_Call(pto->fn, args2, kwargs); Py_DECREF(args2); return ret; }
/* Return result of sleeper_agent._get_state_info() as a C string */ char * sleeper_agent_state(void) { char *rv = NULL; PyGILState_STATE gstate; gstate = PyGILState_Ensure(); rv = PyString_AsString( PyObject_Call( PyObject_GetAttrString( PyImport_ImportModule("sleeper_agent"), "_get_state_info"), Py_BuildValue("()"), NULL)); PyGILState_Release(gstate); return rv; }
static PyObject * gi_gst_fraction_from_value (const GValue * value) { PyObject *fraction_type, *args, *fraction; gint numerator, denominator; numerator = gst_value_get_fraction_numerator (value); denominator = gst_value_get_fraction_denominator (value); fraction_type = gi_gst_get_type ("Fraction"); args = Py_BuildValue ("(ii)", numerator, denominator); fraction = PyObject_Call (fraction_type, args, NULL); Py_DECREF (args); return fraction; }
static void gevent_stop(PyObject* watcher, struct PyGeventLoopObject* loop) { PyObject *result, *method; int error; error = 1; method = PyObject_GetAttrString(watcher, "stop"); if (method) { result = PyObject_Call(method, __pyx_empty_tuple, NULL); if (result) { Py_DECREF(result); error = 0; } Py_DECREF(method); } if (error) { gevent_handle_error(loop, watcher); } }
static int ec_init(PyObject *self, PyObject *args, PyObject *kw) { PyObject *r, *__init__; __init__ = PyObject_GetAttr(self, str__init__); if (__init__ == NULL) return -1; r = PyObject_Call(__init__, args, kw); Py_DECREF(__init__); if (r == NULL) return -1; Py_DECREF(r); return 0; }
static PyObject * partial_call(partialobject *pto, PyObject *args, PyObject *kw) { PyObject *ret; PyObject *argappl, *kwappl; assert (PyCallable_Check(pto->fn)); assert (PyTuple_Check(pto->args)); assert (PyDict_Check(pto->kw)); if (PyTuple_GET_SIZE(pto->args) == 0) { argappl = args; Py_INCREF(args); } else if (PyTuple_GET_SIZE(args) == 0) { argappl = pto->args; Py_INCREF(pto->args); } else { argappl = PySequence_Concat(pto->args, args); if (argappl == NULL) return NULL; assert(PyTuple_Check(argappl)); } if (PyDict_Size(pto->kw) == 0) { kwappl = kw; Py_XINCREF(kwappl); } else { kwappl = PyDict_Copy(pto->kw); if (kwappl == NULL) { Py_DECREF(argappl); return NULL; } if (kw != NULL) { if (PyDict_Merge(kwappl, kw, 1) != 0) { Py_DECREF(argappl); Py_DECREF(kwappl); return NULL; } } } ret = PyObject_Call(pto->fn, argappl, kwappl); Py_DECREF(argappl); Py_XDECREF(kwappl); return ret; }
/* the actual callback - not necessarily called from py */ void bpy_app_generic_callback(struct Main *UNUSED(main), struct ID *id, void *arg) { PyObject *cb_list = py_cb_array[GET_INT_FROM_POINTER(arg)]; if (PyList_GET_SIZE(cb_list) > 0) { PyGILState_STATE gilstate = PyGILState_Ensure(); PyObject *args = PyTuple_New(1); /* save python creating each call */ PyObject *func; PyObject *ret; Py_ssize_t pos; /* setup arguments */ if (id) { PointerRNA id_ptr; RNA_id_pointer_create(id, &id_ptr); PyTuple_SET_ITEM(args, 0, pyrna_struct_CreatePyObject(&id_ptr)); } else { PyTuple_SET_ITEM(args, 0, Py_INCREF_RET(Py_None)); } /* Iterate the list and run the callbacks * note: don't store the list size since the scripts may remove themselves */ for (pos = 0; pos < PyList_GET_SIZE(cb_list); pos++) { func = PyList_GET_ITEM(cb_list, pos); ret = PyObject_Call(func, args, NULL); if (ret == NULL) { /* Don't set last system variables because they might cause some * dangling pointers to external render engines (when exception * happens during rendering) which will break logic of render pipeline * which expects to be the only user of render engine when rendering * is finished. */ PyErr_PrintEx(0); PyErr_Clear(); } else { Py_DECREF(ret); } } Py_DECREF(args); PyGILState_Release(gilstate); } }
PyObject * PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path) { PyObject *args, *kwargs, *error; if (msg == NULL) return NULL; args = PyTuple_New(1); if (args == NULL) return NULL; kwargs = PyDict_New(); if (kwargs == NULL) { Py_DECREF(args); return NULL; } if (name == NULL) { name = Py_None; } if (path == NULL) { path = Py_None; } Py_INCREF(msg); PyTuple_SET_ITEM(args, 0, msg); if (PyDict_SetItemString(kwargs, "name", name) < 0) return NULL; if (PyDict_SetItemString(kwargs, "path", path) < 0) return NULL; error = PyObject_Call(PyExc_ImportError, args, kwargs); if (error != NULL) { PyErr_SetObject((PyObject *)Py_TYPE(error), error); Py_DECREF(error); } Py_DECREF(args); Py_DECREF(kwargs); return NULL; }
PyObject* XPolygon_next(PyObject *obj) { XPolygon *self = (XPolygon*)obj; if (self->list_iter != self->line_ring.point_list.end()) { XCoordinate *coordobj = (XCoordinate*)PyObject_Call((PyObject*)&XCoordinate_typeobj, NULL, NULL); coordobj->point.x = self->list_iter->x; coordobj->point.y = self->list_iter->y; ++self->list_iter; return (PyObject*)coordobj; } else { return NULL; } }
static void explain_issue(PyObject *dispatcher, PyObject *args, PyObject *kws, const char *method_name, const char *default_msg) { PyObject *callback, *result; callback = PyObject_GetAttrString(dispatcher, method_name); if (!callback) { PyErr_SetString(PyExc_TypeError, default_msg); return; } result = PyObject_Call(callback, args, kws); Py_DECREF(callback); if (result != NULL) { PyErr_Format(PyExc_RuntimeError, "%s must raise an exception", method_name); Py_DECREF(result); } }
PythonObject callFunctionWithArgs( PythonObject function, const std::vector<PythonObject>& args, const std::vector<std::pair<std::string, PythonObject>>& kwargs) { if (!PyCallable_Check(function.get())) { throw WrappyError("Wrappy: Supplied object isn't callable."); } // Build tuple size_t sz = args.size(); PythonObject tuple(PythonObject::owning {}, PyTuple_New(sz)); if (!tuple) { PyErr_Print(); throw WrappyError("Wrappy: Couldn't create python typle."); } for (size_t i = 0; i < sz; ++i) { PyObject* arg = args.at(i).get(); Py_XINCREF(arg); // PyTuple_SetItem steals a reference PyTuple_SetItem(tuple.get(), i, arg); } // Build kwargs dict PythonObject dict(PythonObject::owning {}, PyDict_New()); if (!dict) { PyErr_Print(); throw WrappyError("Wrappy: Couldn't create python dictionary."); } for (const auto& kv : kwargs) { PyDict_SetItemString(dict.get(), kv.first.c_str(), kv.second.get()); } PythonObject res(PythonObject::owning{}, PyObject_Call(function.get(), tuple.get(), dict.get())); if (!res) { PyErr_Print(); throw WrappyError("Wrappy: Error calling function"); } return res; }
struct srd_decoder_instance *srd_instance_new(const char *id) { struct srd_decoder *dec; struct srd_decoder_instance *di; PyObject *py_args, *py_value; if (!(dec = srd_get_decoder_by_id(id))) return NULL; /* TODO: Error handling. Use g_try_malloc(). */ di = g_malloc(sizeof(*di)); /* Create an empty Python tuple. */ if (!(py_args = PyTuple_New(0))) { /* NEWREF */ if (PyErr_Occurred()) PyErr_Print(); /* Returns void. */ return NULL; /* TODO: More specific error? */ } /* * FIXME: Pass in a unitsize that matches the selected LA. * FIXME: Fill 'starttime' with something reasonable. */ py_value = Py_BuildValue("{sssisd}", "driver", "demo", "unitsize", _unitsize, "starttime", 129318231823.0); /* Create an instance of the 'Decoder' class. */ di->py_instance = PyObject_Call(dec->py_decobj, py_args, py_value); if (!di->py_instance) { if (PyErr_Occurred()) PyErr_Print(); /* Returns void. */ Py_XDECREF(py_args); Py_XDECREF(py_value); /* TODO: Ref. stolen upon error? */ return NULL; /* TODO: More specific error? */ } Py_XDECREF(py_args); Py_XDECREF(py_value); return di; }