diff --git a/doc/v2/enum.html b/doc/v2/enum.html index a9c6e261..525f9d04 100644 --- a/doc/v2/enum.html +++ b/doc/v2/enum.html @@ -219,7 +219,7 @@ TypeError: bad argument type for built-in operation Revised - 13 November, 2002 + 13 December, 2002 diff --git a/include/boost/python/enum.hpp b/include/boost/python/enum.hpp index 1070c06b..b1fac410 100644 --- a/include/boost/python/enum.hpp +++ b/include/boost/python/enum.hpp @@ -23,6 +23,9 @@ struct enum_ : public objects::enum_base // Add a new enumeration value with the given name and value. inline enum_& value(char const* name, T); + // Add all of the defined enumeration values to the current scope with the + // same names used here. + inline enum_& export_values(); private: static PyObject* to_python(void const* x); static void* convertible_from_python(PyObject* obj); @@ -86,6 +89,13 @@ inline enum_& enum_::value(char const* name, T x) return *this; } +template +inline enum_& enum_::export_values() +{ + this->enum_base::export_values(); + return *this; +} + }} // namespace boost::python #endif // ENUM_DWA200298_HPP diff --git a/include/boost/python/object/enum_base.hpp b/include/boost/python/object/enum_base.hpp index 37cd8f17..8e4a7301 100644 --- a/include/boost/python/object/enum_base.hpp +++ b/include/boost/python/object/enum_base.hpp @@ -25,6 +25,7 @@ struct BOOST_PYTHON_DECL enum_base : python::api::object , type_info); void add_value(char const* name, long value); + void export_values(); static PyObject* to_python(PyTypeObject* type, long x); }; diff --git a/src/object/enum.cpp b/src/object/enum.cpp index d151a486..c658ead2 100644 --- a/src/object/enum.cpp +++ b/src/object/enum.cpp @@ -12,6 +12,8 @@ #include #include #include +#include +#include #include #include @@ -194,6 +196,18 @@ void enum_base::add_value(char const* name_, long value) p->name = incref(name.ptr()); } +void enum_base::export_values() +{ + dict d = extract(this->attr("values"))(); + list values = d.values(); + scope current; + + for (unsigned i = 0, max = len(values); i < max; ++i) + { + api::setattr(current, object(values[i].attr("name")), values[i]); + } + } + PyObject* enum_base::to_python(PyTypeObject* type_, long x) { object type((type_handle(borrowed(type_)))); diff --git a/test/enum.cpp b/test/enum.cpp index b0dd02ae..fb1439c7 100644 --- a/test/enum.cpp +++ b/test/enum.cpp @@ -19,6 +19,7 @@ BOOST_PYTHON_MODULE(enum_ext) .value("red", red) .value("green", green) .value("blue", blue) + .export_values() ; def("identity", identity_); diff --git a/test/enum.py b/test/enum.py index e21f8eb8..00484670 100644 --- a/test/enum.py +++ b/test/enum.py @@ -20,6 +20,17 @@ enum_ext.color.green enum_ext.color(3) >>> identity(color(4)) +enum_ext.color.blue + + --- check export to scope --- + +>>> identity(red) +enum_ext.color.red + +>>> identity(green) +enum_ext.color.green + +>>> identity(blue) enum_ext.color.blue >>> try: identity(1) diff --git a/todo.txt b/todo.txt index 0123d9bd..e6a0f2d3 100644 --- a/todo.txt +++ b/todo.txt @@ -6,6 +6,7 @@ types FILE* conversions: http://aspn.activestate.com/ASPN/Mail/Message/1411366 +Finish shared_ptr support: http://aspn.activestate.com/ASPN/Mail/Message/c++-sig/1456023 Medium Priority: ----------------