[Pyrex] Pybsddb question

Stefan Behnel behnel_ml at gkec.informatik.tu-darmstadt.de
Thu Apr 13 09:07:20 CEST 2006


Hi Brian,

Brian Myers wrote:
>    def __call__(self, DBT *a, DBT *b):
>        cdef int i, res
>        res = 0
>        for i from 0 <= i < self.lstlen:
>            res = oetl_compare(&self.lst[i], a[0].data, b[0].data)
>            if res:
>                if res == ERROR:
>                    self.parent.fire_error(a[0].data, b[0].data)
>                    res = 0
>                break
>        return res
>
> my code contains a:
>
> cdef extern from "db.h":
>     struct DB:
>         pass
>     struct DBT:
>         int size
>         int *data
>     int set_bt_compare(DB *db, int (*bt_compare_fcn)(DB *db, DBT *dbt1,
> DBT *dbt2))
>
> declaration. But I'm guessing by Greg's posting that I can't just get
> the underlying pointer and typecast it. Or at least I have to check with
> the pybsddb people before doing so.


Those DBT objects aren't Python objects at all, so you can't simply expect
Python to build them for you. You will have to figure out what data is
expected in the structure, extract that data from the _Python_objects_ that
you pass into your __call__ method, convert it to C-data and then use it to
call the C function.

Meaning: you have to pass Python objects in to all Python functions/methods,
but C-data into C-functions.

Maybe you could try creating a Python representation for DBT objects as a
Pyrex extension class that stores a pointer to a DBT structure. You can then
take that pointer from a Python object that your method received and pass it
into the C function.

But since I don't really know what you're trying to achieve, the solution may
be something completely different...

Stefan



More information about the Pyrex mailing list