[Numpy-discussion] error in ctypes example from the numpy book?

W. Bryan Smith cssmwbs at gmail.com
Tue Aug 29 11:15:21 EDT 2006


hi,

i posted this to the forum, but it looks like the email list gets much more
traffic, so here goes.

i am attempting to reproduce a portion of the example on using ctypes from
the current version of the numpy book (the example can be found on pps
313-16).

here is what i am trying to do:

import numpy
import interface

x = numpy.array(range(1,1))
y = numpy.ones_like(x)
z = interface.add(a,b)

prints the following error:
BEGIN ERROR>>
26 b = N.require(b, dtype, requires)
27 c = N.empty_like(a)
---> 28 func(a,b,c,a.size)
29 return c
30

ArgumentError: argument 1: exceptions.TypeError: Don't know how to convert
parameter 1
<<END ERROR

In case it matters, here are the relevant bits of code I am using (since I
have not just copied the entire text from the example):

BEGIN testAddInt.c >>
/* Add arrays of contiguous data */
typedef struct {double real;} cdouble;
typedef struct {float real;} cfloat;

void dadd(double *a, double *b, double *c, long n)
{
while (n--) {
*c++ = *a++ + *b++;
}
}
void sadd(float *a, float *b, float *c, long n)
{
while (n--) {
*c++ = *a++ + *b++;
}
}

<<END testAddInt.c

and the interface code:
BEGIN interface.py>>

__all__ = ['add']

import numpy as N
from ctypes import *
import os

_path = os.path.dirname('__file__')

lib = N.ctypeslib.ctypes_load_library('testAddInt', _path)

for name in ['sadd','dadd']:
getattr(lib,name).restype=None

def select(dtype):
if dtype.char in['?bBhHf']:
return lib.sadd, single
else:
return lib.dadd, float
return func, ntype

def add(a,b):
requires = ['CONTIGUOUS','ALIGNED']
a = N.asanyarray(a)
func, dtype = select(a.dtype)
a = N.require(a, dtype, requires)
b = N.require(b, dtype, requires)
c = N.empty_like(a)
func(a,b,c,a.size)
return c
<<END interface.py

any hints/clues/suggestions as to what i am doing wrong here? since i am so
new at this, i am assuming there is a simple mistake in what i am doing, but
i cannot find it! thanks so much,

bryan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20060829/40830f71/attachment.html>


More information about the NumPy-Discussion mailing list