本文整理汇总了C++中PyObject_Call函数的典型用法代码示例。如果您正苦于以下问题:C++ PyObject_Call函数的具体用法?C++ PyObject_Call怎么用?C++ PyObject_Call使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了PyObject_Call函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: _call_dialect
/*
* 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;
}
示例2: Node_traverse
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;
}
示例3: gcc_python_finish_invoking_callback
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;
}
示例4: csv_reader
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;
}
示例5: Py_BuildValue
/* 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);
}
示例6: Py_BuildValue
/* 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);
}
示例7: PyObject_Call
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;
}
示例8: PyDict_New
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;
}
示例9: _def_f_getsize
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;
}
示例10: modena_model_read_substituteModels
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(); }
}
示例11: Py_BuildValue
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
}
示例12: selTournament
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;
}
示例13: keyobject_richcompare
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;
}
示例14: obj_create
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;
}
示例15: call_func
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();
}