[Pyrex] Wrapping "typedef struct _A A;", third pass
Edward C. Jones
edcjones at erols.com
Thu Jun 5 04:07:44 CEST 2003
The following works. But if it is the best way of doing things, Pyrex
has a wart. The declaration
cdef struct _A:
double big_lie
below doesn't seem to be needed except to turn off an error message
about incomplete types. Looking at "wrapper.c", I see essentially
static struct _A __pyx_v_a;
PyObject *__pyx_1 = 0;
__pyx_v_a = make(7);
__pyx_1 = PyInt_FromLong(get(__pyx_v_a));
__Pyx_PrintItem(__pyx_1)
Can Pyrex know that it doesn't care how _A is defined as long as it is a
struct? Perhaps:
cdef struct _A:
pass
ctypedef _A A
because in C "typedef struct _A A;" is _syntactically_ similar to
"typedef struct _A {} A;".
----------------
mystery.h:
struct _A {
int i;
};
----------------
tds.h:
#include "mystery.h"
typedef struct _A A;
A make(int i);
int get(A a);
----------------
tds.c:
#include "tds.h"
A make(int i)
{
A a;
a.i = 7;
return a;
}
int get(A a)
{
return a.i;
}
----------------
wrapped.pyx:
cdef extern from "tds.h":
cdef struct _A:
double big_lie
ctypedef _A A
A make(int i)
int get(A a)
cdef A a
a = make(7)
print get(a)
----------------
test.py:
#! /usr/bin/env python
import wrapped
----------------
somake:
if pyrexc wrapped.pyx
then {
if gcc -c -fPIC -I/usr/include/python2.2/ wrapped.c
then {
gcc -c -fPIC tds.c;
gcc -shared tds.o wrapped.o -o wrapped.so;
} fi;
} fi;
More information about the Pyrex
mailing list