__nonzero__ does not work

Bug #962168 reported by Johan Råde
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
PyBindGen
New
Undecided
Unassigned

Bug Description

I can not add the special method __nonzero__ to my wrapped classes.
Here is a test that shows the problem.

(Adding __len__ and __getitem__ works, but __nonzero__ does not.
I'm using Python 2.7 and PyBindGen 0.15.0.)

----- Test.h: -----

class X0 {
public:
    operator bool() const { return false; }
};

class X1 {
public:
    bool __nonzero__() const { return false; }
};

class X2 {
public:
    bool nonZero() const { return false; }
};

class X3 {};

inline bool __nonzero__(const X3&) { return false; }

class X4 {};

inline bool nonZero(const X4&) { return false; }

----- TestBindings.py -----

import pybindgen
param = pybindgen.param
retval = pybindgen.retval

Test = pybindgen.Module('Test')
Test.add_include('"Test.h"')

X0 = Test.add_class('X0')
X0.add_constructor([])
X0.add_method('operator bool', retval('bool'), [], custom_name = '__nonzero__')

X1 = Test.add_class('X1')
X1.add_constructor([])
X1.add_method('__nonzero__', retval('bool'), [])

X2 = Test.add_class('X2')
X2.add_constructor([])
X2.add_method('nonZero', retval('bool'), [], custom_name = '__nonzero__')

X3 = Test.add_class('X3')
X3.add_constructor([])
X3.add_function_as_method('__nonzero__', retval('bool'), [param('const X3&', 'x3')])

X4 = Test.add_class('X4')
X4.add_constructor([])
X4.add_function_as_method('nonZero', retval('bool'), [param('const X4&', 'x4')], custom_name = '__nonzero__')

----- Main.py -----

import Test
print bool(Test.X0())
print bool(Test.X1())
print bool(Test.X2())
print bool(Test.X3())
print bool(Test.X4())

----- Expected result -----

Main.py produces the output

False
False
False
False
False

----- Observed result -----

Main.py produces the output

True
True
True
True
True

Revision history for this message
Johan Råde (johan-rade) wrote :

I took a look at the C code generated by PyBindGen.
(Fortunately PyBindGen generated exceptionally readable code.)
There I could see that my __nonzero__ method was added to the tp_methods member of the type object.
It should have been added as the nb_nonzero member of the tp_as_number member of the type object; see
http://docs.python.org/c-api/typeobj.html#number-object-structures

So it seems that either I have added __nonzero__ the wrong way, or this is a PyBindGen bug.

Revision history for this message
Gustavo Carneiro (gjc) wrote :

These are simply not regular methods. There's a generic pybindgen support for normal methods, but these are special methods that require specific handling for each one. This is a case of a feature not implemented yet.

Revision history for this message
Johan Råde (johan-rade) wrote :

Is there a list anywhere in the docs of important features that are not implemented yet?

Revision history for this message
Gustavo Carneiro (gjc) wrote :

Incomplete list here: http://code.google.com/p/pybindgen/wiki/Features

I should add: by default, assume that __special_method__ is not properly supported, until evidence to the contrary.

Sorry, blame python, not me, for not making things easier :P

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.