Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I extract a wrapped C++ type from a Python type using boost::python?
    primarykey
    data
    text
    <p>I've wrapped a C++ class using Py++ and everything is working great in Python. I can instantiate the c++ class, call methods, etc.</p> <p>I'm now trying to embed some Python into a C++ application. This is also working fine for the most-part. I can call functions on a Python module, get return values, etc.</p> <p>The python code I'm calling returns one of the classes that I wrapped:</p> <pre><code>import _myextension as myext def run_script(arg): my_cpp_class = myext.MyClass() return my_cpp_class </code></pre> <p>I'm calling this function from C++ like this:</p> <pre><code>// ... excluding error checking, ref counting, etc. for brevity ... PyObject *pModule, *pFunc, *pArgs, *pReturnValue; Py_Initialize(); pModule = PyImport_Import(PyString_FromString("cpp_interface")); pFunc = PyObject_GetAttrString(pModule, "run_script"); pArgs = PyTuple_New(1); PyTuple_SetItem(pArgs, 0, PyString_FromString("an arg")); pReturnValue = PyObject_CallObject(pFunc, pArgs); bp::extract&lt; MyClass&amp; &gt; extractor(pReturnValue); // PROBLEM IS HERE if (extractor.check()) { // This check is always false MyClass&amp; cls = extractor(); } </code></pre> <p>The problem is the extractor never actually extracts/converts the PyObject* to MyClass (i.e. extractor.check() is always false).</p> <p>According to <a href="http://www.boost.org/doc/libs/1_47_0/libs/python/doc/tutorial/doc/html/python/object.html#python.extracting_c___objects" rel="nofollow">the docs</a> this is the correct way to extract a wrapped C++ class.</p> <p>I've tried returning basic data types (ints/floats/dicts) from the Python function and all of them are extracted properly.</p> <p>Is there something I'm missing? Is there another way to get the data and cast to MyClass?</p>
    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.
 

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