[Cython] Pure python mode
Robert Bradshaw
robertwb at math.washington.edu
Tue Oct 7 01:34:50 CEST 2008
On Oct 6, 2008, at 3:03 PM, Ondrej Certik wrote:
>> 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)
>
> Wow, this works too for me (I was trying that xx case above). This is
> what I get with pure python:
>
> 36
> False
>
> and cythonized:
>
> 42
> True
Yeah, the cython.sizeof stub always returns 1. The only place I could
see sizeof being useful is a fake "malloc," etc. that return wrapped
lists.
For most code the interpreted and compiled should do exactly the same
thing.
>> 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.
>
> This brings me the idea, why not to just take the current syntax and
> just generate a pure Python one from it? That way one also needs to
> maintain just one codebase, only it will by the Cython one, but it
> could always be used in Pure python.
>
> For some reason I prefer the pure python syntax approach though.
Same here. I'd edit the .py files then force the edit-compile-run
cycle for that mode as well. Most code already exists (or starts out)
as pure Python, then gets "cythonoized" which also makes it easier to
go this direction. I also anticipate being able to point cython at
any .py file to gain (at least some) improvements. All of these
reasons make it easier for the .py file to be the "original" rather
than generated.
Writing a .py writer would be a nontrivial amount of work as
well...certainly doable but there are more productive things that I
would rather spend my time doing :).
- Robert
More information about the Cython-dev
mailing list