Embedded Boost.Python Enum

Roman Yakovenko roman.yakovenko at gmail.com
Thu Oct 18 15:07:32 EDT 2007


On 10/18/07, Cory <Cory.Leach at gmail.com> wrote:
>
> Hi,
>
> I have a hopefully quick question about how to use Boost.Python to
> export an Enum.
> I am embedding python in C++ and am currently exporting my classes in
> the following way:
>
> nameSpace["OBJECT"] = class_<OBJECT>("OBJECT")
>         .def("getType", &OBJECT::getType)
>         .def("setSprite", &OBJECT::setSprite);
>
> So following this, I assumed that when exporting an enum the following
> should work:
>
> nameSpace["OBJECT_TYPE"] = enum_<OBJECT_TYPE>("OBJECT_TYPE")
>                         .value("GENERIC_OBJECT",GENERIC_OBJECT)
>                         .value("MAP_OBJECT",MAP_OBJECT)
>                         .value("TOTAL_OBJECT_TYPES",TOTAL_OBJECT_TYPES)
>                         .export_values();
>
> while the above compiles, it causes the following run time exception:
>
> AttributeError: 'NoneType' object has no attribute 'OBJECT_TYPE'
>
> I took a look at the documentation and the only explanation I found
> for enum appeared to be for extending python with modules. using the
> following form:
>
> BOOST_PYTHON_MODULE(enums)
> {
>     enum_<color>("color")
>         .value("red", red)
>         .value("green", green)
>         .export_values()
>         .value("blue", blue)
>         ;
>
> }


I think you should read next documentation:

http://boost.org/libs/python/doc/v2/scope.html

I COULD do the above, I would prefer the first method if possible. I
> however do not know how to import the module if it is statically
> linked because doing a simple import does not work and I am not
> familiar enough with the boost.python library, Python C API, or Python
> itself to know how to set it up. So My question is this:
>
> How can I either make the first method of adding an enum work and not
> throw the exception, OR once I create the BOOST_PYTHON_MODULE in an
> embedded python c++ program how to I then import that module into my
> embedded python?


http://boost.org/libs/python/doc/tutorial/doc/html/python/embedding.html
Embedding example:
http://svn.boost.org/trac/boost/browser/trunk/libs/python/test/import_.cpp

HTH

-- 
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20071018/eac1e321/attachment.html>


More information about the Python-list mailing list