[Cython] Pure python mode
Robert Bradshaw
robertwb at math.washington.edu
Mon Oct 6 19:04:44 CEST 2008
On Oct 5, 2008, at 3:46 PM, Ondrej Certik wrote:
> On Sat, Oct 4, 2008 at 1:42 PM, Robert Bradshaw
> <robertwb at math.washington.edu> wrote:
>> I polished off some more of the pure python mode stuff tonight. I
>> just pushed it to cython-devel. Now you can do stuff like:
>>
>>> import cython # you need a single file to be in your path to use
>>> it in pure Python mode, provided (and installed) with Cython
>>>
>>> @cython.locals(x=int, y=cython.p_int)
>>> def foo(x):
>>> y = cython.cast(cython.p_int, x)
>>> print x + cython.sizeof(y) + cython.sizeof(cython.p_int)
>>> if cython.compiled:
>>> print "the compiler was run
>>> else:
>>> print "just being interpreted"
>>>
>>> x = cython.declare(int)
>>> xx = cython.declare(cython.p_int)
>>> xx = cython.address(x)
>>>
>>> MyStruct = cython.struct(x=int, y=int, data=cython.pp_double)
>>>
>>> a = cython.declare(MyStruct)
>>> a.x = 5
>>>
>>> T = cython.typedef(MyStruct)
>>> b = cython.declare(cython.pointer(T))
>>> b[0].x = 4
>>
>>
>> On top of this, you can write a .pxd file to accompany your .py file
>> and it will coerce your .py file classes and def statements into cdef
>> and cpdef versions.
>>
>> It's still very new and probably highly experimental (though it only
>> modifies the cython.* nodes, so it shouldn't cause any regressions).
>> Let me know what you think.
>
> Thanks a lot for doing this!
You're welcome.
> I installed cython-devel and then I tried this little example:
>
>
> --------
> import Cython as cython
>
> @cython.locals(x=int, y=cython.p_int)
> def foo(x):
> y = cython.cast(cython.p_int, x)
> print x + cython.sizeof(y) + cython.sizeof(cython.p_int)
> if cython.compiled:
> print "the compiler was run"
> else:
> print "just being interpreted"
>
> foo(5)
> -----------
>
> When running in it in Python, it gives:
>
> File "t.py", line 12, in <module>
> foo(5)
> File "t.py", line 5, in foo
> y = cython.cast(cython.p_int, x)
> 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
>
>
> Am I using it incorrectly?
No, it's just not letting you cast an int into an p_int. I suppose it
should? It just doesn't make sense to emulate this in Python.
> Btw, when I compile it with Cython:
>
> $ python -c "import t; t.foo(5)"
> Traceback (most recent call last):
> File "<string>", line 1, in <module>
> File "t.py", line 12, in t (t.c:515)
> foo(5)
> File "t.py", line 5, in t.foo (t.c:265)
> y = cython.cast(cython.p_int, x)
> 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
You need to import cython, not import Cython as cython.
> Looking at the code, I need to pass it Cython p_int integers, right? I
> also tried:
>
> x = cython.declare(int)
> xx = cython.declare(cython.p_int)
> xx = cython.address(x)
> foo(xx)
>
> But this also raises the same error as above.
This should work, Again, I think it's because you imported Cython
rather than cython.
> But otherwise this is
> exactly the way to go, I am really looking forward to just having one
> codebase for both compiled and interpreted stuff. Unfortunately, I got
> again busy lately to implement this myself, so thanks again for the
> work.
>
> Ondrej
> _______________________________________________
> Cython-dev mailing list
> Cython-dev at codespeak.net
> http://codespeak.net/mailman/listinfo/cython-dev
More information about the Cython-dev
mailing list