[prev in list] [next in list] [prev in thread] [next in thread] 

List:       python-cpp-sig
Subject:    Re: [C++-sig] boost::python on Linux
From:       Jim Treadway <jimtreadway () gmail ! com>
Date:       2009-06-23 19:17:19
Message-ID: efdaede40906231217h2481e2adpd0bf2f14f661cd0a () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


On Tue, Jun 23, 2009 at 12:06 PM, Stefan Seefeld <seefeld@sympatico.ca>wrote:

> On 06/23/2009 02:45 PM, Jim Treadway wrote:
>
>> I'm having trouble getting a simple boost::python sample program to
>> work properly on Linux.  It compiles and seems to link properly, but the
>> Python
>> interpreter is unable to call my C++ function.  On Mac OS X the
>> program works as expected.
>>
>> Any help would be appreciated, hopefully I'm missing something obvious.
>>
>>
>
>
> Let's see:
>
> I expect the problem to be that the Python interpreter doesn't 'see' the
> 'Test' module. While normally this would just result in an import error,
> here it may just happen  to find a different 'Test' module, which, as it
> doesn't match what you expect, raises an ArgumentError.
>

Renaming everything from test to "MyUniqueTest" produces the same effective
results.

 -- BEGIN --
> #include<stdlib.h>
> #include<string>
> #include<boost/python.hpp>
>
> using namespace boost::python;
>
> /* g++ -I/usr/include/python2.5 -lpython2.5 -lboost_python -Wall -o
> test test.cpp */
>
> int test(int i)
> {
>        fprintf(stderr, "%s:\n", __FUNCTION__);
>        return i * 5;
> }
>
> BOOST_PYTHON_MODULE(Test)
> {
>        using namespace boost::python;
>        def("test", test);
> }
>
> int main(int argc, char *argv[])
> {
>        Py_Initialize();
>
>        try {
>                initTest();
>


I'm actually not sure what the effect of calling 'initTest()' is.
> I believe you should call:
>
>  PyImport_AppendInittab(const_cast<char*>("Test"), initTest);


My understanding is that PyImport_AppendInittab makes it so that a Python
"import" statement will call your initialization function so that you do not
have to call "initTest" (for example) from the C++ code.

Nevertheless, using PyImport_AppendInittab instead, in both the location you
recommend as well as before the call to Py_Initialize() produces no change
in the results.

                PyRun_SimpleString("import Test");
>                PyRun_SimpleString("print 'calling test function'");
>                PyRun_SimpleString("print Test.test(5)");
>        } catch (boost::python::error_already_set) {
>                PyErr_Print();
>        }
>
>        Py_Finalize();
>        return 0;
> }
> -- END --


I've also tried:

    PyRun_SimpleString("import sys, dl");
    PyRun_SimpleString("sys.setdlopenflags(dl.RTLD_NOW | dl.RTLD_GLOBAL);

before the "import Test" part, but that doesn't change the result either.

Jim

>

[Attachment #5 (text/html)]

<div class="gmail_quote">On Tue, Jun 23, 2009 at 12:06 PM, Stefan Seefeld <span \
dir="ltr">&lt;<a href="mailto:seefeld@sympatico.ca">seefeld@sympatico.ca</a>&gt;</span> \
wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, \
204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"> <div class="im">On 06/23/2009 \
02:45 PM, Jim Treadway wrote:<br> <blockquote class="gmail_quote" style="border-left: \
1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"> I&#39;m \
having trouble getting a simple boost::python sample program to<br> work properly on \
Linux.  It compiles and seems to link properly, but the Python<br> interpreter is \
unable to call my C++ function.  On Mac OS X the<br> program works as expected.<br>
<br>
Any help would be appreciated, hopefully I&#39;m missing something obvious.<br>
   <br>
</blockquote>
<br>
<br></div>
Let&#39;s see:<br>
<br>
I expect the problem to be that the Python interpreter doesn&#39;t &#39;see&#39; the \
&#39;Test&#39; module. While normally this would just result in an import error, here \
it may just happen  to find a different &#39;Test&#39; module, which, as it \
doesn&#39;t match what you expect, raises an ArgumentError.<div class="im"> \
</div></blockquote><div class="im"><br>Renaming everything from test to \
&quot;MyUniqueTest&quot; produces the same effective results.<br> <br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); \
                margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
-- BEGIN --<br>
#include&lt;stdlib.h&gt;<br>
#include&lt;string&gt;<br>
#include&lt;boost/python.hpp&gt;<br>
<br>
using namespace boost::python;<br>
<br>
/* g++ -I/usr/include/python2.5 -lpython2.5 -lboost_python -Wall -o<br>
test test.cpp */<br>
<br>
int test(int i)<br>
{<br>
        fprintf(stderr, &quot;%s:\n&quot;, __FUNCTION__);<br>
        return i * 5;<br>
}<br>
<br>
BOOST_PYTHON_MODULE(Test)<br>
{<br>
        using namespace boost::python;<br>
        def(&quot;test&quot;, test);<br>
}<br>
<br>
int main(int argc, char *argv[])<br>
{<br>
        Py_Initialize();<br>
<br>
        try {<br>
                initTest();  <br>
</blockquote>
<br>
<br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, \
204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"> I&#39;m actually not sure what \
the effect of calling &#39;initTest()&#39; is.<br> I believe you should call:<br>
<br>
  PyImport_AppendInittab(const_cast&lt;char*&gt;(&quot;Test&quot;), \
initTest);</blockquote><div class="im"><br>My understanding is that \
PyImport_AppendInittab makes it so that a Python &quot;import&quot; statement will \
call your initialization function so that you do not have to call \
&quot;initTest&quot; (for example) from the C++ code.<br> <br>Nevertheless, using \
PyImport_AppendInittab instead, in both the location you recommend as well as before \
the call to Py_Initialize() produces no change in the results. <br><br> <blockquote \
class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt \
0pt 0.8ex; padding-left: 1ex;">  PyRun_SimpleString(&quot;import Test&quot;);<br>
                PyRun_SimpleString(&quot;print &#39;calling test \
function&#39;&quot;);<br>  PyRun_SimpleString(&quot;print Test.test(5)&quot;);<br>
        } catch (boost::python::error_already_set) {<br>
                PyErr_Print();<br>
        }<br>
<br>
        Py_Finalize();<br>
        return 0;<br>
}<br>
-- END --</blockquote><div><br>I&#39;ve also tried:<br><br>    \
PyRun_SimpleString(&quot;import sys, dl&quot;);<br>    \
PyRun_SimpleString(&quot;sys.setdlopenflags(dl.RTLD_NOW | \
dl.RTLD_GLOBAL);<br><br>before the &quot;import Test&quot; part, but that doesn&#39;t \
change the result either.<br> <br>Jim<br></div>
</div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, \
204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"> <font color="#888888">
</font></blockquote></div>



_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic