Czarek Tomczak, 07.12.2012 08:27:
> On Thursday, December 6, 2012 10:11:59 PM UTC-8, Stefan Behnel wrote:
>
>> What C(++) compiler are you using? (And, just for completeness, which
>> version of Cython?)
>
> I'm using Visual Studio 2008 SP1 compiler & Cython 0.17.2.
>
>
>> It would also help if you pasted the relevant lines of the generated C
>> file when reporting C/C++ compiler errors and warnings.
>
> cefpython.cpp(8597) : warning C4800: 'int' : forcing value to bool 'true'
> or 'false' (performance warning)
>
> Line 8597:
>
> __pyx_t_5 = __Pyx_PyObject_IsTrue(((PyObject *)__pyx_v_forward)); if
> (unlikely((__pyx_t_5 == (bool)-1) && PyErr_Occurred())) {...}
Hmm - this is wrong. It shouldn't try to cast -1 to a C++ bool. That's
exactly the kind of broken code that the declaration of 'bool' in
libcpp/__init__.pxd would give you. I think we should remove it.
>> Surprisingly enough, the declaration of 'bool' in libcpp/__init__.pxd is
>> this:
>>
>> ctypedef bint bool
>>
>> That doesn't look right. Have you tried using plain 'bool' instead of
>> cimporting it from libcpp? AFAIR, that's the way to do it in C++ mode.
>
> I'm not sure what you mean by plain bool, there is no such type identifier,
Hmm, doesn't this work?
cdef bool b
*without* cimporting from libcpp?
> I need to import it from cpython or libcpp, if by plain bool you mean the
> one from cpython, then I've tried this:
>
> from cpython cimport bool as py_bool
> from cpython cimport bool as c_bool
>
> But now getting a different warning:
>
> cefpython.cpp(8602) : warning C4800: 'PyBoolObject *' : forcing value
> to bool 'true' or 'false' (performance warning)
>
> Line 8602:
>
> /* "C:\cefpython\cefpython\cefpython\cef1\windows\setup\browser.pyx":211
> * self.GetCefBrowser().get().Find(searchId, cefSearchText, forward,
> * matchCase, findNext) #
> */
> __pyx_t_4.get()->Find(__pyx_v_searchId, __pyx_v_cefSearchText,
> __pyx_v_forward, __pyx_v_matchCase, __pyx_v_findNext);
Ok, this looks completely broken now.
>>> The .pyx file:
>>>
>>> from cpython cimport bool as py_bool
>>> cpdef object DoFind(self, int searchId, str searchText, py_bool
>>> forward, py_bool matchCase, py_bool findNext):
>>> Find(searchId, cefSearchText, forward, matchCase, findNext)
>>
>> I have no idea why you do this. Enforcing Python's bool type here makes
>> your interface very unpythonic.
>
> I'm not sure what you mean. I'm just making functions signatures strict
> typed to catch mistakes early.
That's ok for most types, but it's uncommon for boolean values and strings.
In Python, [] evaluates to False just like 0 or None. If you enforce the
Python 'bool' type at the interface level, you force users to explicitly
pass in True or False, instead of whatever boolean-compatible value they
currently have in their hands. I.e. code like this will fail with a TypeError:
x = 1
if x:
DoFind(1, u'abc', forward=x, matchCase=None, findNext=False)
although it would work perfectly in normal Python code. In Py2, your code
will reject the unicode text value and in all Python versions it will
reject the non-bool values for forward and matchCase in this example.
Instead, test (and, if necessary, convert) the value of searchText
yourself, and use the C++ bool type for the boolean values to get
auto-conversion on input.
That's also the reason why 'bool' isn't the Python bool type in C++ mode -
it's simply useless in almost all cases. What you want in the 99% case is
the C++ bool type.
Stefan