On Mon, Oct 6, 2008 at 7:47 PM, Robert Bradshaw
<[EMAIL PROTECTED]> wrote:
> 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?

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.

I mean, this is some artificial example. I was just trying to get it
running to see how it works.

> 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


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

?

Ondrej
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to