[C++-sig] Static public variable definition not working

Niall Douglas s_sourceforge at nedprod.com
Thu Feb 9 18:28:02 CET 2006


In FXGLContext.pypp.cpp:

        FXGLContext_exposer.def_readonly( "metaClass", 
FX::FXGLContext::metaClass );

This throws an exception of:

exceptions.AttributeError, can't set attribute


This is caused by pyplusplus trying to expose the public static 
variable:

static const FX::FXMetaClass metaClass;

As far as I can see pyplusplus is doing the right thing. It may be a 
problem in BPL.

The offending line is in boost\libs\python\src\object\class.cpp in 
BPL where class_base::setattr() calls PyObject_SetAttrString(). It's 
simply failing with the above exception:

  void class_base::setattr(char const* name, object const& x)
  {
      if (PyObject_SetAttrString(this->ptr(), 
const_cast<char*>(name), x.ptr()) < 0)
          throw_error_already_set();
  }


Is it possible that because FXGLContext has a base of FXId that the 
wrapper for FXId has already set "metaClass" to its own metaClass? If 
so, BPL should be rewritten as follows:

  void class_base::setattr(char const* name, object const& x)
  {
      if (PyObject_HasAttrString(this->ptr(), 
const_cast<char*>(name))
      {
          if (PyObject_DelAttrString(this->ptr(), 
const_cast<char*>(name), x.ptr()) < 0)
              throw_error_already_set();
      }
      if (PyObject_SetAttrString(this->ptr(), 
const_cast<char*>(name), x.ptr()) < 0)
          throw_error_already_set();
  }

In other words, if you set an attribute for a second time in BPL, you 
overwrite the old one rather than generate an error.

Cheers,
Niall






More information about the Cplusplus-sig mailing list