473,842 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unexpected extension module behaviour

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_testAr ray(PyObject *self, PyObject *args) {

double *nums;
int nsamp;
int i;
PyObject *pynums;

if (!PyArg_ParseTu ple(args, "i", &nsamp)) {
return NULL;
}

// call the C function
nums = testArray(nsamp );

// build a Python list object containing the array values
pynums = PyList_New(nsam p);
for (i=0; i<=nsamp; i++){
PyList_SetItem( pynums, i, PyFloat_FromDou ble(nums[i]));
}
return Py_BuildValue(" O", pynums);
}

// method table mapping names to wrappers
static PyMethodDef extTestMethods [] = {
{"testArray" , extTest_testArr ay, 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="ext Test", version="0.0.1" ,
ext_modules=[Extension("extT est", ["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.testArr ay(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.Inde xError: '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

May 25 '06 #1
3 1447
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

May 25 '06 #2
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_testAr ray(PyObject *self, PyObject *args) {

double *nums;
int nsamp;
int i;
PyObject *pynums;

if (!PyArg_ParseTu ple(args, "i", &nsamp)) {
return NULL;
}

// call the C function
nums = testArray(nsamp );

// build a Python list object containing the array values
pynums = PyList_New(nsam p);
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_FromDou ble(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
May 25 '06 #3
Thanks for the help John.

Indeed, changing <= to < has it licked.

May 25 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2765
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 functions that can change the data are written in C. The question came up if this is by itself thread safe, if some two or more threads try to change these data types, are the C functions by themselves are "atomic" or can they
0
1379
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 fully qualified path, the import fails. Config: Python 2.4 on Windows 2000 For example: mypackage contains:
0
1030
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 modules. See the simple test case below --------8<-------------------------------------------------- #include "Python.h" static PyObject *
2
2173
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 declared in a virtual base class via a chain of virtual method calls. To further complicate (or perhaps simplify) matters, some compilers (GCC and MingW) produce the expected behaviour, while others (MSVS 7.1) do not. I can only offer two explanations for my observations: 1. The Microsoft...
4
2757
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 XML .glade file However that's not true for the re module in python, since this one takes the regexp as if were specified this way: '^<widget class=".*"
7
2791
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 terminated by a *space* followed by a newline What surprised me was that the csv reader included the trailing space in the final field value returned, even though it is outside of the quotes.
13
2495
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?
5
2133
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 related in any way to main logic flow. When "problematicStamp = ..." is commented I get gmtStamp: 1130634600.0
10
1614
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 overwriting the stderr file object method write. The external module outputs to stderr this way: from sys import std err
0
9865
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9709
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10609
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10303
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9446
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7025
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5694
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4498
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.