2
votes

I have a private.h, public.h and the file.c, and I need to wrap it into Cython. How do I wrap the function Person_ptr Person_create(const char* name);?

private.h:

#ifndef __PERSON_PRIVATE_H__
#define __PERSON_PRIVATE_H__

#include "Person.h"

typedef struct Person_TAG {
    char* name;
    int age;
} Person;


void person_init(Person_ptr self, const char* name);

# endif /* __PERSON_PRIVATE_H__ */

public.h

#ifndef __PERSON_H__
#define __PERSON_H__

#include <assert.h>

typedef struct Person_TAG* Person_ptr;

#define PERSON(x) \
    ((Person_ptr) (x))

#define PERSON_CHECK_INSTANCE(x) \
    assert(PERSON(x) != PERSON(NULL))

/* public interface */
Person_ptr Person_create(const char* name);
void Person_destroy(Person_ptr self);

const char* Person_get_name(const Person_ptr self);
int Person_get_age(const Person_ptr self);
void Person_birthday(Person_ptr self);

# endif /* __PERSON_H__ */

And this is file.c:

#include "Person.h"
#include "Person_private.h"

#include <stdlib.h>
#include <string.h>
#include <assert.h>

Person_ptr Person_create(const char* name)
{
    Person_ptr self = PERSON(malloc(sizeof(Person)));
    PERSON_CHECK_INSTANCE(self);
    person_init(self, name);
    return self;
}

void Person_destroy(Person_ptr self)
{
    PERSON_CHECK_INSTANCE(self);
    if (NULL != self->name) free(self->name);
}

 /* Do not free not change the returned string! */
const char* Person_get_name(const Person_ptr self)
{
    PERSON_CHECK_INSTANCE(self);
    return self->name;
}

int Person_get_age(const Person_ptr self)
{
    PERSON_CHECK_INSTANCE(self);
    return self->age;
}

void Person_birthday(Person_ptr self)
{
    PERSON_CHECK_INSTANCE(self);
    ++self->age;
}

/* private/protected methods */
void person_init(Person_ptr self, const char* name)
{
    self->name = malloc(sizeof(char) * (strlen(name) + 1));
    assert(NULL != self->name);
    strcpy(self->name, name);
    self->age = 0;
}

What I have tried to do is file.pyx:

from ctypes import *

cdef extern from "Person.h":
ctypedef struct Person_ptr:
        pass
Person_ptr Person_create "P_create" (char *name)

cdef class Person:
    cdef Person P_create(p_name):
    return Person_create(p_name)

It's not right - it compiles in Cython but then it gives me a lot of errors in GCC. How can I fix this problem?

2
Very similar question to the recently asked stackoverflow.com/questions/7619557/… - John Keyes
mmm there was no solution there @JohnKeyes - Elena Mazzi
Irrelevant. It's still the same question. - Ignacio Vazquez-Abrams
yes its similar with mine,my prof give it to me this,actually u can find this one on internet but just in c version there are not in cython. - Elena Mazzi
Welcome to Stack Overflow! Two small pieces of advice: (1) "it gives me a lot of errors in gcc. Any ideas?" -- it would be far easier to give good advice if you include the gcc command you used and the errors you get (2) Please do not use 'u' or 'thnx' sms-speak. We don't want our community to devolve into unintelligible shorthand. Please use the best English you're capable of. (We're aware of the difficulty of using English correctly, but 'u' and 'thnx' is lazy and disrespectful.) Thanks. - sarnold

2 Answers

7
votes

cperson.pxd:

cdef extern from "Person.h":
    cdef struct Person_TAG:
        pass
    ctypedef Person_TAG* Person_ptr

    Person_ptr Person_create(char* name)
    void Person_destroy(Person_ptr self)

    char* Person_get_name(Person_ptr self)

person.pyx:

from cperson cimport Person_ptr, Person_create, Person_destroy, Person_get_name

cdef class Person:
    cdef Person_ptr thisptr

    def __cinit__(self, char* name):
        self.thisptr = Person_create(name)
        if self.thisptr is NULL:
            raise MemoryError

    def __dealloc__(self):
        if self.thisptr is not NULL:
            Person_destroy(self.thisptr)

    property name:
        def __get__(self):
            return Person_get_name(self.thisptr)

person.pyxbld:

import os
from distutils.extension import Extension

dirname = os.path.dirname(__file__)

def make_ext(modname, pyxfilename):
    return Extension(name=modname,
        sources=[pyxfilename, "Person.c"],
        include_dirs=[dirname],
        # or libraries=["person"], if you compile Person.c separately 
        )

test_person.py:

import pyximport; pyximport.install()

from person import Person

p = Person('Garry')
print p.name

Example

$ python test_person.py 
Garry
1
votes

In general, it doesn't make sense to do this kind of thing by hand, unless you have some very special requirements. Instead, use SWIG to generate the wrappers for you. You don't need to struggle to get these details right, and as a bonus when the C code changes, it's trivial to regenerate new wrappers to match.