Ticket #2744: enum_with_duplicated_values_v2.2.patch

File enum_with_duplicated_values_v2.2.patch, 2.4 KB (added by hugo.lima@…, 14 years ago)
  • libs/python/src/object/enum.cpp

     
    1414#include <boost/python/object_protocol.hpp>
    1515#include <structmember.h>
    1616
    17 namespace boost { namespace python { namespace objects { 
     17namespace boost { namespace python { namespace objects {
    1818
    1919struct enum_object
    2020{
     
    4343            char* name = PyString_AsString(self->name);
    4444            if (name == 0)
    4545                return 0;
    46            
     46
    4747            return PyString_FromFormat("%s.%s.%s", mod, self_->ob_type->tp_name, name);
    4848        }
    4949    }
     
    139139      dict d;
    140140      d["__slots__"] = tuple();
    141141      d["values"] = dict();
     142      d["names"] = dict();
    142143
    143144      object module_name = module_prefix();
    144145      if (module_name)
    145146         d["__module__"] = module_name;
    146147      if (doc)
    147148         d["__doc__"] = doc;
    148      
     149
    149150      object result = (object(metatype))(name, make_tuple(base), d);
    150      
     151
    151152      scope().attr(name) = result;
    152153
    153154      return result;
     
    167168    converter::registration& converters
    168169        = const_cast<converter::registration&>(
    169170            converter::registry::lookup(id));
    170            
     171
    171172    converters.m_class_object = downcast<PyTypeObject>(this->ptr());
    172173    converter::registry::insert(to_python, id);
    173174    converter::registry::insert(convertible, construct, id);
     
    186187
    187188    dict d = extract<dict>(this->attr("values"))();
    188189    d[value] = x;
    189    
     190
    190191    // Set the name field in the new enum instanec
    191192    enum_object* p = downcast<enum_object>(x.ptr());
    192193    Py_XDECREF(p->name);
    193194    p->name = incref(name.ptr());
     195
     196    dict names_dict = extract<dict>(this->attr("names"))();
     197    names_dict[x.attr("name")] = value;
    194198}
    195199
    196200void enum_base::export_values()
    197201{
    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();
    200204    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]);
    206208 }
    207209
    208210PyObject* enum_base::to_python(PyTypeObject* type_, long x)