On 03/05/15 22:52, Jeroen Demeyer wrote:
> On 2015-05-03 22:47, Vincent Delecroix wrote:
>> Hello,
>>
>> How do we convert Python object to a C long in Cython? The following
>> does not work properly as rationals or float gets converted (to their
>> floor)
>> 
> In Cython, you call PyNumber_Index():
> 
> from cpython.number cimport PyNumber_Index
> def f(u):
>     cdef long u_long = PyNumber_Index(u)

Thanks!

If u is an instance of an extension class that properly defines
__index__, does the above call avoids the creation of an intermediate
Python int? I wonder because its signature is

  PyObject* PyNumber_Index(PyObject *o)

If not, then the following would be faster for u being a Sage integer
isn't it

def f(u):
    if type(u) is Integer:
        if mpz_fits_slong_p((<Integer>u).value):
            u_long = mpz_get_si((<Integer>u).value)
    ...

Vincent

-- 
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.

Reply via email to