On Jul 7, 2009, at 11:15 AM, David Simmons-Duffin wrote:

> I'm trying to interface with some C code that defines a type called
> "object"

The type "object" is used to denote a Python object (essentially a  
refcounted PyObject*), which is why you're having trouble redefining it.

> typedef union {
>    struct {
>             objtype type; reftype nref;
>    } any;
>    intcel i;
>    bigint b;
>    vector v;
>    matrix m;
>    poly   pl;
>    simpgrp s;
>    group g;
>    tekst  t;
> } objcel, *object;
>
> However, if I try to write some cython code involving an object:
>
> cdef extern object groupmake(char lietype, int rank)
> cdef object g
> g = groupmake("A", 3)
>
> It's interpreted as a PyObject in the c code:
>
> PyObject *__pyx_t_1 = NULL;
> ...
> __pyx_t_1 = groupmake('A', 2);
>
> One possible solution is to just replace the word "object" in the C
> source with something else throughout.  But I'd like to make as few
> modifications of the C source as possible.  Is there an alternate
> solution?

Yes, you can use the magic c-namespace-renaming quotes. For your  
example.

cdef extern from *:
     ctypedef int objtype
     ctypedef int reftype
     ctypedef int intcel
     cdef struct any_struct:
         objtype type
         reftype nref
     ctypedef union objcel:
         any_struct any
         intcel i
         ...
     ctypedef objcel *my_c_object "object"

Now use my_c_object, and in the c sources it will come out as object.

>
> I actually have another unrelated question: what is the correct cython
> syntax for the union statement at the top of this message?  Cython
> doesn't seem to like me putting
>
> struct any:
>       objtype type
>       reftype nfref
>
> in the middle of a union statement.

Cython doesn't support anonymous structs. Give that struct a name,  
and then you can declare your variable with that type. (The name you  
choose will never actually be used.)

> And I'm also not sure what to do
> with "objcel, *object" at the end there.  I'm a novice at this stuff,
> so apologies if these are stupid questions.

No worries. The only stupid questions are those that should have been  
answered by a couple minutes of googling and reading the wiki. These  
ones don't fall into that boat.

Just out of curiosity, what are you wrapping? It looks like a math  
library of sorts.

- Robert

_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to