Opened 11 years ago

Last modified 9 years ago

#1181 new Bugs

[boost.python] can modify enum value

Reported by: qiaozhiqiang@… Owned by: troy d. straszheim
Milestone: To Be Determined Component: python USE GITHUB
Version: Boost 1.34.0 Severity: Problem
Keywords: enum modify Cc:

Description (last modified by Dave Abrahams)

I use boost 1.34.0, python 2.5.1 User can modify the enum value, But enum should be a const value(read only).

enum my_enum{my_value};
struct my_s{ enum{ my_value2 };};

void my_export
    enum_< my_enum >("my_enum") .value("my_value", my_value);
    scope* my_s_scope = new scope(class_< my_s>("my_s", init<  >()));
    scope().attr("my_value2") = (int)my_value2;
    delete my_s_scope;
#in python
module.my_enum.my_value = 1
print module.my_enum.my_value
module.my_s.my_value2 = 2
print module.my_s.my_value2
# we can modify the const value.
# output 
#  1
#  2

Change History (3)

comment:1 Changed 10 years ago by Dave Abrahams

Description: modified (diff)

Please try to use the WikiFormatting rules to create readable tickets.

comment:2 Changed 10 years ago by Dave Abrahams

Status: newassigned

This seems reasonable enough, since we can use properties to make read-only attributes, but it will take some doing. As described in, "The get method won't be called when the property is accessed as a class attribute (C.x) instead of as an instance attribute (C().x)." This will remain a low-priority until I (or someone else -- hint, hint) figures out how to appropriately "override the __get__ operation for properties used as a class attribute" (in pure python code) as described on that page.

comment:3 Changed 9 years ago by troy d. straszheim

Owner: changed from Dave Abrahams to troy d. straszheim
Status: assignednew

I have a fix for this locally, but it needs some cleanup/refactoring yet. It borrows some of the internals of class_ (specifically the metatype) to implement readonly class properties.

Note: See TracTickets for help on using tickets.