[pypy-dev] For embedding pypy, how to deal with None value

Yicong Huang hengha.mao at gmail.com
Wed May 20 09:54:49 CEST 2015


Yes, C could *not* have a function that returns either a double or NULL.
But python could.
In addition, python could have a function paramter that is either int or
None.

The problem is we might get a python function, and we would like to call
this python function in C.
We're not sure whether this function return int or return None.
Follow the document ,we might define the function callback as below:
@ffi.callback("int (int, int)")
However, sometimes the python function will return None, which cause
Operation Error from CFFI.

On the other hand, it is valid to python for receving the function
parameter as None.
How could we pass NULL to python function via ffi.callback definition?


On Wed, May 20, 2015 at 2:39 PM, Armin Rigo <arigo at tunes.org> wrote:

> Hi Ryan,
>
> On 19 May 2015 at 20:09, Ryan Gonzalez <rymg19 at gmail.com> wrote:
> > Why not return a pointer to a double? Like (UNTESTED!):
> >
> > d = ffi.new('double*')
> > d[0] = 9.0
> > return d
>
> This doesn't work!  You can't return a ffi.new() pointer, because the
> 'd' variable is not kept alive.
>
> >>> The document Embedding PyPy shows good examples, but did not tell how
> to
> >>> deal with None value.
>
> I'm still unsure I understand the question.  You need to know at least
> a bit of C in order to use cffi.  In C you can't write a function that
> returns either a double or NULL.  First start by thinking about a
> valid C interface, and then use cffi to implement it.  Maybe you want
> something like
>
>     int foo(int argument, double *result);
>
> which returns a status code as an integer (e.g. 0=ok, -1=error), and
> in the ok case, fills in "*result".
>
>
> A bientôt,
>
> Armin.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pypy-dev/attachments/20150520/bf24a5f4/attachment.html>


More information about the pypy-dev mailing list