Re: [C++-sig] problem w/intermodule communication

2008-11-25 Thread William Ladwig
Neal,

Try adding:

register_ptr_to_python< boost::shared_ptr >();

to your BOOST_PYTHON_MODULE(test2) section.

Hope this helps,

Bill


From: [EMAIL PROTECTED] [EMAIL PROTECTED] On Behalf Of Neal Becker [EMAIL 
PROTECTED]
Sent: Monday, November 24, 2008 6:15 PM
To: cplusplus-sig@python.org
Subject: [C++-sig] problem w/intermodule communication

Here is a simple example.

---
test1.cc

#include 
#include 
#include 
#include 

using namespace boost::python;

struct A {
  A (int _x) : x (_x) {}
  int x;
};

BOOST_PYTHON_MODULE(test1) {
  class_ ("A", init());
}
--

test2.cc

#include 
#include 
#include 
#include 
#include 

using namespace boost::python;

struct A {
  A (int _x) : x (_x) {}
  int x;
};

struct B {
  B (boost::shared_ptr _a) : a (_a) {}
  boost::shared_ptr a;
};

BOOST_PYTHON_MODULE(test2) {
  class_ ("B", init >())
.def_readonly ("a", &B::a)
 ;
}
-

test12.py

from test1 import A
from test2 import B

a = A(1)
b = B(a)

print b.a
-

TypeError: No to_python (by-value) converter found for C++ type: 
boost::shared_ptr

How can I solve this?  Now consider:


test3.cc

#include 
#include 
#include 
#include 

using namespace boost::python;

struct A {
  A (int _x) : x (_x) {}
  int x;
};

struct B {
  B (boost::shared_ptr _a) : a (_a) {}
  boost::shared_ptr a;
};

BOOST_PYTHON_MODULE(test3) {
  class_ ("A", init());

  class_ ("B", init >())
.def_readonly ("a", &B::a)
 ;
}

-

test13.py

from test3 import A, B

a = A(1)
b = B(a)

print b.a
---


So if both are in same module, no problem.  If they are in different modules it 
doesn't work.  Any ideas?

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


Re: [C++-sig] Extended python system needs access to cPickle in c++

2009-05-05 Thread William Ladwig
You can import python modules in C++ using the import function.  See here:

http://www.boost.org/doc/libs/1_39_0/libs/python/doc/v2/import.html

You can also make your C++ extension classes 'pickleable' using boost python's 
pickle suite:

http://www.boost.org/doc/libs/1_39_0/libs/python/doc/v2/pickle.html


Bill

-Original Message-
From: cplusplus-sig-bounces+wladwig=wdtinc@python.org 
[mailto:cplusplus-sig-bounces+wladwig=wdtinc@python.org] On Behalf Of Simon 
Pickles
Sent: Tuesday, May 05, 2009 4:29 AM
To: cplusplus-sig@python.org
Subject: [C++-sig] Extended python system needs access to cPickle in c++

Hi,

I have an app with a python core, then c++ extension modules.

I'd like to be able to use cPickle to pack structures, especially 
boost::python::tuples, in c++.

Is there a way I can expose a python module in the c++ extensions?

I thought about passing a module as an arg to a c++ function, as a 
boost::python::object:

// cModule

void DoStuff(object pickleModule)
{
tuple t = make_tuple("Spam",42);
object pickleDumps = pickleModule.attr("dumps");
object s = pickleDumps(t);

// Send s to other process
}



# python
import cModule
import cPickle

cModule.DoStuff(cPickle)
#

Am I barking up the wrong tree?

Many thanks

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


Re: [C++-sig] operator+(A, B) without B::B() default constructor in boost.python

2009-05-19 Thread William Ladwig
It looks to me like you're missing a '()' next to your 'init<>' in the class 
wrapper for B.

Try changing this:

  class_("B",init)//line 19
;

to this:

  class_("B",init())//line 19
;

The only operator that has given me problems is the << operator.  I've had 
pretty good success with the others.

Regards,
Bill

-Original Message-
From: cplusplus-sig-bounces+wladwig=wdtinc@python.org 
[mailto:cplusplus-sig-bounces+wladwig=wdtinc@python.org] On Behalf Of Hans 
Roessler
Sent: Thursday, May 14, 2009 8:16 AM
To: cplusplus-sig@python.org
Subject: [C++-sig] operator+(A, B) without B::B() default constructor in 
boost.python


Hello,
how can I wrap the following overloaded operator+ using boost.python?
The code as is fails with 
>...
>pytest.cpp: In function 'void init_module_pytest()':
>pytest.cpp:19: error: expected primary-expression before '(' token
>...
If I try ".def(self+B())" instead of ".def(self+other())", the error is
>...
>pytest.cpp: In function 'void init_module_pytest()':
>pytest.cpp:17: error: no matching function for call to 'B::B()'
>...

best wishes
Hans


pytest.cpp:

#include 
using namespace boost::python;

class A{
public:
};
class B{
public:
  B(int i){};
};

int operator+(const A& a,const B& b){return 42;};

BOOST_PYTHON_MODULE(pytest)
{
  class_("A")
.def(self+other())   //.def(self+B())//line 17
;
  class_("B",init)//line 19
;
}


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


Re: [C++-sig] variable argument number and BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS

2009-05-21 Thread William Ladwig
I haven't tried this myself, but I think all you need to do to wrap the member 
function is this:

