[PySide] Shiboken-generated __nonzero__() methods and truth value testing

Matthew Woehlke matthew.woehlke at kitware.com
Mon Sep 23 19:20:26 CEST 2013

On 2013-09-22 16:46, John Tourtellott wrote:
> I am using Shiboken to wrap a small class library. In my typesystem.xml
> file, I have added a __nonzero__() method to some of the classes using the
> <add-function> element. The method gets generated OK and I can call it from
> python, however, python is *not* calling the Shiboken-generated
> __nonzero__() method when doing truth value testing, for example, in an "if
> object:" expression.
> Looking briefly at the Shiboken source, in particular,
> CppGenerator::hasBoolCast(), I get the impression that I *must* use the
> --use-isnull-as-nb_nonzero command-line option in order to get the expected
> boolean value. My tests further indicate that this option only applies to
> isNull() methods that are implemented in the C++ classes, not added via the
> typesystem file. Is this correct?

According to 
type customizations like this never use "normal" functions, but must use 
the nb_* members of the cpython object structure.

Do I guess there are a few options:

1. Add a C++ isNull().
2. Modify Shiboken to allow specifying the name of the nb_bool method. 
(Still must implement it in C++)
3. Modify Shiboken to recognize typesystem-added functions for nb_bool.
4. Modify Shiboken to (preferentially?) recognize __nonzero__ as the 
nb_bool method (likely in combination with (3)).

(2), (3) and (4) are more or less orthogonal; there would be benefit to 
implementing all of them.

I happen to know that (1) is not feasible in our case, as the class in 
question is an STL class.


More information about the PySide mailing list