python-c-api

Passing python objects as arguments to C/C++ function using ctypes

筅森魡賤 提交于 2019-11-28 11:38:05
问题 I have a dll with a function that takes PyObject as argument something like void MyFunction(PyObject* obj) { PyObject *func, *res, *test; //function getAddress of python object func = PyObject_GetAttrString(obj, "getAddress"); res = PyObject_CallFunction(func, NULL); cout << "Address: " << PyString_AsString( PyObject_Str(res) ) << endl; } and I want to call this function in the dll from python using ctypes My python code looks like import ctypes as c path = "h:\libTest" libTest = c.cdll

Get Python's LIB path

折月煮酒 提交于 2019-11-28 08:15:58
问题 I can see that INCLUDE path is sysconfig.get_path('include'). But I don't see any similar value for LIB . NumPy outright hardcodes it as os.path.join(sys.prefix, "libs") in Windows and get_config_var('LIBDIR') (not documented and missing in Windows) otherwise. Is there a more supported way? 回答1: Since it's not a part of any official spec/doc, and, as shown by another answer, there are cases when none of appropriate variables from sysconfig / distutils.sysconfig .get_config_var() are set, the

How to pass an array from C to an embedded python script

亡梦爱人 提交于 2019-11-28 06:05:00
I am running to some problems and would like some help. I have a piece code, which is used to embed a python script. This python script contains a function which will expect to receive an array as an argument (in this case I am using numpy array within the python script). I would like to know how can I pass an array from C to the embedded python script as an argument for the function within the script. More specifically can someone show me a simple example of this. Really, the best answer here is probably to use numpy arrays exclusively, even from your C code. But if that's not possible, then

Global Interpreter Lock and access to data (eg. for NumPy arrays)

可紊 提交于 2019-11-27 16:39:42
问题 I am writing a C extension for Python, which should release the Global Interpreter Lock while it operates on data. I think I have understood the mechanism of the GIL fairly well, but one question remains: Can I access data in a Python object while the thread does not own the GIL? For example, I want to read data from a (big) NumPy array in the C function while I still want to allow other threads to do other things on the other CPU cores. The C function should release the GIL with Py_BEGIN

C Python: Running Python code within a context

我的未来我决定 提交于 2019-11-27 13:52:50
问题 The Python C API function PyEval_EvalCode let's you execute compiled Python code. I want to execute a block of Python code as if it were executing within the scope of a function , so that it has its own dictionary of local variables which don't affect the global state. This seems easy enough to do, since PyEval_EvalCode lets you provide a Global and Local dictionary: PyObject* PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals) The problem I run into has to do with how

Py_INCREF/DECREF: When

China☆狼群 提交于 2019-11-27 09:29:02
问题 Is one correct in stating the following: If a Python object is created in a C function, but the function doesn't return it, no INCREF is needed, but a DECREF is. [false]If the function does return it, you do need to INCREF , in the function that receives the return value.[/false] When assigning C typed variables as attributes, like double , int etc., to the Python object, no INCREF or DECREF is needed. Assigning Python objects as attributes to your other Python objects goes like this:

How to create a generator/iterator with the Python C API?

▼魔方 西西 提交于 2019-11-27 09:21:51
问题 How do I replicate the following Python code with the Python C API? class Sequence(): def __init__(self, max): self.max = max def data(self): i = 0 while i < self.max: yield i i += 1 So far, I have this: #include <Python/Python.h> #include <Python/structmember.h> /* Define a new object class, Sequence. */ typedef struct { PyObject_HEAD size_t max; } SequenceObject; /* Instance variables */ static PyMemberDef Sequence_members[] = { {"max", T_UINT, offsetof(SequenceObject, max), 0, NULL}, {NULL

Create an object using Python's C API

一曲冷凌霜 提交于 2019-11-27 07:04:35
Say I have my object layout defined as: typedef struct { PyObject_HEAD // Other stuff... } pyfoo; ...and my type definition: static PyTypeObject pyfoo_T = { PyObject_HEAD_INIT(NULL) // ... pyfoo_new, }; How do I create a new instance of pyfoo somewhere within my C extension? Call PyObject_New() , followed by PyObject_Init() . EDIT: The best way is to call the class object, just like in Python itself: /* Pass two arguments, a string and an int. */ PyObject *argList = Py_BuildValue("si", "hello", 42); /* Call the class object. */ PyObject *obj = PyObject_CallObject((PyObject *) &pyfoo_T, argList

Define a global in a Python module from a C API

倖福魔咒の 提交于 2019-11-27 06:29:46
问题 I am developing a module for Python using a C API. How can I create a variable that is seen as global from Python? For example, if my module is module , I want to create a variable g that does this job: import module print module.g In particular, g is an integer. Solution from Alex Martelli PyObject *m = Py_InitModule("mymodule", mymoduleMethods); PyObject *v = PyLong_FromLong((long) 23); PyObject_SetAttrString(m, "g", v); Py_DECREF(v); 回答1: You can use PyObject_SetAttrString in your module's

Python for .NET “unable to find assembly” error

瘦欲@ 提交于 2019-11-27 04:41:42
问题 I'm using CPython and I have a C# dll. I'm trying to use Python for .NET to make them talk. I can't use IronPython because I need to integrate this into an existing CPython system. I'm completely new to Python for .NET, and I actually have very little experience with Python and no experience with C#. So please forgive me if my question seems very basic. I'm using Python 2.7.3, and I downloaded pythonnet-2.0-alpha2-clr2.0_131_py27_UCS2 and unzipped it into a folder named pyfornet_test, which