.def("info", (void(A::*)(int))0, A_info_overloads());

Hope this helps,
Bill

-Original Message-
From: cplusplus-sig-bounces+wladwig=wdtinc@python.org 
[mailto:cplusplus-sig-bounces+wladwig=wdtinc@python.org] On Behalf Of Hans 
Roessler
Sent: Thursday, May 21, 2009 8:40 AM
To: cplusplus-sig@python.org
Subject: [C++-sig] variable argument number and 
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS


Hello,
I try to wrap a class with a function info([int arg]) that takes either zero or 
one argument (exact colde below). For some reason I can't get 
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS to work.

The examples at 
http://www.boost.org/doc/libs/1_39_0/libs/python/doc/tutorial/doc/html/python/functions.html#python.overloading
 and
http://www.boost.org/doc/libs/1_39_0/libs/python/doc/v2/overloads.html#BOOST_PYTHON_FUNCTION_OVERLOADS-spec
 only show the situation where either
- the *member* function has up to three arguments with default values assigned 
or
- three *free* functions have one to three  arguments.

I don't find an example where a class has several *member* functions with the 
same name, that take a variable number of (non-default!) arguments.

My attempt below gives the error
"pytest.cpp:17: error: no matching function for call to 
'boost::python::class_::def(const char [5], , A_info_overloads)'"

Do I have to replace the ".def("info",&A::info, ..." with something like  
".def("foo", (void(*)(int,int))0, ..." as shown in the Auto-Overloading section 
in the tutorial? If yes, how must this function signature look like? It doesn't 
work like this.

Hope someone can help
Hans

=== pytest.py ===
#include 
#include 

using namespace boost::python;

class A{
public:
  void info() {printf("This is an A\n");}
  void info(int arg) {printf("Unnecessary arg.\n");}
};

BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(A_info_overloads, info, 0,1)

BOOST_PYTHON_MODULE(test)
{
  class_("A")
  .def("info",&A::info,A_info_overloads()) //line 17
;
}



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


Re: [C++-sig] variable argument number and BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS

2009-05-21 Thread William Ladwig
It's making a NULL pointer to a member function of A with an integer argument 
and a void return type.  

I'm not sure what's going on under the hood with why the NULL is required, but 
I suspect that it has something to do with the fact that 

void info();
void info(int i);

require two function pointers, but 

void info(int i=0) 

can only use one function pointer.  I suspect that they're trying to handle 
both cases with the same interface.  I'm just guessing though, so I could be 
wrong about this.

Regards,
Bill


-Original Message-
From: cplusplus-sig-bounces+wladwig=wdtinc@python.org 
[mailto:cplusplus-sig-bounces+wladwig=wdtinc@python.org] On Behalf Of Hans 
Roessler
Sent: Thursday, May 21, 2009 4:00 PM
To: Development of Python/C++ integration
Subject: Re: [C++-sig] variable argument number and 
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS


Thanks, ".def("info", (void(A::*)(int))0, A_info_overloads());" does it.

Could somebody explain what the meaning of this "(void(A::*)(int))0" construct 
is? Is it a function pointer? Why the 0?
(Just now I am fine with the proposed solution, but if I even understand it, 
you will read fewer stupid question from me in this list ;-)


Hans



- Ursprüngliche Mail 
> Von: William Ladwig 
> An: Development of Python/C++ integration 
> Gesendet: Donnerstag, den 21. Mai 2009, 18:30:46 Uhr
> Betreff: Re: [C++-sig] variable argument number and 
> BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS
> 
> I haven't tried this myself, but I think all you need to do to wrap the 
> member 
> function is this:
> 
> .def("info", (void(A::*)(int))0, A_info_overloads());
> 
> Hope this helps,
> Bill



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


Re: [C++-sig] wrapping operator=

2009-05-25 Thread William Ladwig
Generally how that situation is handled on the Python side is to use the 
container emulation API, so that assigning all values would look like this to a 
user of your class:

data[:] = 42.

To do this, you just need to write a small c++ wrapper to expose the 
__setitem__(self, key, value) function, which accepts (or extracts) a slice 
object for the key and then calls your class operator= function.  Or, you could 
just stick with the assign function that you created, which may be better if 
your class isn't supposed to look like a container.  This may actually be less 
error prone.

Here is the Python container emulation documentation:

http://docs.python.org/reference/datamodel.html#emulating-container-types

Other people may have better suggestions.  

Regards,
Bill


From: cplusplus-sig-bounces+wladwig=wdtinc@python.org 
[cplusplus-sig-bounces+wladwig=wdtinc@python.org] On Behalf Of Hans 
Roessler [hansroess...@yahoo.de]
Sent: Monday, May 25, 2009 9:08 AM
To: cplusplus-sig@python.org
Subject: [C++-sig] wrapping operator=

Hello!
I want to wrap with boost.python a C++ class "DataCube" which has overloaded 
the operator=.

The  constructor DataCube::DataCube(double x) allocates huge amounts of memory 
and fills it with x's.
The  DataCube::operator=(double x) just overwrites the already allocated memory 
with x's.

Now in C++ these commands first allocate memory, which is filled with 0.'s, and 
then overwrite the memory with 42.'s:
>DataCube data(0.)
>data=42.

In Python these commands first build the DataCube as desired, but then set 
data=42. (now data is a float), where the reference to the DataCube is lost:
>data=DataCube(0.)
>data=42.

