python-c-api

Passing a C pointer around with the Python/C API

百般思念 提交于 2019-12-04 00:18:52
问题 I'm new to the Python/C API ... I'm trying to add new functionality to my C program, wherein I can embed python into it and simultaneously extend functionality so that the embedded interpreter can execute a script that will interact with an extending python module written as part of my C program. My C program doesn't have global variables. I would like to keep things this way; At the same time in order to expose C functionality to python, it appears the extending C function at least needs

Suggestions on how to speed up a distance calculation

 ̄綄美尐妖づ 提交于 2019-12-03 19:36:45
问题 Consider the following class: class SquareErrorDistance(object): def __init__(self, dataSample): variance = var(list(dataSample)) if variance == 0: self._norm = 1.0 else: self._norm = 1.0 / (2 * variance) def __call__(self, u, v): # u and v are floats return (u - v) ** 2 * self._norm I use it to calculate the distance between two elements of a vector. I basically create one instance of that class for every dimension of the vector that uses this distance measure (there are dimensions that use

Python C API How to pass array of structs from C to Python

允我心安 提交于 2019-12-03 17:30:57
问题 For a python module I'm creating, I want to pass to the python user an array of structs like this: struct tcpstat { inet_prefix local; inet_prefix remote; int lport; int rport; int state; int rq, wq; int timer; int timeout; int retrs; unsigned ino; int probes; unsigned uid; int refcnt; unsigned long long sk; int rto, ato, qack, cwnd, ssthresh; }; I thought that Py_BuildValues was the function I was looking for. But seems like it isn't. Looking in the Python documentation I found the Buffer

Does the Python 3 interpreter leak memory when embedded?

眉间皱痕 提交于 2019-12-03 16:55:31
This bug report states that the Python interpreter, as of June 2007, will not clean up all allocated memory after calling Py_Finalize in a C/C++ application with an embedded Python interpreter. It was recommended to call Py_Finalize once at application termination. This bug report states that as of version 3.3 and March 2011 the interpreter still leaks memory. Does anyone know the current state of this issue? I am concerned because I have an application in which the interpreter is called multiple times per running instance and I am experiencing memory leaks. I am already using boost::python to

Nested Python C Extensions/Modules?

天涯浪子 提交于 2019-12-03 14:12:53
How do I compile a C-Python module such that it is local to another? E.g. if I have a module named "bar" and another module named "mymodule", how do I compile "bar" so that it imported via "import mymodule.bar"? (Sorry if this is poorly phrased, I wasn't sure what the proper term for it was.) I tried the following in setup.py, but it doesn't seem to work: from distutils.core import setup, Extension setup(name='mymodule', version='1.0', author='Me', ext_modules=[Extension('mymodule', ['mymodule-module.c']), Extension('bar', ['bar-module.c'])]) Edit Thanks Alex. So this is what I ended up using:

Returning objects to Python from C

廉价感情. 提交于 2019-12-03 14:02:05
I've read the documentation for the Python C-API, and even written a few extension modules. However, I'm still a bit unclear on the exact semantics when it comes to returning Python objects from a C function. The limited examples in the Python docs usually show a C function which returns the result of Py_BuildValue . Now, Py_BuildValue returns a New Reference , and transfers ownership of this reference over to the interpreter. So, can I extrapolate from this that it is a general rule that any object returned to Python must be a new reference , and that returning an object from a C function is

PyObject_CallMethod with keyword arguments

半城伤御伤魂 提交于 2019-12-03 13:46:06
I'm trying to embed a Python (2.7) library in my C application and I'm using the Python/C API to call Python code from C. I need to call a Python method that takes keyword arguments. Semantically, I'm trying to achieve the equivalent of the following line in Python: myobject.dosomething('blahdy blah', somearg=True) By reading the documentation , I have managed to get as far as the following, but this doesn't pass in the keyword arguments: PyObject_CallMethod(myobject, "dosomething", "s", "blahdy blah"); I'm not super familiar with Python and I'm kind of stuck at this point as the documentation

In Python, why is a module implemented in C faster than a pure Python module, and how do I write one?

喜你入骨 提交于 2019-12-03 12:19:05
The python documentation states, that the reason cPickle is faster than Pickle is, that the former is implemented in C. What does that mean exactly? I am making a module for advanced mathematics in Python, and some calculations take a significant amount of time. Does that mean that if my program is implemented in C it can be made much faster? I wish to import this module from other Python programs, just the way I can import cPickle. Can you explain how to do implement a Python module in C? you can write fast C code and then use it in your python scripts, so your program will run faster. [1]

Embedding Python into C - importing modules

孤街醉人 提交于 2019-12-03 08:16:44
I am having problems using the Embedded Python for C as per the Documentation - Whenever I try using imported modules I get an : Unhandled exception at 0x1e089e85 in PythonIncl.exe: 0xC0000005: Access violation reading location 0x00000004. The error occurs in the PyObject_GetAttrString() method and the documentation isn't much help. I have also tried using tutorials as in an Example from IBM, but always get the same access violation. The following is the example code from one of the tutorials which I can't seem to get to work, what is wrong here? C-Code (in one main file): #include <Python.h>

Passing arguments to tp_new and tp_init from subtypes in Python C API

天涯浪子 提交于 2019-12-03 06:26:19
I originally asked this question on the Python capi-sig list: How to pass arguments to tp_new and tp_init from subtypes? I'm reading the Python PEP-253 on subtyping and there are plenty of good recommendations on how to structure the types, call tp_new and tp_init slots, etc. But, it lacks an important note on passing arguments from sub to super type. It seems the PEP-253 is unfinished as per the note: (XXX There should be a paragraph or two about argument passing here.) So, I'm trying to extrapolate some strategies well known from the Python classes subtyping , especially techniques that each