On Jun 29, 2009, at 9:10 AM, Dag Sverre Seljebotn wrote:

> Bjarke Roune wrote:
>> There seems to be no better way to report issues in Cython than send
>> it to someone on the project (I suggest a better way be made that
>> doesn't require signing up for something). Here you go.
>
> It is to avoid spam (both on trac and the mailing list). Anyway,  
> thanks
> for reporting. See below.

Sorry. Yes, we only do this because it's been by far the most  
effective way of killing spam.

>
>> On Mon, Jun 29, 2009 at 5:35 PM, <[email protected]>  
>> wrote:
>>> You are not allowed to post to this mailing list, and your  
>>> message has
>>> been automatically rejected.  If you think that your messages are
>>> being rejected in error, contact the mailing list owner at
>>> [email protected].
>>>
>>>
>>>
>>> ---------- Forwarded message ----------
>>> From: Bjarke Roune <[email protected]>
>>> To: [email protected]
>>> Date: Mon, 29 Jun 2009 17:31:16 +0200
>>> Subject: Surprise: A bool isn't a bool
>>> I was wrapping a C++ function in Cython, and ran into a mysterious
>>> segmentation fault, and the cause turned out to be quite a trap for
>>> beginning Cython programmers wrapping C++ libraries. Let's say there
>>> was a function like this:
>>>
>>> bool foo() {
>>>    return true;
>>> }
>>>
>>> I then declared it in Cython as
>>>
>>> cdef extern from "bar.h":
>>>    bool foo()
>>>
>>> and I used the function in code such as
>>>
>>> if (foo()):
>>>    print "baz"
>>>
>>> This crashed my program with a segmentation violation and no  
>>> explanation.
>>>
>>> After a while of looking through my C++ code trying to find the
>>> pointer error, I realized that a C++ bool is not a Cython bool, and
>>> that since a Cython bool is an object, mismatching the two can  
>>> lead to
>>> a memory error. I replaced bool with bint and everything was fine
>>> (though I wonder if Cython ever puts anything other than 0 and 1  
>>> into
>>> a bint, since only 0 and 1 are valid values for a C++ bool).

Yes, Cython can put any int value into a bint--it's only  
distinguishing feature is conversion to/from Python objects. However,  
the C++ compiler should turn it into 0/1 whenever you assign it to/ 
use it as a C++ bool.

>>> It is surprising and confusing to a beginning Cython user such as me
>>> that a bool isn't a bool, and if there is any way to issue some sort
>>> of warning about mismatching a C++ bool with a Cython bool, I think
>>> that would be a good idea.
>
> Indeed. The question is whether it is possible to find such a way.
> Perhaps disallow "bool" in cdef extern functions? Although that would
> add an unecesarry typecheck if a C library in fact did return a Python
> boolean object... (like many C functions in Python's standard library
> might).

Perhaps at least a warning would be useful.

- Robert


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

Reply via email to