Boost C++ Libraries: Ticket #6203: boost::python::api::object constructor failure https://svn.boost.org/trac10/ticket/6203 <p> Here is the C++ code for a Python extension module and a Python script that calls the module. Expected behavior when you run the script: </p> <pre class="wiki">The script runs and produces the output "Hello, I'm Y!" </pre><p> Observed behavior (with Boost 1.48.0, Python 2.7.2, MSVS 2010): </p> <pre class="wiki">The script fails with the error message: Traceback (most recent call last): File "../TestHello/TestHello.py", line 12, in &lt;module&gt; y = Hello.create('Y') TypeError: No to_python (by-value) converter found for C++ type: class Y Press any key to continue . . . </pre><p> I have also attached a zip file with the code as a solution for MSVS 2010 and Python Tools for Visual Studio. </p> <p> Hello.cpp: </p> <pre class="wiki">#include &lt;string&gt; #include &lt;exception&gt; #include &lt;boost/noncopyable.hpp&gt; #include &lt;boost/python.hpp&gt; using std::string; using boost::noncopyable; using boost::python::class_; using boost::python::def; class X : noncopyable { public: string hello() const { return "Hello, I'm X!"; } }; class Y : noncopyable { public: string hello() const { return "Hello, I'm Y!"; } }; class Z : noncopyable { public: string hello() const { return "Hello, I'm Z!"; } }; PyObject* create(char c) { if(c == 'X') return boost::python::api::object(new X).ptr(); else if(c == 'Y') return boost::python::api::object(new Y).ptr(); else if(c == 'Z') return boost::python::api::object(new Z).ptr(); else throw std::exception("The function argument must be 'X', 'Y' or 'Z'."); } BOOST_PYTHON_MODULE(Hello) { class_&lt;X, noncopyable&gt;("X").def("hello", &amp;X::hello); class_&lt;Y, noncopyable&gt;("Y").def("hello", &amp;Y::hello); class_&lt;Z, noncopyable&gt;("Z").def("hello", &amp;Z::hello); def("create", &amp;create); } </pre><p> Test.py: </p> <pre class="wiki">import Hello y = Hello.create('Y') print(y.hello()) </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/6203 Trac 1.4.3 Johan Råde <johan.rade@…> Sat, 03 Dec 2011 11:38:51 GMT attachment set https://svn.boost.org/trac10/ticket/6203 https://svn.boost.org/trac10/ticket/6203 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">Test.zip</span> </li> </ul> Ticket Johan Råde <johan.rade@…> Sat, 03 Dec 2011 11:46:58 GMT <link>https://svn.boost.org/trac10/ticket/6203#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6203#comment:1</guid> <description> <p> Additional info: I ran the code on Windows XP. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Ralf W. Grosse-Kunstleve</dc:creator> <pubDate>Sun, 04 Dec 2011 04:00:15 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/6203#comment:2 https://svn.boost.org/trac10/ticket/6203#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">invalid</span> </li> </ul> <p> This is not a bug. Construction of boost::python::object from a raw pointer is not supported. Try this (didn't try to compile this, but I tested the idea): </p> <pre class="wiki">boost::python::object create(char c) { if(c == 'X') return boost::python::object(boost::shared_ptr&lt;X&gt;(new X)); else if(c == 'Y') return boost::python::object(boost::shared_ptr&lt;Y&gt;(new Y)); else if(c == 'Z') return boost::python::object(boost::shared_ptr&lt;Z&gt;(new Z)); else throw std::exception("The function argument must be 'X', 'Y' or 'Z'."); } </pre><p> with </p> <pre class="wiki"> class_&lt;X, boost::shared_ptr&lt;X&gt;, noncopyable&gt;("X").def("hello", &amp;X::hello); </pre><p> etc. </p> Ticket Johan Råde <johan.rade@…> Sun, 04 Dec 2011 09:29:20 GMT <link>https://svn.boost.org/trac10/ticket/6203#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6203#comment:3</guid> <description> <p> Thank you! I have tested it and it works. </p> </description> <category>Ticket</category> </item> </channel> </rss>