On Oct 6, 2008, at 10:25 AM, Ondrej Certik wrote: > On Mon, Oct 6, 2008 at 7:04 PM, Robert Bradshaw > <[EMAIL PROTECTED]> wrote: >> On Oct 5, 2008, at 3:46 PM, Ondrej Certik wrote: >> >>> On Sat, Oct 4, 2008 at 1:42 PM, Robert Bradshaw >>> <[EMAIL PROTECTED]> 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. > > No, you are right, I was just trying to get it run. > >> >>> 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. > > > I tried that too: > > [EMAIL PROTECTED]:~/ext/cython-pure$ cat t.py > import 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) > > x = cython.declare(int) > xx = cython.declare(cython.p_int) > xx = cython.address(x) > foo(xx) > [EMAIL PROTECTED]:~/ext/cython-pure$ python t.py > Traceback (most recent call last): > File "t.py", line 17, in <module> > foo(xx) > 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 > > > With the same output. I copied the cython.py from cython-devel, is > that correct? I am probably doing something stupid. The error is > really simple and it fails, but currently I am not sure what it is > supposed to do correctly.
To clarify, this is failing for you both interpreted and compiled? Try changing the line > y = cython.cast(cython.p_int, x) to > y = cython.address(x) so it can make sense of it. - Robert _______________________________________________ Cython-dev mailing list [email protected] http://codespeak.net/mailman/listinfo/cython-dev
