[C++-sig] Checking for or null PyObject* or PyNone

Murray Cumming murrayc at murrayc.com
Sun Feb 7 11:47:08 CET 2010


Doing this
  boost::python::object obj = get_some_object();
  if(obj)
  {
     //do something
  }
seems to check a bool value inside the underlying PyObject, though I
guess that could throw an exception if it doesn't contain actually
contain a bool. Is that right?
The reference documentation is not very clear about this:
http://www.boost.org/doc/libs/1_42_0/libs/python/doc/v2/object.html


I had wrongly guessed that it would check if the underlying PyObject*
was 0. I did this because I had wrapped a PyObject* in a
boost::python::object for convenience, without checking for 0 first.

Is there any correct way to check, other than doing
  if(obj.ptr())
?


And what's the correct way to check for PyNone other than 
  if (obj == boost::python::object())
?

-- 
murrayc at murrayc.com
www.murrayc.com
www.openismus.com



More information about the Cplusplus-sig mailing list