>>
>> To clarify, this is failing for you both interpreted and compiled?
>
> In fact, cython refuses to compile this one:
>
> $ cython t.py
>
> Error converting Pyrex file to C:
> ------------------------------------------------------------
> ...
> #foo(5)
>
> x = cython.declare(int)
> xx = cython.declare(cython.p_int)
> xx = cython.address(x)
> foo(xx)
> ^
> ------------------------------------------------------------
>
> But when I tried the original foo(5), the cython version failed too.
This will fail, because foo takes an int.
> I mean, this is some artificial example. I was just trying to get it
> running to see how it works.
Yeah, my example was totally artificial...
>
>> Try changing the line
>>
>>> y = cython.cast(cython.p_int, x)
>>
>>
>> to
>>
>>> y = cython.address(x)
>>
>>
>> so it can make sense of it.
>
> Still the same problem:
>
> Traceback (most recent call last):
> File "t.py", line 17, in <module>
> foo(xx)
> File "t.py", line 5, in foo
> y = cython.address(x)
> File "/home/ondra/lib/lib/python/Cython/Shadow.py", line 22, in
> address
> return pointer(type(arg))([arg])
> File "/home/ondra/lib/lib/python/Cython/Shadow.py", line 49, in
> __init__
> self._items = [cast(self._basetype, a) for a in value]
> File "/home/ondra/lib/lib/python/Cython/Shadow.py", line 14, in cast
> return type(arg)
> File "/home/ondra/lib/lib/python/Cython/Shadow.py", line 53, in
> __init__
> raise ValueError
> ValueError
This works for me:
import cython
@cython.locals(x=int, y=cython.p_int)
def foo(x):
y = cython.address(x)
print x + cython.sizeof(y) + cython.sizeof(cython.p_int)
print cython.compiled
foo(34)
> But I tried this simpler example:
>
> import cython
>
> @cython.locals(x=int)
> def foo(x):
> return x+5
>
> print foo(5)
>
>
> And this works as expected both interpreted and compiled! Very nice.
> How will you implement something like:
>
> "
> def f(x):
> return g(x)
>
> cdef int g(int x):
> return x+5
> "
>
> E.g. having the option to write pure C functions (and classes). Maybe
> something like:
>
> @cython.locals(g=int, x=int)
> def g(x):
> return x+5
Exactly how to do this with decorators (and/or automatically) is less
clear (syntax-wise, and there are more technical issues about
signatures matching, etc.). What you can do is write an
accompanying .pxd file which will, of course, be ignored by Python,
but can declare cdef classes/methods/etc for Cython. Then your class
and def statements will be appropriately coerced when you compile.
- Robert
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev