简体   繁体   中英

Strange TypeError occured when calling method function exposed by boost python

I write a python wrapper for box2d, everything work perfect but a strange TypeError error occurs sometimes when calling method function exposed by boost python. It's a random behavior, not happened every time.

The problem python code:

try:
    world = body.GetWorld()  # world is b2World instance, body is b2Body instance
    world.DestroyBody(body)  # raise TypeError: 'NoneType' object is not callable
except TypeError:  # catch it, and print some infomation
    print "xxxxx", world  # I got a b2World instance here
    print "xxxxx", sys.getrefcount(world)  # I got a value of 66 here
    print "xxxxx", world.DestroyBody  # I got a bound method object here
    raise

It seems all okay. How does that happened?

And part of my wrapper code:

// [file]: https://github.com/layzerar/box2d-py/blob/master/python/world.cpp
// [project]: https://github.com/layzerar/box2d-py
template<class T>
inline void b2Func_ClearUserData(T& self)
{
    using namespace boost::python;

    xdecref((PyObject*)self.GetUserData());
    self.SetUserData(NULL);
}
inline void b2World_DestroyBody(b2World& self, b2Body* body)
{
    b2Assert(self.GetBodyCount() > 0);
    b2Assert(self.IsLocked() == false);

    b2Func_ClearUserData(*body);
    self.DestroyBody(body);
}
class_<b2World, b2World_W , boost::noncopyable>("b2World")
    //...
    .def("CreateBody", b2World_CreateBody, return_internal_reference<>())
    .def("DestroyBody", b2World_DestroyBody)
    //...
;

Did I make an obvious mistake?

After a few days exploring I found the answer.

I make a mistake about the question, the exception is really not thrown by DestroyBody. It's thrown by a virtual function which is rewrite in python and called in DestroyBody function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM