Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
typedef struct consistency between C and Cython
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Al Danial  
View profile  
 More options Nov 11 2012, 11:30 pm
From: Al Danial <a...@danial.org>
Date: Sun, 11 Nov 2012 20:30:03 -0800
Local: Sun, Nov 11 2012 11:30 pm
Subject: typedef struct consistency between C and Cython

I'm trying to wrap a legacy C library with cython.  The C library
defines a custom data type with a 'typedef struct'.  The Python
side of the code needs to initialize C variables having this
type but I haven't figured out how to declare the variables
correctly.  Cython complains that

  ./library.c:2:6: note: expected ‘legacy_struct’ but argument is of type
‘__pyx_t_10new_module_legacy_struct’

Platform:
  Python 2.7.3
  Cython 0.17.1
  Fedora 17 i686

Files that boil the problem down to the bare minimum are below and
also in the attached tar file.  Anyone know where I'm going wrong?
      -- Al

-------------- \/ cyth.pyx
#!/usr/bin/python

ctypedef struct legacy_struct:
    int     *S_start
    double  *N
    char    *data

cdef extern from "library.c":
    int  legacy_function(int          nC ,
                        legacy_struct A)
import  numpy as np
cimport numpy as np
np.import_array()

def Save():
    cdef int nC = 0
    cdef legacy_struct m
    legacy_function( nC , <legacy_struct> m)
    return
-------------- /\ cyth.pyx

-------------- \/ header.h
typedef struct {
    int     *S_start  ;
    double  *N        ;
    char    *data     ;

} legacy_struct;

int legacy_function(int           nC ,
                    legacy_struct A  );
-------------- /\ header.h

-------------- \/ library.c
#include "header.h"
int  legacy_function(int           nC,
                    legacy_struct  A ) {
    A.S_start[nC] = 0;
    return 1;

}

-------------- /\ library.c

-------------- \/ Makefile
CFLAGS= -g
LDFLAGS= -g

all: cyth.pyx library.c header.h
LDFLAGS=$(LDFLAGS) CFLAGS=$(CFLAGS) python setup.py build_ext --inplace

clean:
rm -rf build *.so cyth.c library.o
-------------- /\ Makefile

-------------- \/ setup.py
import numpy
from Cython.Distutils import build_ext

def configuration(parent_package='', top_path=None):
    from numpy.distutils.misc_util import Configuration
    config = Configuration('', parent_package, top_path)
    config.add_extension('new_module',
                         sources=['cyth.pyx'],
                         depends=['cyth.c'],
                         extra_compile_args=["-g"],
                         extra_link_args=["-g"],
                         include_dirs=[numpy.get_include()])
    return config

if __name__ == '__main__':
    params = configuration(top_path='').todict()
    params['cmdclass'] = dict(build_ext=build_ext)
    from numpy.distutils.core import setup
    setup(**params)
-------------- /\ setup.py

  cython_C_struct.tar.gz
1K Download

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Stefan Behnel  
View profile  
 More options Nov 12 2012, 1:20 am
From: Stefan Behnel <stefan...@behnel.de>
Date: Mon, 12 Nov 2012 07:20:34 +0100
Local: Mon, Nov 12 2012 1:20 am
Subject: Re: [cython-users] typedef struct consistency between C and Cython
Al Danial, 12.11.2012 05:30:

> I'm trying to wrap a legacy C library with cython.  The C library
> defines a custom data type with a 'typedef struct'.  The Python
> side of the code needs to initialize C variables having this
> type but I haven't figured out how to declare the variables
> correctly.  Cython complains that

>   ./library.c:2:6: note: expected ‘legacy_struct’ but argument is of type
> ‘__pyx_t_10new_module_legacy_struct’

It's the C compiler complaining here.

> Files that boil the problem down to the bare minimum are below and
> also in the attached tar file.  Anyone know where I'm going wrong?
>       -- Al

> -------------- \/ cyth.pyx
> #!/usr/bin/python

> ctypedef struct legacy_struct:
>     int     *S_start
>     double  *N
>     char    *data

> cdef extern from "library.c":
>     int  legacy_function(int          nC ,
>                         legacy_struct A)

Since you're dealing with an externally defined type, you have to move the
ctypedef into the cdef extern block. Otherwise, Cython will define the type
itself in the C code, which is not what you want here.

Stefan


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Al Danial  
View profile  
 More options Nov 12 2012, 10:15 am
From: Al Danial <a...@danial.org>
Date: Mon, 12 Nov 2012 07:15:11 -0800
Local: Mon, Nov 12 2012 10:15 am
Subject: Re: [cython-users] typedef struct consistency between C and Cython

So simple!  And yet I'd have never figured it out on my own.  Thanks,
my code builds cleanly now.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »