Ticket #2744: enum_with_duplicated_values_v2.2.patch
File enum_with_duplicated_values_v2.2.patch, 2.4 KB (added by , 14 years ago) |
---|
-
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")] = value; 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)