Note that there are some explanatory texts on larger screens.

plurals
  1. POBoost.Python - How to re-enter module scope?
    primarykey
    data
    text
    <p>I'm exposing some C++ classes to Python that have nested <code>enum</code>'s. Looking at the example documentation at <a href="http://www.boost.org/doc/libs/1_48_0/libs/python/doc/tutorial/doc/html/python/object.html" rel="nofollow">boost.org</a> and <a href="http://wiki.python.org/moin/boost.python/module" rel="nofollow">wiki.python.org</a>, I can't see how to leave a scope once it has been entered, in order to go back to the global / module scope. Instead, each subsequent scope gets nested within the previous one.</p> <p>As an example:</p> <pre class="lang-cpp prettyprint-override"><code>#include &lt;boost/python.hpp&gt; class Foo { public: enum Choose { eFoo, eBar }; /* Default constructor with enum as required argument */ Foo(Choose choice): m_choice(choice) {} ~Foo() {} Choose get() const { return m_choice; } private: const Choose m_choice; }; class Bar { }; BOOST_PYTHON_MODULE(foo) { using namespace boost::python; scope global; /* Define Foo class, and a scope to go with it. */ scope in_Foo = class_&lt;Foo&gt; ("Foo", init&lt;Foo::Choose&gt;()) .def("rovalue", &amp;Foo::get) ; /* Expose 'Choose' enum as Foo.Choose */ enum_&lt;Foo::Choose&gt;("Choose") .value("Foo", Foo::eFoo) .value("Bar", Foo::eBar) ; /* How to get back to module scope?? */ global; scope(); /* This currently is exposed as Foo.Bar, but should just be Bar */ class_&lt;Bar&gt;("Bar", init&lt;&gt;()) ; } </code></pre> <p>I've tried changing that <code>global;</code> line to various things, but all to the same result:</p> <pre class="lang-bash prettyprint-override"><code>$ g++ -fPIC -shared scope.cpp -o foo.so -lpython2.7 -I/usr/include/python2.7 -lboost_python $ python -c 'import foo; print "Bar" in dir(foo)' False $ python -c 'import foo; print "Bar" in dir(foo.Foo)' True </code></pre> <p><strong>EDIT:</strong></p> <p>Having had another look at wiki.python.org, it would seem that in the above example, the correct answer would be to use <code>scope within(global)</code> to revert to the module level scope. Indeed, this works for the above example. Unfortunately however, I get a compile error when I use it in my actual application..</p> <pre><code>#include &lt;boost/python.hpp&gt; using namespace boost; BOOST_PYTHON_MODULE(foo) { python::scope module_level; /* .... */ python::scope python::within(module_level); /* ... */ } </code></pre> <p>Compile error:</p> <pre><code>error: invalid use of qualified-name 'boost::python::within' </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload