Boost logo

Boost Users :

Subject: Re: [Boost-users] Boost::python readonly access to nonconstant class members
From: Ralf Goertz (R_Goertz_at_[hidden])
Date: 2010-12-22 03:42:42


Dave Abrahams wrote:

> At Tue, 21 Dec 2010 15:47:38 +0100,
> Ralf Goertz wrote:
>>
>> Hi,
>>
>> is it possible to have python have readonly access to class members
>> that are non constant?
>>
>> typedef map<string,string> Links;
>>
>> class DataSet {
>> Links links;
>> }
>>
>> BOOST_PYTHON_MODULE(_boosttest) {
>> class_<Links>("Links")
>> .def(map_indexing_suite<Links>());
>>
>> class_<DataSet>("DataSet")
>> .def_readonly("links",&DataSet::links);
>> }
>>
>> Although DataSet.links is defined readonly I can change it from within
>> python.
>
> If you mean that you can do
>
> d = DataSet()
> d.links = 1
>
> that would be a Boost.Python bug.

No I don't mean that and indeed that does not work:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: can't set attribute

> If instead you mean that you can do
>
> d = DataSet()
> d.links["foo"] = "bar"
>
> that would be unsurprising (to me), intended behavior; that's just how
> the Python object model works.

I know this would be intended if I had used def_readwrite("links",...)
above. At least that's how I read

http://www.boost.org/doc/libs/1_45_0/libs/python/doc/tutorial/doc/html/python/exposing.html#python.class_data_members

The only differences there to my case are that I can't have links to be
a constant member of DataSet and that I am using map_indexing_suite. Is
the latter the problem maybe?

 
>> What can I do to prevent python from overwriting links?
>
> You could always do the equivalent of
>
> del DataSet.__dict__["__setitem__"]
>
> to disable it.

I have tried

del Links.__dict__["__setitem__"]

as I am concerned with the member variable of type Links but that gave
the error

TypeError: 'dictproxy' object does not support item deletion

I guess map_indexing_suite creates this dictproxy which doesn't allow me
to delete __setitem__. Maybe I can use some policy with
map_indexing_suite? Sorry for my unqualified questions but I am only
starting to learn python.

Ralf


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net