I have circumvented this by replacing the second line with
> data.assign(42.)
with a function assign which is defined appropriately, but I would prefer to 
use the same assignment as in C++.

Finally the question:
Can I define the DataCube class in Python, so that the data variable above will 
behave as in C++, when I write "data=42." ?
In other words, is there any possibility in Python that "x=y" does NOT make x a 
reference to y?

Thank you
Hans




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


Re: [C++-sig] boost::python and threads

2009-07-04 Thread William Ladwig
It looks to me like you have a garbage collection problem going on.  If you 
create a wrapped c++ object in python, then python is going to own the object 
and will destroy it when its reference count goes to 0.  In your python example 
script at the bottom, you call the Ticker's run() function, which from the 
python point of view, returns quickly and the script ends.  Python has no idea 
that you spawned off a new thread from the C++ side, so when the script ends, 
python destroys the object and now you have a problem.  One way that you can 
check to see if this is what is going on is to add this to the bottom of the 
test script and see if the crashes go away:

# Warning, you'll need to kill this script manually
import time
while True:
time.sleep(1)

Generally when I want to fire off a new C++ thread, I hold any objects that the 
thread needs in an auto_ptr (see held type for class wrappers), take ownership 
of them in C++ (see the FAQ in the documentation) and start the thread from the 
C++ side.  Or, you can also create C++ wrappers which accept shared_ptr 
arguments, while holding your classes in shared_ptrs, and this should handle 
the reference counting as well.  Unfortunately, this may require some interface 
changes to what you have already written (or possibly some clever wrapping).  

Hope this helps,
Bill



From: cplusplus-sig-bounces+wladwig=wdtinc@python.org 
[cplusplus-sig-bounces+wladwig=wdtinc@python.org] On Behalf Of Paul Scruby 
[p...@gingernut.tv]
Sent: Friday, July 03, 2009 6:15 AM
To: cplusplus-sig@python.org
Subject: [C++-sig] boost::python and threads

I am having some problems using boost::python with boost::thread.  I'm using
threads because I want to run some tasks in the background when I'm using
the Python's interactive shell.  However, when I use get_override() to call
a Python method from  another boost::thread it crashes internally.  For
example:

#include 
#include 
#include 

using namespace boost::python;

class Ticker
:public wrapper
{
private:
bool run_;
volatile bool * running_;
boost::thread * thread_;
boost::xtime xt_;
public:
Ticker() : running_(&run_) { *running_ = false; }

void operator()()
{
while (*running_)
{
boost::xtime_get(&xt_, boost::TIME_UTC);
++xt_.sec;
boost::thread::sleep(xt_);
onTick();
}
}

void run()
{
if (*running_ == false)
{
*running_ = true;
thread_ = new boost::thread(*this);
}
}

void stop()
{
if (*running_ == true)
{
*running_ = false;
thread_->join();
delete thread_;
}
}

virtual void onTick() { get_override("onTick")(); }
void default_onTick() {}
};

BOOST_PYTHON_MODULE(tick)
{
class_ ("Ticker")
.def("run", &Ticker::run)
.def("stop", &Ticker::stop)
.def("onTick", &Ticker::default_onTick);
}

Here is a test script that which will crash when you import it into Python's
interactive shell.

from tick import Ticker

class MyTicker(Ticker):
def onTick(self):
print "Each second"

myticker = MyTicker()
myticker.run()

I ran this test initially on Python 2.4.4 with the Sun C++ 5.9 compiler on
Solaris and I also tested it using Python 2.6.2 with Visual Studio 2008 on
Windows XP.

The call-stack in dbx on Solaris:

