Ticket #2744: enum_with_duplicated_values_v2.3.patch
File enum_with_duplicated_values_v2.3.patch, 4.2 KB (added by , 14 years ago) |
---|
-
libs/python/test/enum.py
4 4 ''' 5 5 >>> from enum_ext import * 6 6 7 >>> identity(color.red) 8 enum_ext.color. red7 >>> identity(color.red) # in case of duplicated enums it always take the last enum 8 enum_ext.color.blood 9 9 10 10 >>> identity(color.green) 11 11 enum_ext.color.green … … 13 13 >>> identity(color.blue) 14 14 enum_ext.color.blue 15 15 16 >>> identity(color(1)) 17 enum_ext.color. red16 >>> identity(color(1)) # in case of duplicated enums it always take the last enum 17 enum_ext.color.blood 18 18 19 19 >>> identity(color(2)) 20 20 enum_ext.color.green … … 28 28 --- check export to scope --- 29 29 30 30 >>> identity(red) 31 enum_ext.color. red31 enum_ext.color.blood 32 32 33 33 >>> identity(green) 34 34 enum_ext.color.green … … 42 42 43 43 >>> c = colorized() 44 44 >>> c.x 45 enum_ext.color. red45 enum_ext.color.blood 46 46 >>> c.x = green 47 47 >>> c.x 48 48 enum_ext.color.green 49 >>> red == blood 50 True 51 >>> red == green 52 False 53 >>> hash(red) == hash(blood) 54 True 55 >>> hash(red) == hash(green) 56 False 49 57 ''' 50 58 51 59 # pickling of enums only works with Python 2.3 or higher -
libs/python/test/enum.cpp
12 12 #endif 13 13 using namespace boost::python; 14 14 15 enum color { red = 1, green = 2, blue = 4 };15 enum color { red = 1, green = 2, blue = 4, blood = 1 }; 16 16 17 17 #if BOOST_WORKAROUND(__MWERKS__, <= 0x2407) 18 18 namespace boost // Pro7 has a hard time detecting enums … … 34 34 .value("red", red) 35 35 .value("green", green) 36 36 .value("blue", blue) 37 .value("blood", blood) 37 38 .export_values() 38 39 ; 39 40 -
libs/python/src/object/enum.cpp
14 14 #include <boost/python/object_protocol.hpp> 15 15 #include <structmember.h> 16 16 17 namespace boost { namespace python { namespace objects { 17 namespace boost { namespace python { namespace objects { 18 18 19 19 struct enum_object 20 20 { … … 43 43 char* name = PyString_AsString(self->name); 44 44 if (name == 0) 45 45 return 0; 46 46 47 47 return PyString_FromFormat("%s.%s.%s", mod, self_->ob_type->tp_name, name); 48 48 } 49 49 } … … 139 139 dict d; 140 140 d["__slots__"] = tuple(); 141 141 d["values"] = dict(); 142 d["names"] = dict(); 142 143 143 144 object module_name = module_prefix(); 144 145 if (module_name) 145 146 d["__module__"] = module_name; 146 147 if (doc) 147 148 d["__doc__"] = doc; 148 149 149 150 object result = (object(metatype))(name, make_tuple(base), d); 150 151 151 152 scope().attr(name) = result; 152 153 153 154 return result; … … 167 168 converter::registration& converters 168 169 = const_cast<converter::registration&>( 169 170 converter::registry::lookup(id)); 170 171 171 172 converters.m_class_object = downcast<PyTypeObject>(this->ptr()); 172 173 converter::registry::insert(to_python, id); 173 174 converter::registry::insert(convertible, construct, id); … … 186 187 187 188 dict d = extract<dict>(this->attr("values"))(); 188 189 d[value] = x; 189 190 190 191 // Set the name field in the new enum instanec 191 192 enum_object* p = downcast<enum_object>(x.ptr()); 192 193 Py_XDECREF(p->name); 193 194 p->name = incref(name.ptr()); 195 196 dict names_dict = extract<dict>(this->attr("names"))(); 197 names_dict[x.attr("name")] = x; 194 198 } 195 199 196 200 void enum_base::export_values() 197 201 { 198 dict d = extract<dict>(this->attr(" values"))();199 list values = d.values();202 dict d = extract<dict>(this->attr("names"))(); 203 list items = d.items(); 200 204 scope current; 201 202 for (unsigned i = 0, max = len(values); i < max; ++i) 203 { 204 api::setattr(current, object(values[i].attr("name")), values[i]); 205 } 205 206 for (unsigned i = 0, max = len(items); i < max; ++i) 207 api::setattr(current, items[i][0], items[i][1]); 206 208 } 207 209 208 210 PyObject* enum_base::to_python(PyTypeObject* type_, long x)