Index: libs/python/test/enum.py =================================================================== --- libs/python/test/enum.py (revisão 51792) +++ libs/python/test/enum.py (cópia de trabalho) @@ -4,8 +4,8 @@ ''' >>> from enum_ext import * ->>> identity(color.red) -enum_ext.color.red +>>> identity(color.red) # in case of duplicated enums it always take the last enum +enum_ext.color.blood >>> identity(color.green) enum_ext.color.green @@ -13,8 +13,8 @@ >>> identity(color.blue) enum_ext.color.blue ->>> identity(color(1)) -enum_ext.color.red +>>> identity(color(1)) # in case of duplicated enums it always take the last enum +enum_ext.color.blood >>> identity(color(2)) enum_ext.color.green @@ -28,7 +28,7 @@ --- check export to scope --- >>> identity(red) -enum_ext.color.red +enum_ext.color.blood >>> identity(green) enum_ext.color.green @@ -42,10 +42,18 @@ >>> c = colorized() >>> c.x -enum_ext.color.red +enum_ext.color.blood >>> c.x = green >>> c.x enum_ext.color.green +>>> red == blood +True +>>> red == green +False +>>> hash(red) == hash(blood) +True +>>> hash(red) == hash(green) +False ''' # pickling of enums only works with Python 2.3 or higher Index: libs/python/test/enum.cpp =================================================================== --- libs/python/test/enum.cpp (revisão 51792) +++ libs/python/test/enum.cpp (cópia de trabalho) @@ -12,7 +12,7 @@ #endif using namespace boost::python; -enum color { red = 1, green = 2, blue = 4 }; +enum color { red = 1, green = 2, blue = 4, blood = 1 }; #if BOOST_WORKAROUND(__MWERKS__, <= 0x2407) namespace boost // Pro7 has a hard time detecting enums @@ -34,6 +34,7 @@ .value("red", red) .value("green", green) .value("blue", blue) + .value("blood", blood) .export_values() ; Index: libs/python/src/object/enum.cpp =================================================================== --- libs/python/src/object/enum.cpp (revisão 51792) +++ libs/python/src/object/enum.cpp (cópia de trabalho) @@ -14,7 +14,7 @@ #include #include -namespace boost { namespace python { namespace objects { +namespace boost { namespace python { namespace objects { struct enum_object { @@ -43,7 +43,7 @@ char* name = PyString_AsString(self->name); if (name == 0) return 0; - + return PyString_FromFormat("%s.%s.%s", mod, self_->ob_type->tp_name, name); } } @@ -139,15 +139,16 @@ dict d; d["__slots__"] = tuple(); d["values"] = dict(); + d["names"] = dict(); object module_name = module_prefix(); if (module_name) d["__module__"] = module_name; if (doc) d["__doc__"] = doc; - + object result = (object(metatype))(name, make_tuple(base), d); - + scope().attr(name) = result; return result; @@ -167,7 +168,7 @@ converter::registration& converters = const_cast( converter::registry::lookup(id)); - + converters.m_class_object = downcast(this->ptr()); converter::registry::insert(to_python, id); converter::registry::insert(convertible, construct, id); @@ -186,23 +187,24 @@ dict d = extract(this->attr("values"))(); d[value] = x; - + // Set the name field in the new enum instanec enum_object* p = downcast(x.ptr()); Py_XDECREF(p->name); p->name = incref(name.ptr()); + + dict names_dict = extract(this->attr("names"))(); + names_dict[x.attr("name")] = x; } void enum_base::export_values() { - dict d = extract(this->attr("values"))(); - list values = d.values(); + dict d = extract(this->attr("names"))(); + list items = d.items(); scope current; - - for (unsigned i = 0, max = len(values); i < max; ++i) - { - api::setattr(current, object(values[i].attr("name")), values[i]); - } + + for (unsigned i = 0, max = len(items); i < max; ++i) + api::setattr(current, items[i][0], items[i][1]); } PyObject* enum_base::to_python(PyTypeObject* type_, long x)