>>> t...@2 (l...@2) signal SEGV (no mapping at the fault address) in
PyErr_Restore at 0xfef38fa1
0xfef38fa1: PyErr_Restore+0x0031:   movl 0x0028(%edi),%ecx
Current function is boost::python::override::operator()
   99   detail::method_result x(

(dbx) where
current thread: t...@2
  [1] PyErr_Restore(0x80652fc, 0x80f1220, 0x0, 0xfe77ee90, 0xfef3951e,
0x80652fc), at 0xfef38fa1
  [2] PyErr_SetObject(0x80652fc, 0x80f1220), at 0xfef3901e
  [3] PyErr_Format(0x80652fc, 0xfef5c2d8, 0xfef7902c), at 0xfef3951e
  [4] PyObject_Call(0xfef88768, 0x806102c, 0x0), at 0xfeee291a
  [5] PyEval_CallObjectWithKeywords(0xfef88768, 0x806102c, 0x0), at
0xfef2bf02
  [6] PyEval_CallFunction(0xfef88768, 0xfeb02004), at 0xfef434c5
=>[7] boost::python::override::operator()(this = 0xfe77ef30), line 99 in
"override.hpp"
  [8] Ticker::onTick(this = 0x810a304), line 48 in "ticker.cc"
  [9] Ticker::operator()(this = 0x810a304), line 25 in "ticker.cc"
  [10] boost::detail::thread_data::run(this = 0x810a288), line
56 in "thread.hpp"
  [11] thread_proxy(0x810a288), at 0xfea78ce4
  [12] _thr_setup(0xfe670200), at 0xfee159b9
  [13] _lwp_start(0xfe77ef54, 0x80f1220, 0xfe77ee7c, 0xfef3901e,
0x80652fc, 0x80f1220), at 0xfee15ca0

Re: [C++-sig] boost::python and threads

2009-07-04 Thread William Ladwig
Whoops, I think this problem is a little uglier than I thought, since you 
overrode the onTick() function in python with a call to print, which needs 
access to the interpreter in your new thread.  See the link Thomas posted for 
dealing with the GIL (along with worrying about any possible garbage collection 
issues).  Now I remember why I kept my C++ threads isolated from Python 
stuffsorry, it's been a while

Bill 

From: cplusplus-sig-bounces+wladwig=wdtinc@python.org 
[cplusplus-sig-bounces+wladwig=wdtinc@python.org] On Behalf Of William 
Ladwig [wlad...@wdtinc.com]
Sent: Saturday, July 04, 2009 1:34 PM
To: Development of Python/C++ integration
Subject: Re: [C++-sig] boost::python and threads

It looks to me like you have a garbage collection problem going on.  If you 
create a wrapped c++ object in python, then python is going to own the object 
and will destroy it when its reference count goes to 0.  In your python example 
script at the bottom, you call the Ticker's run() function, which from the 
python point of view, returns quickly and the script ends.  Python has no idea 
that you spawned off a new thread from the C++ side, so when the script ends, 
python destroys the object and now you have a problem.  One way that you can 
check to see if this is what is going on is to add this to the bottom of the 
test script and see if the crashes go away:

# Warning, you'll need to kill this script manually
import time
while True:
time.sleep(1)

Generally when I want to fire off a new C++ thread, I hold any objects that the 
thread needs in an auto_ptr (see held type for class wrappers), take ownership 
of them in C++ (see the FAQ in the documentation) and start the thread from the 
C++ side.  Or, you can also create C++ wrappers which accept shared_ptr 
arguments, while holding your classes in shared_ptrs, and this should handle 
the reference counting as well.  Unfortunately, this may require some interface 
changes to what you have already written (or possibly some clever wrapping).

Hope this helps,
Bill



From: cplusplus-sig-bounces+wladwig=wdtinc@python.org 
[cplusplus-sig-bounces+wladwig=wdtinc@python.org] On Behalf Of Paul Scruby 
[p...@gingernut.tv]
Sent: Friday, July 03, 2009 6:15 AM
To: cplusplus-sig@python.org
Subject: [C++-sig] boost::python and threads

I am having some problems using boost::python with boost::thread.  I'm using
threads because I want to run some tasks in the background when I'm using
the Python's interactive shell.  However, when I use get_override() to call
a Python method from  another boost::thread it crashes internally.  For
example:

#include 
#include 
#include 

using namespace boost::python;

class Ticker
:public wrapper
{
private:
bool run_;
volatile bool * running_;
boost::thread * thread_;
boost::xtime xt_;
public:
Ticker() : running_(&run_) { *running_ = false; }

void operator()()
{
while (*running_)
{
boost::xtime_get(&xt_, boost::TIME_UTC);
++xt_.sec;
boost::thread::sleep(xt_);
onTick();
}
}

void run()
{
if (*running_ == false)
{
*running_ = true;
thread_ = new boost::thread(*this);
}
}

void stop()
{
if (*running_ == true)
{
*running_ = false;
thread_->join();
delete thread_;
}
}

virtual void onTick() { get_override("onTick")(); }
void default_onTick() {}
};

BOOST_PYTHON_MODULE(tick)
{
class_ ("Ticker")
.def("run", &Ticker::run)
.def("stop", &Ticker::stop)
.def("onTick", &Ticker::default_onTick);
}

Here is a test script that which will crash when you import it into Python's
interactive shell.

from tick import Ticker

class MyTicker(Ticker):
def onTick(self):
print "Each second"

myticker = MyTicker()
myticker.run()

I ran this test initially on Python 2.4.4 with the Sun C++ 5.9 compiler on
Solaris and I also tested it using Python 2.6.2 with Visual Studio 2008 on
Windows XP.

The call-stack in dbx on Solaris:

>>> t...@2 (l...@2) signal SEGV (no mapping at the fault address) in
PyErr_Restore at 0xfef38fa1
0xfef38fa1: PyErr_Restore+0x0031:   movl 0x0028(%edi),%ecx
Current function is boost::python::override::operator()
   99   detail::method_result x(

(dbx) where
current thread: t...@2
  [1] PyErr_Restore(0x80652fc, 0x80f1220, 0x0, 0xfe77ee90, 0xfef3951e,
0x8065

Re: [C++-sig] Building with Scons

2009-07-06 Thread William Ladwig
Does the name of the module defined in your BOOST_PYTHON_MODULE section match 
the name of the .so file (assuming you are using Linux)?  That's usually the 
error I get when I have a name mismatch.  

Also, I haven't really used scons, but shouldn't this:

env.SharedLibrary(target='cylucene', source=FILES, SHLIBPREFIX='')

be:

env.SharedLibrary(target='cylucene', source=FILES, SHLIBPREFIX=''") ?


Bill



From: cplusplus-sig-bounces+wladwig=wdtinc@python.org 
[cplusplus-sig-bounces+wladwig=wdtinc@python.org] On Behalf Of Nicolas Lara 
[nicolasl...@gmail.com]
Sent: Monday, July 06, 2009 6:25 PM
To: Development of Python/C++ integration
Subject: [C++-sig] Building with Scons

Hello,

I am trying to build a python extension with scons but the resulting
library cannot be loaded:
   ImportError: dynamic module does not define init function (initcylucene)

I am using the following SConstruct file:

FILES = ['typeconversion.cpp', 'document.cpp', 'search.cpp', 'queryparser.cpp',
 'analysis.cpp', 'index.cpp', 'store.cpp', 'util.cpp']
def python_tool(env):
pybase = 'python%s' % sys.version[0:3]
env.Append(CPPPATH=[os.path.join(sys.prefix, 'include', pybase)],
   LIBPATH=[os.path.join(sys.prefix, 'lib', pybase, 'config')],
   LIBS=['lib%s' % pybase])
if env['PLATFORM'] not in ['cygwin', 'win32']:
env.Append(LIBS=['util'])

def boost_python_tool(env):
env.Append(CPPDEFINES=['BOOST_PYTHON_DYNAMIC_LIB',
   'BOOST_PYTHON_DYNAMIC_MODULE'],
   CPPPATH=['$boostIncludes'],  # boostIncludes is a PathOption
   LIBS=['boost_python'])

def clucene_tool(env):
env.Append(CPPPATH=['/usr/local/lib/CLucene/',
'/usr/local/lib/'],
   LIBS=['clucene'])

import os
env = Environment(ENV=os.environ, tools=['default', python_tool,
 boost_python_tool, clucene_tool])
env.SharedLibrary(target='cylucene', source=FILES, SHLIBPREFIX='')


Previously I was using the following Makefile, which worked but was
statically linked:

SHELL = /bin/bash
SRC = ./src/
BUILD = ./build/
INCLUDES = -I/usr/include/python2.6/ -I/usr/local/lib/ -I/usr/local/lib/CLucene/
LIBS = -lboost_python -lpython2.6 -lclucene
CFLAGS = -shared -fPIC -g -pedantic -Wall -Wextra
OBJECTS = document.o search.o queryparser.o analysis.o index.o store.o util.o
LOBJECTS = $(BUILD)typeconversion.o $(BUILD)document.o
$(BUILD)search.o $(BUILD)queryparser.o $(BUILD)analysis.o
$(BUILD)index.o $(BUILD)store.o $(BUILD)util.o

all: cylucene

cylucene: $(OBJECTS)
mkdir -p $(BUILD) && cp $(SRC)initfile.py $(BUILD)__init__.py
$(CC) $(SRC)cylucene.cpp $(LOBJECTS) $(INCLUDES) $(LIBS) $(CFLAGS) -o
$(BUILD)cylucene.so

document.o: typeconversion.o
mkdir -p $(BUILD)
$(CC) $(SRC)document.cpp $(INCLUDES) $(LIBS) $(CFLAGS) -o 
$(BUILD)document.o

queryparser.o: typeconversion.o
mkdir -p $(BUILD)
$(CC) $(SRC)queryparser.cpp $(INCLUDES) $(LIBS) $(CFLAGS) -o
$(BUILD)queryparser.o

analysis.o:
mkdir -p $(BUILD)
$(CC) $(SRC)analysis.cpp $(INCLUDES) $(LIBS) $(CFLAGS) -o 
$(BUILD)analysis.o

search.o:
mkdir -p $(BUILD)
$(CC) $(SRC)search.cpp $(INCLUDES) $(LIBS) $(CFLAGS) -o $(BUILD)search.o

index.o:
mkdir -p $(BUILD)
$(CC) $(SRC)index.cpp $(INCLUDES) $(LIBS) $(CFLAGS) -o $(BUILD)index.o

store.o:
mkdir -p $(BUILD)
$(CC) $(SRC)store.cpp $(INCLUDES) $(LIBS) $(CFLAGS) -o $(BUILD)store.o

util.o:
mkdir -p $(BUILD)
$(CC) $(SRC)util.cpp $(INCLUDES) $(LIBS) $(CFLAGS) -o $(BUILD)util.o

typeconversion.o:
mkdir -p $(BUILD)
$(CC) $(SRC)typeconversion.cpp $(CFLAGS) -o $(BUILD)typeconversion.o

clean:
rm -Rf build/


Does anyone have experience working with scons and boost::python? Can
anyone help?

Thanks in advance!


--
Nicolas Lara
Linux user #380134
http://nicolas-lara.blogspot.com/
Public key id: 0x152e7713 at http://subkeys.pgp.net/
___
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig
___
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig


Re: [C++-sig] Building with Scons

2009-07-06 Thread William Ladwig
Never mind, those single quotes looked like one double quote on my screen



From: cplusplus-sig-bounces+wladwig=wdtinc@python.org 
[cplusplus-sig-bounces+wladwig=wdtinc@python.org] On Behalf Of William 
Ladwig [wlad...@wdtinc.com]
Sent: Monday, July 06, 2009 9:53 PM
To: Development of Python/C++ integration
Subject: Re: [C++-sig] Building with Scons

Does the name of the module defined in your BOOST_PYTHON_MODULE section match 
the name of the .so file (assuming you are using Linux)?  That's usually the 
error I get when I have a name mismatch.

Also, I haven't really used scons, but shouldn't this:

env.SharedLibrary(target='cylucene', source=FILES, SHLIBPREFIX='')

be:

env.SharedLibrary(target='cylucene', source=FILES, SHLIBPREFIX=''") ?


Bill



From: cplusplus-sig-bounces+wladwig=wdtinc@python.org 
[cplusplus-sig-bounces+wladwig=wdtinc@python.org] On Behalf Of Nicolas Lara 
[nicolasl...@gmail.com]
Sent: Monday, July 06, 2009 6:25 PM
To: Development of Python/C++ integration
Subject: [C++-sig] Building with Scons

Hello,

I am trying to build a python extension with scons but the resulting
library cannot be loaded:
   ImportError: dynamic module does not define init function (initcylucene)

I am using the following SConstruct file:

FILES = ['typeconversion.cpp', 'document.cpp', 'search.cpp', 'queryparser.cpp',
 'analysis.cpp', 'index.cpp', 'store.cpp', 'util.cpp']
def python_tool(env):
pybase = 'python%s' % sys.version[0:3]
env.Append(CPPPATH=[os.path.join(sys.prefix, 'include', pybase)],
   LIBPATH=[os.path.join(sys.prefix, 'lib', pybase, 'config')],
   LIBS=['lib%s' % pybase])
if env['PLATFORM'] not in ['cygwin', 'win32']:
env.Append(LIBS=['util'])

def boost_python_tool(env):
env.Append(CPPDEFINES=['BOOST_PYTHON_DYNAMIC_LIB',
   'BOOST_PYTHON_DYNAMIC_MODULE'],
   CPPPATH=['$boostIncludes'],  # boostIncludes is a PathOption
   LIBS=['boost_python'])

def clucene_tool(env):
env.Append(CPPPATH=['/usr/local/lib/CLucene/',
'/usr/local/lib/'],
   LIBS=['clucene'])

import os
env = Environment(ENV=os.environ, tools=['default', python_tool,
 boost_python_tool, clucene_tool])
env.SharedLibrary(target='cylucene', source=FILES, SHLIBPREFIX='')


Previously I was using the following Makefile, which worked but was
statically linked:

SHELL = /bin/bash
SRC = ./src/
BUILD = ./build/
INCLUDES = -I/usr/include/python2.6/ -I/usr/local/lib/ -I/usr/local/lib/CLucene/
LIBS = -lboost_python -lpython2.6 -lclucene
CFLAGS = -shared -fPIC -g -pedantic -Wall -Wextra
OBJECTS = document.o search.o queryparser.o analysis.o index.o store.o util.o
LOBJECTS = $(BUILD)typeconversion.o $(BUILD)document.o
$(BUILD)search.o $(BUILD)queryparser.o $(BUILD)analysis.o
$(BUILD)index.o $(BUILD)store.o $(BUILD)util.o

all: cylucene

cylucene: $(OBJECTS)
mkdir -p $(BUILD) && cp $(SRC)initfile.py $(BUILD)__init__.py
$(CC) $(SRC)cylucene.cpp $(LOBJECTS) $(INCLUDES) $(LIBS) $(CFLAGS) -o
$(BUILD)cylucene.so

document.o: typeconversion.o
mkdir -p $(BUILD)
$(CC) $(SRC)document.cpp $(INCLUDES) $(LIBS) $(CFLAGS) -o 
$(BUILD)document.o

queryparser.o: typeconversion.o
mkdir -p $(BUILD)
$(CC) $(SRC)queryparser.cpp $(INCLUDES) $(LIBS) $(CFLAGS) -o
$(BUILD)queryparser.o

analysis.o:
mkdir -p $(BUILD)
$(CC) $(SRC)analysis.cpp $(INCLUDES) $(LIBS) $(CFLAGS) -o 
$(BUILD)analysis.o

search.o:
mkdir -p $(BUILD)
$(CC) $(SRC)search.cpp $(INCLUDES) $(LIBS) $(CFLAGS) -o $(BUILD)search.o

index.o:
mkdir -p $(BUILD)
$(CC) $(SRC)index.cpp $(INCLUDES) $(LIBS) $(CFLAGS) -o $(BUILD)index.o

store.o:
mkdir -p $(BUILD)
$(CC) $(SRC)store.cpp $(INCLUDES) $(LIBS) $(CFLAGS) -o $(BUILD)store.o

util.o:
mkdir -p $(BUILD)
$(CC) $(SRC)util.cpp $(INCLUDES) $(LIBS) $(CFLAGS) -o $(BUILD)util.o

typeconversion.o:
mkdir -p $(BUILD)
$(CC) $(SRC)typeconversion.cpp $(CFLAGS) -o $(BUILD)typeconversion.o

clean:
rm -Rf build/


Does anyone have experience working with scons and boost::python? Can
anyone help?

Thanks in advance!


--
Nicolas Lara
Linux user #380134
http://nicolas-lara.blogspot.com/
Public key id: 0x152e7713 at http://subkeys.pgp.net/
___
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig
___
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig
___
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig


Re: [C++-sig] Building with Scons

2009-07-07 Thread William Ladwig
You definitely want the name of the extension .so file to be cylucene.so, so 
you will want to drop the 'lib' prefix.  Is your BOOST_PYTHON_MODULE contained 
in a file named cylucene.cpp?  It looks like you are using a cylucene.cpp file 
in the final step of your makefile, but it is not in your list of "FILES" for 
your scons script, so I don't think it is ever getting compiled and linked 
(that would be consistent with the error you are getting).  If that's the case, 
you can probably just add it to the FILES list and everything will come to life.

Also, here is a link from the boost.python howto which has an example of 
building with scons:

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

Bill



From: cplusplus-sig-bounces+wladwig=wdtinc@python.org 
[cplusplus-sig-bounces+wladwig=wdtinc@python.org] On Behalf Of Nicolas Lara 
[nicolasl...@gmail.com]
Sent: Tuesday, July 07, 2009 7:54 AM
To: Development of Python/C++ integration
Subject: Re: [C++-sig] Building with Scons

Thanks for the reply,

Yes, I checked the name of the module. It matches the name of the
generated file. I also tried changing the name to include "lib" (since
scons also generates called libcylucene.so) but it doesnt work.
My module looks like this:

void init_util();

BOOST_PYTHON_MODULE(cylucene)
{
   init_util();
}

On Tue, Jul 7, 2009 at 2:53 AM, William Ladwig wrote:
> Does the name of the module defined in your BOOST_PYTHON_MODULE section match 
> the name of the .so file (assuming you are using Linux)?  That's usually the 
> error I get when I have a name mismatch.
>
> Also, I haven't really used scons, but shouldn't this:
>
> env.SharedLibrary(target='cylucene', source=FILES, SHLIBPREFIX='')
>
> be:
>
> env.SharedLibrary(target='cylucene', source=FILES, SHLIBPREFIX=''") ?
>
>
> Bill
>
>
> 
> From: cplusplus-sig-bounces+wladwig=wdtinc@python.org 
> [cplusplus-sig-bounces+wladwig=wdtinc@python.org] On Behalf Of Nicolas 
> Lara [nicolasl...@gmail.com]
> Sent: Monday, July 06, 2009 6:25 PM
> To: Development of Python/C++ integration
> Subject: [C++-sig] Building with Scons
>
> Hello,
>
> I am trying to build a python extension with scons but the resulting
> library cannot be loaded:
>   ImportError: dynamic module does not define init function (initcylucene)
>
> I am using the following SConstruct file:
>
> FILES = ['typeconversion.cpp', 'document.cpp', 'search.cpp', 
> 'queryparser.cpp',
> 'analysis.cpp', 'index.cpp', 'store.cpp', 'util.cpp']
> def python_tool(env):
>pybase = 'python%s' % sys.version[0:3]
>env.Append(CPPPATH=[os.path.join(sys.prefix, 'include', pybase)],
>   LIBPATH=[os.path.join(sys.prefix, 'lib', pybase, 'config')],
>   LIBS=['lib%s' % pybase])
>if env['PLATFORM'] not in ['cygwin', 'win32']:
>env.Append(LIBS=['util'])
>
> def boost_python_tool(env):
>env.Append(CPPDEFINES=['BOOST_PYTHON_DYNAMIC_LIB',
>   'BOOST_PYTHON_DYNAMIC_MODULE'],
>   CPPPATH=['$boostIncludes'],  # boostIncludes is a PathOption
>   LIBS=['boost_python'])
>
> def clucene_tool(env):
>env.Append(CPPPATH=['/usr/local/lib/CLucene/',
>'/usr/local/lib/'],
>   LIBS=['clucene'])
>
> import os
> env = Environment(ENV=os.environ, tools=['default', python_tool,
> boost_python_tool, clucene_tool])
> env.SharedLibrary(target='cylucene', source=FILES, SHLIBPREFIX='')
>
>
> Previously I was using the following Makefile, which worked but was
> statically linked:
>
> SHELL = /bin/bash
> SRC = ./src/
> BUILD = ./build/
> INCLUDES = -I/usr/include/python2.6/ -I/usr/local/lib/ 
> -I/usr/local/lib/CLucene/
> LIBS = -lboost_python -lpython2.6 -lclucene
> CFLAGS = -shared -fPIC -g -pedantic -Wall -Wextra
> OBJECTS = document.o search.o queryparser.o analysis.o index.o store.o util.o
> LOBJECTS = $(BUILD)typeconversion.o $(BUILD)document.o
> $(BUILD)search.o $(BUILD)queryparser.o $(BUILD)analysis.o
> $(BUILD)index.o $(BUILD)store.o $(BUILD)util.o
>
> all: cylucene
>
> cylucene: $(OBJECTS)
>mkdir -p $(BUILD) && cp $(SRC)initfile.py $(BUILD)__init__.py
>$(CC) $(SRC)cylucene.cpp $(LOBJECTS) $(INCLUDES) $(LIBS) $(CFLAGS) -o
> $(BUILD)cylucene.so
>

Re: [C++-sig] Boost.Python: same class in several modules

2009-07-14 Thread William Ladwig
Are you using the static or dynamic version of the boost python library?  This 
looks similar to a problem a coworker had and switching to the dynamic version 
of the library fixed his problem.  According to the documentation, the dynamic 
version of the library "contains a type conversion registry. Because one 
registry is shared among all extension modules, instances of a class exposed to 
Python in one dynamically-loaded extension module can be passed to functions 
exposed in another such module."

Also, if you organize your extension modules to be used within packages, you 
can use __init__.py magic to load any extension classes before using them.  I 
had to do this once when I defined a base class in one extension module and 
subclassed it in another.  I believe the dynamic version of the boost python 
library is required for this to work.

Regards, 

Bill



-Original Message-
From: cplusplus-sig-bounces+wladwig=wdtinc@python.org 
[mailto:cplusplus-sig-bounces+wladwig=wdtinc@python.org] On Behalf Of Rock 
Lobster
Sent: Tuesday, July 14, 2009 9:48 AM
To: cplusplus-sig@python.org
Subject: [C++-sig] Boost.Python: same class in several modules


Hello,

I tried to do the following:
I've got two Python modules which are both wrapped by boost.python, and both
of them share some header files, so there are several classes which are used
by both modules.

As an easy example:
- first module is called "videolib" and second module is called "videofx"
- both modules use a class called "VideoFile", which is inside a single
"videofile.h" (included by both modules)
- videolib module has a function that returns a VideoFile*.
- videofx module has a class with a method that accepts VideoFile* as a
parameter.

Now I'd like to use both modules in Python, and call e.g.
videolib.createVideoFile() and then use the returned object to put it into
the, let's say, videofx.doSomethingWith(vf) method.

But the problem is that Python doesn't know that both VideoFile* types are
exactly the same, so he says "Python argument types did not match C++
signature".

Is there anything I can do to manage this situation? Or would I have to
re-engineer the library structures?

Nice greetings and thanks in advance
Chris
-- 
View this message in context: 
http://www.nabble.com/Boost.Python%3A-same-class-in-several-modules-tp24479797p24479797.html
Sent from the Python - c++-sig mailing list archive at Nabble.com.

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


Re: [C++-sig] wrapping reference members

2009-07-22 Thread William Ladwig
You may need to create a getter and a setter in your wrapper, and then you can 
use the "make_function" function to change the CallPolicy when you use create a 
property.  I think you want your call policy to be return_internal_reference<> 
and that will handle the lifetime correctly.  Here is a reference link that 
that technique:

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

There may be an easier solution to your problem, but I know that the technique 
above will work.

Hope this helps,

Bill

-Original Message-
From: cplusplus-sig-bounces+wladwig=wdtinc@python.org 
[mailto:cplusplus-sig-bounces+wladwig=wdtinc@python.org] On Behalf Of 
Sebastian Kraemer
Sent: Wednesday, July 22, 2009 9:57 AM
To: cplusplus-sig@python.org
Subject: Re: [C++-sig] wrapping reference members

> I would think that .def_readwrite would do what you need. If it doesn't
> look at using .def_property and get/set pair. Caveat: making sure that
> 'a' stays a valid reference is your problem. Python will happily crash
> horribly (if you are lucky) in response to bogus pointers or references.
Thanks for your fast answer! I tried it with following code:

class_("Foo", init<>())
.def_readwrite("a", &Foo::a)
;

But it raises the error:

error: cannot create pointer to reference member 'Foo::a'

And it seems it's really not possible to create a pointer to a reference in c++ 
;) So what else do I have to write instead of "&Foo::a"?

Cheers,
Sebastian

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


Re: [C++-sig] function with >15 args yields get_signature error

2009-10-26 Thread William Ladwig
There is a config macro BOOST_PYTHON_MAX_ARITY which may work for you.  I'm not 
sure how high you can increase this number though.  Refer to:

http://www.boost.org/doc/libs/1_40_0/libs/python/doc/v2/configuration.html



-Original Message-
From: cplusplus-sig-bounces+wladwig=wdtinc@python.org 
[mailto:cplusplus-sig-bounces+wladwig=wdtinc@python.org] On Behalf Of Eilif 
Mueller
Sent: Monday, October 26, 2009 12:02 PM
To: cplusplus-sig@python.org
Subject: [C++-sig] function with >15 args yields get_signature error

Hi,

Wrapping a function f with 16 arguments:

int f(int x1, int x2, int x3, int x4, int x5,
  int x6, int x7, int x8, int x9, int x10,
  int x11, int x12, int x13, int x14, int x15,
  int x16) {

  return x1;

}

BOOST_PYTHON_MODULE(test)
{

  def("f",f);

}


yields

/usr/include/boost/python/make_function.hpp: In function 
'boost::python::api::object boost::python::make_function(F) [with F = int 
(*)(int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, 
int)]':
/usr/include/boost/python/def.hpp:82:   instantiated from 
'boost::python::api::object boost::python::detail::make_function1(T, ...) [with 
T = int (*)(int, int, int, int, int, int, int, int, int, int, int, int, int, 
int, int, int)]'
/usr/include/boost/python/def.hpp:91:   instantiated from 'void 
boost::python::def(const char*, Fn) [with Fn = int (*)(int, int, int, int, int, 
int, int, int, int, int, int, int, int, int, int, int)]'
test_module.cpp:20:   instantiated from here
/usr/include/boost/python/make_function.hpp:104: error: no matching function 
for call to 'get_signature(int (*&)(int, int, int, int, int, int, int, int, 
int, int, int, int, int, int, int, int))'
make: *** [stream.o] Error 1


whereas all is fine if that last arg "int x16" is removed.  All of gcc-4.2, 
gcc-4.3 and gcc-4.4 seem to exhibit the same behaviour.  Same effect for 
libboost-python1.38-dev on Ubuntu karmic and libboost-python1.35-dev on Ubuntu 
jaunty.

I need that 16th arg and more ... about 32 args, I think.

Thanks for any help you can offer.

cheers,

Eilif





 
-- 
Jetzt kostenlos herunterladen: Internet Explorer 8 und Mozilla Firefox 3.5 -
sicherer, schneller und einfacher! http://portal.gmx.net/de/go/atbrowser

-- 
Jetzt kostenlos herunterladen: Internet Explorer 8 und Mozilla Firefox 3.5 -
sicherer, schneller und einfacher! http://portal.gmx.net/de/go/atbrowser
___
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig
___
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig