[C++-sig] ownership of C++ object extended in Python

2012-12-20 Thread Tiago Coutinho
Hello all,

I want to export a class 'A' from an external library to python.
In python I need to extend the class and I need to tell the library that it
will 'own' the object.

I have tried the example in:

http://wiki.python.org/moin/boost.python/HowTo

(chapter "ownership of C++ object")

And executing the code I send in attachment I get:

$ python test1.py

Traceback (most recent call last):
  File "test1.py", line 19, in 
main()
  File "test1.py", line 15, in main
print(a.get_int())
Boost.Python.ArgumentError: Python argument types in
A.get_int(B)
did not match C++ signature:
get_int(A {lvalue})

Can you tell me what I am missing in the code?

Thank you in advance

PS: Sorry quality of the Makefile.

Regards
Tiago


Makefile
Description: Binary data
// c++ -o test1 test1.cpp -lboost_python -I/usr/include/python2.7 -lpython2.7

#include 
#include 

using namespace std;
using namespace boost::python;

class A
{
public:
A() {}
virtual ~A() {}

int get_int() { return 22; }
};

class AWrap: public A
{
public:

PyObject *self;

AWrap(PyObject* self_): self(self_) { Py_INCREF(self); }
~AWrap() { Py_DECREF(self); }
};

class Owner
{
public:
A* a;
Owner(): a(0) {}
virtual ~Owner() { if (a!=0) delete a;}

void set_A(A* a_) { a=a_; }
};

void set_A(Owner& self, auto_ptr a_)
{
self.set_A(a_.get());
a_.release();
}

BOOST_PYTHON_MODULE(_test1)
{
class_, boost::noncopyable >("A")
.def("get_int", &A::get_int)
;
   
implicitly_convertible, auto_ptr >();

class_("Owner")
.def("set_A", &set_A)
;
}



test1.py
Description: Binary data
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig

Re: [C++-sig] Boost-Python Linking Problem with Hello World Example

2012-12-20 Thread Jaedyn K. Draper

  
  
I had this problem yesterday when I switched my python library to
Stackless, which I compiled myself. The problem is that python can
be built two ways - with 2-byte wide chars (USC2), or with 4-byte
wide chars (USC4). In my case it was the case that boost was trying
and failing to link to a USC4 build of python, but mine had built
with USC2. It looks like in your case it's trying and failing to
link to USC2.

If you built python yourself, try again, but in the configure
script, pass:

--enable-unicode=ucs2 --enable-shared

--enable-shared isn't related to this problem, but I found that when
embedding python into my C++ program, it would fail to import
several modules because of undefined symbols, even though it would
work fine running scripts as a standalone. It might work for you
without --enable-shared as well.

In my case I had to pass --enable-unicode=usc4 to get it to link, so
we seem to be having opposite problems. So this may or may not fix
it. But it's worth a shot.

On 12/19/2012 10:47 PM, Hesam wrote:


  
  
  Hello,
  I
am trying to compile and link boost-python hello world example
and I have some linking problems.
  OS:
Ubuntu
  g++ -fPIC -w Test2.cpp -I ../../../Libs/Python/Python-2.7.3/Include -I ../../../Libs/Python/Python-2.7.3 -I ../../../Libs/Boost/boost_1_52_0 -Wl,-rpath,../../../Libs/Python/Python-2.7.3/build/lib.linux-x86_64-2.7 -L -L../../../Libs/Python/Python-2.7.3/build/lib.linux-x86_64-2.7 -lssl -lcrypto -lpthread -lm -lutil -lpython2.7 -Wl,-rpath, -L../../../Libs/Boost/boost_1_52_0/lib -L../../../Libs/Boost/boost_1_52_0/stage/lib -lboost_python
  I
get the following Error
  ../../../Libs/Boost/boost_1_52_0/stage/lib/libboost_python.so: undefined reference to `PyUnicodeUCS2_AsWideChar'
../../../Libs/Boost/boost_1_52_0/stage/lib/libboost_python.so: undefined reference to `PyUnicodeUCS2_FromEncodedObject'
collect2: ld returned 1 exit status
make: *** [Test2] Error 1
  I
heard that it maybe due to incompatible version of python and
the python library used by Boost-python. But whatever I tried I
couldn't solve the problem. It is taking several days and no
success. Appreciate any comment.
  Thanks
  Hesam
  
  
  
  ___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig


  

___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig