Re: [Cython] Surprise: A bool isn't a bool
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.
> On Mon, Jun 29, 2009 at 5:35 PM, <cython-dev-owner@...> 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
>> cython-dev-owner@...
>>
>>
>>
>> ---------- Forwarded message ----------
>> From: Bjarke Roune <bjarke@...>
>> To: cython-dev@...
>> 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).
>>
>> 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).
--
--
Dag Sverre