Dag Sverre Seljebotn, 07.07.2010 09:36:
> Stefan Behnel wrote:
>> With my limited knowledge of C++, I don't see a problem here. After all,
>> Cython ignores the exact values stored in those references, and as longs as
>> you can't pass a ref-counted Python object as a C++ reference, it doesn't
>> have to know anything about them.
>>
>> This will not work, though:
>>
>>       function_taking_a_reference(<PyObject*>some_object)
>>
>> but it should not be too hard to catch by the compiler, in case it's
>> currently valid.
> [...]
> The current state in Cython leads to the following problem:
>
> C++:
> void f(int&  x) { x = 10; }
>
> Cython:
> cdef extern from "foo.h":
>      void f(int x) # int* will cause syntax errors, it must be plain int
>
> cdef int x = 3
> y = 3 # object
> f(x) # x changes to 10
> f(y) # the temporary result of conversion is changed -- y is not changed!

Right, that's ugly. However, what I actually meant above is this:

C++: (not sure if this syntax works)

void f(PyObject*&  x) { x = PyInt_FromLong(10); }

Cython:
cdef extern from "foo.h":
       void f(object x)

f(5)


But I'd also be fine with laughing at the hole in the foot of a user who 
does that.


> So the current state is both inobvious and would lead to hard to find
> bugs IMO. So that's why I argue explicit support is needed in some form
> or another.

Agreed. I think a specific type for C++ references would solve this, 
though. If you could write something like

cdef extern from "foo.h":
       void f(&int x)           # syntax improvements pending :)

then Cython could take care about required coercions, re-assignments and 
compile time errors.

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

Reply via email to