python-c-api

Embedded Python 2.7.2 Importing a module from a user-defined directory

一个人想着一个人 提交于 2019-12-19 01:19:06
问题 I'm embedding Python into a C/C++ application that will have a defined API. The application needs to instantiate classes defined in a script, which are structured roughly like this: class userscript1: def __init__(self): ##do something here... def method1(self): ## method that can be called by the C/C++ app...etc I've managed in the past (for the proof-of-concept) to get this done using the following type of code: PyObject* pName = PyString_FromString("userscript.py"); PyObject* pModule =

(Python C API) PyRun_StringFlags missing builtin functions?

我们两清 提交于 2019-12-18 20:08:45
问题 I am trying to embed some python in my pet project. I have reduced my problem to the following code: #include <Python.h> #include "iostream" int main(int argc, char *argv[]) { Py_Initialize(); PyObject *globals = Py_BuildValue("{}"); PyObject *locals = Py_BuildValue("{}"); PyObject *string_result = PyRun_StringFlags( "a=5\n" "s='hello'\n" "d=dict()\n" , Py_file_input, globals, locals, NULL); if ( PyErr_Occurred() ) {PyErr_Print();PyErr_Clear();return 1;} return 0; } (I know I'm not cleaning

Python C-API Object Allocation

半世苍凉 提交于 2019-12-18 16:31:41
问题 I want to use the new and delete operators for creating and destroying my objects. The problem is python seems to break it into several stages. tp_new, tp_init and tp_alloc for creation and tp_del, tp_free and tp_dealloc for destruction. However c++ just has new which allocates and fully constructs the object and delete which destructs and deallocates the object. Which of the python tp_* methods do I need to provide and what must they do? Also I want to be able to create the object directly

Python C-API Object Allocation

喜夏-厌秋 提交于 2019-12-18 16:30:51
问题 I want to use the new and delete operators for creating and destroying my objects. The problem is python seems to break it into several stages. tp_new, tp_init and tp_alloc for creation and tp_del, tp_free and tp_dealloc for destruction. However c++ just has new which allocates and fully constructs the object and delete which destructs and deallocates the object. Which of the python tp_* methods do I need to provide and what must they do? Also I want to be able to create the object directly

Python C-API functions that borrow and steal references

青春壹個敷衍的年華 提交于 2019-12-18 13:10:34
问题 The standard convention in the Python C-API is that functions do not steal references from input arguments (that are objects) return values and output arguments (that are objects) own a reference Most functions in the Python C-API follow this convention. However, there are some exceptions. I have come across the following: Functions that steal a reference from an input argument PyModule_AddObject Functions with return values or output arguments that borrow a reference PyErr_Occurred PyTuple

How to return an C array to Python?

…衆ロ難τιáo~ 提交于 2019-12-18 09:10:55
问题 I wrote a Python/C extension function that was called by Python, How can return an 2d array int[][] to Python? static PyObject* inference_function(PyObject *self, PyObject *args) { PyObject* doc_lst; int K,V; double alpha,beta; int n_iter; if (!PyArg_ParseTuple(args, "Oiiddi", &doc_lst, &K,&V, &alpha,&beta,&n_iter)) { printf("传入参数错误!\n"); return NULL; } return Py_BuildValue("i", 1); } 回答1: What kind of array are you using? One way, which I find convenient, is to use numpy arrays, and modify

Numpy C-Api example gives a SegFault

丶灬走出姿态 提交于 2019-12-18 04:12:37
问题 I'm trying to understand how the Python C- Api works, and I want to exchange numpy arrays between Python and a C Extension. So, I started this tutorial: http://dsnra.jpl.nasa.gov/software/Python/numpydoc/numpy-13.html Tried to do the first example there, a C module that calculates the trace of a 2d numpy array, was very neat for me, since I want to do elementary operations in 2d arrays also. #include <Python.h> #include "Numeric/arrayobject.h" #include<stdio.h> int main(){ Py_Initialize();

Allowing Ctrl-C to interrupt a python C-extension

隐身守侯 提交于 2019-12-18 04:07:46
问题 I'm running some computationally heavy simulation in (home-made) C-based python extensions. Occasionally I get stuff wrong and would like to terminate a simulation. However, Ctrl-C doesn't seem to have any effect (other than printing ^C to the screen) so I have to kill the process using kill or the system monitor. As far as I can see python just waits for the C extension to finish and doesn't really communicate with it during this time. Is there a way to make this work? Update : The main

Multithreading with Python and C api

夙愿已清 提交于 2019-12-18 02:47:53
问题 I have a C++ program that uses the C api to use a Python library of mine. Both the Python library AND the C++ code are multithreaded. In particular, one thread of the C++ program instantiates a Python object that inherits from threading.Thread . I need all my C++ threads to be able to call methods on that object. From my very first tries (I naively just instantiate the object from the main thread, then wait some time, then call the method) I noticed that the execution of the Python thread

How To catch python stdout in c++ code

ⅰ亾dé卋堺 提交于 2019-12-17 07:12:07
问题 This question was migrated from Server Fault because it can be answered on Stack Overflow. Migrated 9 years ago . I have a program which during it's run sometimes needs to call python in order to preform some tasks. I need a function that calls python and catches pythons stdout and puts it in some file. This is a declaration of the function pythonCallBackFunc(const char* pythonInput) My problem is to catch all the python output for a given command (pythonInput). I have no experience with