swig: conversion of C double to Python float

Lyle Johnson lyle at users.sourceforge.net
Wed Feb 11 16:30:18 CET 2004


Ernie wrote:

> Here is the swig file mvf.i:
>  %module mvf
>  %include "carrays.i"
>  %array_class(double, doubleArray);
>  %typemap(out) double, float "$result = PyFloat_FromDouble($1);"
>  %include mvf.h

<snip>

> What could have gone wrong?

You need to add a verbatim block near the top of your SWIG interface 
file, e.g.

	%module mvf

	%{
	#include "mvf.h"
	%}

	%include "carrays.i"
	%array_class(double, doubleArray);

	%include mvf.h

Otherwise, there's no declaration of SumSquares() in the SWIG-generated 
mvf_wrap.c file, and the compiler assumes that SumSquares() returns an 
int instead of a double. (You would see a warning about this if you 
added the -Wall flag to your compiler flags when compiling mvf_wrap.c). 
For more information on why this is needed, see the "Input format" 
section of the "SWIG Basics" chapter of the SWIG documentation:

	http://www.swig.org/Doc1.3/SWIG.html#n3

I've also removed the "out" typemap for double and float, since that's 
built in and doesn't need to be explicitly specified.

Hope this helps,

Lyle



More information about the Python-list mailing list