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

Ryan Gonzalez rymg19 at gmail.com
Tue May 19 20:09:29 CEST 2015


Why not return a pointer to a double? Like (UNTESTED!):

d = ffi.new('double*')
d[0] = 9.0
return d

Then you could return None, which is converted to a C NULL (same thing other way around).

On May 19, 2015 11:27:28 AM CDT, Yicong Huang <hengha.mao at gmail.com> wrote:
>One approach we thought of is to use additional int pointer to indicate
>whether the value is None.
>
>ffi.cdef('''struct API {    double (*add_numbers)(double x, double y);
>
>    int *ret};
>
>And then wrap the python function, if the return value is None, set
>ret[0] to 1.
>
>
>
>On Tue, May 19, 2015 at 11:06 PM, Yicong Huang <hengha.mao at gmail.com>
>wrote:
>
>> The document Embedding PyPy shows good examples, but did not tell how
>to
>> deal with None value.
>> As Python support None value, it might bring some troubles:
>> 1. How to pass NULL from C to python for None?
>> 2. If python function return None, how to handle the issue in C?
>>
>> For the above cases, we observed errors like these:
>>
>> From cffi callback <function add_numbersInt at 0x00007f25f313e110>:
>> Trying to convert the result back to C:
>> TypeError: expected integer, got NoneType object
>>
>>
>>
>
>
>------------------------------------------------------------------------
>
>_______________________________________________
>pypy-dev mailing list
>pypy-dev at python.org
>https://mail.python.org/mailman/listinfo/pypy-dev

-- 
Sent from my Android device with K-9 Mail. Please excuse my brevity.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pypy-dev/attachments/20150519/a772fb8e/attachment-0001.html>


More information about the pypy-dev mailing list