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

David Abrahams dave at boost-consulting.com
Fri Feb 10 22:06:25 CET 2006


"Niall Douglas" <s_sourceforge at nedprod.com> writes:

> The offending line is in boost\libs\python\src\object\class.cpp in 
> BPL where class_base::setattr() calls PyObject_SetAttrString(). 

I object.  What evidence do you have that this line is offending?

> 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? 

It is normally never a problem to call setattr to rebind an attribute
that already exists.  Why do you think this would make a difference?

> 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.

That's the normal behavior of Python; there's no reason any of these
contortions should be necessary.

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com




More information about the Cplusplus-sig mailing list