I'm writing an extension module in C in which I'm passing an array of
floats from C to python. The code below illustrates a simple C
function designed to output an array of floats.
---------
extTest.c
---------
#include <stdio.h>
double *testArray(int nsamp) {
double nums[10000];
int i;
double cumdata = 0.0;
printf("%d\n", nsamp);
for (i=0; i<=nsamp; i++) {
printf("%d\n", i);
nums[i] = cumdata;
cumdata += 0.5;
printf("%f\n", nums[i]);
}
return nums;
}
Then I write a wrapper function to pass the data back and forth between
C and Python.
----------------
extTestWrapper.c
----------------
#include "/usr/include/python2.4/Python.h"
#include <stdio.h>
// external declarations
extern float *testArray(int);
// Python wrapper for the testArray function
PyObject *extTest_testArray(PyObject *self, PyObject *args) {
double *nums;
int nsamp;
int i;
PyObject *pynums;
if (!PyArg_ParseTuple(args, "i", &nsamp)) {
return NULL;
}
// call the C function
nums = testArray(nsamp);
// build a Python list object containing the array values
pynums = PyList_New(nsamp);
for (i=0; i<=nsamp; i++){
PyList_SetItem(pynums, i, PyFloat_FromDouble(nums[i]));
}
return Py_BuildValue("O", pynums);
}
// method table mapping names to wrappers
static PyMethodDef extTestMethods [] = {
{"testArray", extTest_testArray, METH_VARARGS},
{NULL, NULL}
};
//module init function
void initextTest() {
Py_InitModule("extTest", extTestMethods);
}
I then run the following setup.py script using python setup.py install
--install-lib=.
--------------------------------------------------------------------------------------------
# setup.py for extTest
from distutils.core import setup, Extension
setup(name="extTest", version="0.0.1",
ext_modules=[Extension("extTest", ["extTest.c", "extTestWrapper.c"])])
--------------------------------------------------------------------------------------------
The library builds and installs ok. When I invoke the testArray
function, it appears to work correctly (the output is as expected).
For example,
import extTest
a = extTest.testArray(5)
yields the following output:
5
0
0.000000
1
0.500000
2
1.000000
3
1.500000
4
2.000000
5
2.500000
Exception exceptions.IndexError: 'list assignment index out of range'
in 'garbage collection' ignored
Fatal Python error: unexpected exception during garbage collection
Aborted
Here is where I'm stumped. I must be doing something wrong during the
PyList_SetItem or the Py_BuildValue.
Any ideas on fixing this problem ?
Regards,
Rimmer 3 1348
On 25/05/2006 12:09 PM, rimmer wrote: I'm writing an extension module in C in which I'm passing an array of floats from C to python. The code below illustrates a simple C function designed to output an array of floats.
--------- extTest.c --------- #include <stdio.h>
double *testArray(int nsamp) {
double nums[10000]; int i; double cumdata = 0.0;
printf("%d\n", nsamp); for (i=0; i<=nsamp; i++) { printf("%d\n", i); nums[i] = cumdata; cumdata += 0.5; printf("%f\n", nums[i]); } return nums;
Your problem is right here. The array nums is local to the function.
You are returning a pointer to memory whose contents are utterly useless
once you return from the function. Depending on the architecture and the
compiler, the pointer may point outside the stack, maybe causing the
hardware to take exception when the pointer is dereferenced, or it may
be inside the stack, in which case the next few function calls are
liable to trash the contents.
}
Then I write a wrapper function to pass the data back and forth between C and Python.
Before you do that, test it with a simple C main()!!!
[snip]
Here is where I'm stumped. I must be doing something wrong during the PyList_SetItem or the Py_BuildValue.
Yes, you may have problems there too, but I didn't bother reading that
far :-) Any ideas on fixing this problem ?
Regards,
Rimmer
On 25/05/2006 12:09 PM, rimmer wrote: I'm writing an extension module in C in which I'm passing an array of floats from C to python. The code below illustrates a simple C function designed to output an array of floats.
[snip]
Couldn't restrain myself from reading further :-) Then I write a wrapper function to pass the data back and forth between C and Python.
---------------- extTestWrapper.c ----------------
#include "/usr/include/python2.4/Python.h" #include <stdio.h>
// external declarations extern float *testArray(int);
Um, shouldn't that be "double", not "float"? // Python wrapper for the testArray function PyObject *extTest_testArray(PyObject *self, PyObject *args) {
double *nums; int nsamp; int i; PyObject *pynums;
if (!PyArg_ParseTuple(args, "i", &nsamp)) { return NULL; }
// call the C function nums = testArray(nsamp);
// build a Python list object containing the array values pynums = PyList_New(nsamp);
Test for errors!
for (i=0; i<=nsamp; i++){
Um, shouldn't that be "<", not "<="???
Note, you have the same problem in the C function.
"nsamp" is presumed in the absence of any docs to mean "number of
samples". A caller passing in 5 expects to get 5 values, NOT 6.
But you are calling PyList_New with 5.
PyList_SetItem(pynums, i, PyFloat_FromDouble(nums[i]));
Given you are trying to stuff one extra item into the list, you should
definitely test for errors here!!!
I suggest that you change this incrementally. First, just change the
above line to test for errors. Then run it again so you can see what
happens. Second, fix the other problems.
} return Py_BuildValue("O", pynums);
Rather unnecessary; you can just return pynums.
[read before snipping :-)]
HTH,
John
Thanks for the help John.
Indeed, changing <= to < has it licked. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Torsten Mohr |
last post by:
Hi,
i write an extension module in C at the moment. This
module does some work on some own data types that
consist of some values.
The...
|
by: Mark English |
last post by:
Basic problem:
If there is a C-extension module in a package and it tries to import
another python module in the same package without using the...
|
by: Sean Richards |
last post by:
Python 2.3.4 (#1, May 29 2004, 17:05:23)
on linux2
Getting some strange behaviour with keyword arguments for optional
arguments in extension...
|
by: Gerhard Esterhuizen |
last post by:
Hi,
I am observing unexpected behaviour, in the form of a corrupted class
member access, from a simple C++ program that accesses an attribute...
|
by: conan |
last post by:
This regexp
'<widget class=".*" id=".*">'
works well with 'grep' for matching lines of the kind
<widget class="GtkWindow" id="window1">
on a...
|
by: Andrew McLean |
last post by:
I have a bunch of csv files that have the following characteristics:
- field delimiter is a comma
- all fields quoted with double quotes
- lines...
|
by: llothar |
last post by:
On windows everything is '.pyd' but there seems to be two ways to get
this on unix?
Why and what is the rule?
|
by: Ivan Velev |
last post by:
Hello,
Minimal example below - it gives me different output if I comment /
uncomment the extra time.mktime call - note that this call is not...
|
by: nisp |
last post by:
Hi all !
I'm trying to capture stderr of an external module I use in my python
program. I'm doing this
by setting up a class in my module...
|
by: better678 |
last post by:
Question:
Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct?
Answer:
Java is an object-oriented...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
by: jalbright99669 |
last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
| |