From: Phil Austin (paustin_at_[hidden])
Date: 2001-10-29 17:40:57


I'm a little confused about how to access an instance's __dict__
once I've overridden getattr and setattr using BPL.
The default getattr and setattr provided by boost
work as I expect, i.e. after creating an instance of the wrapped
class called outfile, the lines:

> outfile.test="a string"
> print "attribute test: ",outfile.test
> print "asking for dict: ",outfile.__dict__

produce:

attribute test: a string
asking for dict: {'test': 'a string'}

However when I add my own versions of __getattr__ and __setattr__
in BOOST_PYTHON_MODULE_INIT:

  HDFFile_class.def(&HDFFile::get_attribute, "__getattr__");
  HDFFile_class.def(&HDFFile::set_attribute, "__setattr__");

Where:

py::ref HDFFile::get_attribute(const py::string& pyname)
{
  std::cout << "in get_attribute " << pyname.c_str() << std::endl;
  if (std::strcmp(pyname.c_str(), "__dict__") == 0){
    std::cout << "caught dict " << std::endl;
  }
  return attribute_set->get_attribute(pyname);
}

void HDFFile::set_attribute(const py::string& pyname, const py::ref& x)
{
  std::cout << "in set_attribute " << pyname.c_str() << std::endl;
  attribute_set->set_attribute(pyname, x);
}

I can't trap attempts to read __dict__ from Python, or alternatively,
figure out how to get my attributes into the instance namespace.

With the new getattr and setattr,

> outfile.test="a string"
> print "attribute test: ",outfile.test
> print "asking for dict: ",outfile.__dict__

now produces:

in set_attribute test
attribute test: in get_attribute test
 a string
asking for dict: {}

Any suggestions appreciated, I'm happy to provide the module
code if it will help.

Regards, Phil Austin