Re: [C++-sig] boost.python: no overflow checking for unsigned int parameters ?

2009-06-23 Thread Ralf W. Grosse-Kunstleve

Thanks for your work! I'm out of town, back in a couple weeks. I'll integrate 
your
changes when I'm back (unless anyone else gets to it before I'm back).
Ralf



- Original Message 
From: Anderson Lizardo 
To: Development of Python/C++ integration 
Sent: Thursday, June 18, 2009 6:08:20 AM
Subject: Re: [C++-sig] boost.python: no overflow checking for unsigned int 
parameters ?

On Mon, Jun 15, 2009 at 4:23 PM, Ralf W. Grosse-Kunstleve wrote:
>
> If you send me a patch (against the boost trunk) I'll test it and check it in.
> Probably, the file to be changed is
>  boost/libs/python/src/converter/builtin_converters.cpp
> I'd try working with
>  boost/numeric/conversion/cast.hpp

Done. See https://svn.boost.org/trac/boost/ticket/3189 and the patch
attached to it.

Note the patch still lacks the addition of a test to the test suite.
I'm working on it, but you can test it in advance.

Regards,
-- 
Anderson Lizardo
OpenBossa Labs - INdT
Manaus - Brazil

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


[C++-sig] Pybindgen/pygccxml integration

2009-06-23 Thread Ben Fitzpatrick

Hi everyone,

I'm just starting to check out pybindgen after the messages I've seen 
floating around on this mailing list, and I have a question about the 
pygccxml integration. I've made a pure-virtual class with a pointer 
return type, like so:


class pure_virtual_class
{
   virtual int get_value(int* value)=0;
   virtual int put_value(int value)=0;

   static pure_virtual_class* Create();
};

Normally I'd call add_function('get_value', retval(int), [param('int *', 
'value', transfer_ownership=True)]) (I think)
But since I'm scanning the headers with gccxml, I'm not sure how to tell 
it to transfer the ownership.
Right now it's giving me a TypeConfigurationError during 'parse', which 
appears to be because it doesn't know how to deal with the ownership.


I looked in the documentation and couldn't find anything referencing 
this, and none of the examples use gccxml.


Any ideas?

Thanks,
Ben Fitzpatrick

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


Re: [C++-sig] Pybindgen/pygccxml integration

2009-06-23 Thread Gustavo Carneiro
2009/6/23 Ben Fitzpatrick 

> Hi everyone,
>
> I'm just starting to check out pybindgen after the messages I've seen
> floating around on this mailing list, and I have a question about the
> pygccxml integration. I've made a pure-virtual class with a pointer return
> type, like so:
>
> class pure_virtual_class
> {
>   virtual int get_value(int* value)=0;
>   virtual int put_value(int value)=0;
>
>   static pure_virtual_class* Create();
> };
>
> Normally I'd call add_function('get_value', retval(int), [param('int *',
> 'value', transfer_ownership=True)]) (I think)
> But since I'm scanning the headers with gccxml, I'm not sure how to tell it
> to transfer the ownership.
> Right now it's giving me a TypeConfigurationError during 'parse', which
> appears to be because it doesn't know how to deal with the ownership.
>
> I looked in the documentation and couldn't find anything referencing this,
> and none of the examples use gccxml.
>
> Any ideas?


Yes, there are two possible (alternative) ways to deal with this:

  1. Add inline annotations as C++ comments in the header file to be
scanned;
  2. Add scanning hooks to inject annotations via a function callback;

For inline annotations, see tests/foo.h, look for the comment lines
containing -#-.  I must warn you, though, that annotations are kind of a
"brittle" system.  The annotations are searched upwards starting in the line
immediately before the line that gccxml reports as the line the declaration
is defined.  The problem here is that gccxml reports the line of a
declaration sometimes in weird and unexpected places.

The other way is more complicated in terms of code, but doable with some
pacience.  You can see an example in ns-3 [1], in the pre_scan_hook
function.


[1]
http://code.nsnam.org/ns-3-dev/file/96811f76c3e2/bindings/python/ns3modulescan.py

I hope this helps.

-- 
Gustavo J. A. M. Carneiro
INESC Porto, Telecommunications and Multimedia Unit
"The universe is always one step beyond logic." -- Frank Herbert
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig

Re: [C++-sig] Pybindgen/pygccxml integration

2009-06-23 Thread Ben Fitzpatrick

Thanks for the suggestions!

I tried the first one, just as a test. I'd like to do the second 
programatically if I can, but I just wanted to make sure annotations 
were going to fix this. It still seems to be giving me the same error:
(...)/pure_virtual.h:5: WrapperWarning: Parameter 'int * value' error 
(used in int pure_virtual_class::get_value(int * value) [member 
function]): at 0xb780326c>

 virtual int get_value(int* value) = 0;

Here is what I changed my code to:

   // -#- @value(transfer_ownership=true) -#-
  virtual int get_value(int* value)=0;

This appears to be similar to the declaration you pointed me to in foo.h:
// -#- @zbr(transfer_ownership=true) -#-
void store_zbr (Zbr *zbr);

Also tried renaming it to 'foobaz' in case 'value' was a keyword. Is 
there something I'm missing?


Thanks,
Ben Fitzpatrick


Gustavo Carneiro wrote:



2009/6/23 Ben Fitzpatrick >


Hi everyone,

I'm just starting to check out pybindgen after the messages I've
seen floating around on this mailing list, and I have a question
about the pygccxml integration. I've made a pure-virtual class
with a pointer return type, like so:

class pure_virtual_class
{
  virtual int get_value(int* value)=0;
  virtual int put_value(int value)=0;

  static pure_virtual_class* Create();
};

Normally I'd call add_function('get_value', retval(int),
[param('int *', 'value', transfer_ownership=True)]) (I think)
But since I'm scanning the headers with gccxml, I'm not sure how
to tell it to transfer the ownership.
Right now it's giving me a TypeConfigurationError during 'parse',
which appears to be because it doesn't know how to deal with the
ownership.

I looked in the documentation and couldn't find anything
referencing this, and none of the examples use gccxml.

Any ideas?


Yes, there are two possible (alternative) ways to deal with this:

  1. Add inline annotations as C++ comments in the header file to be 
scanned;

  2. Add scanning hooks to inject annotations via a function callback;

For inline annotations, see tests/foo.h, look for the comment lines 
containing -#-.  I must warn you, though, that annotations are kind of 
a "brittle" system.  The annotations are searched upwards starting in 
the line immediately before the line that gccxml reports as the line 
the declaration is defined.  The problem here is that gccxml reports 
the line of a declaration sometimes in weird and unexpected places.


The other way is more complicated in terms of code, but doable with 
some pacience.  You can see an example in ns-3 [1], in the 
pre_scan_hook function.



[1] 
http://code.nsnam.org/ns-3-dev/file/96811f76c3e2/bindings/python/ns3modulescan.py


I hope this helps.

--
Gustavo J. A. M. Carneiro
INESC Porto, Telecommunications and Multimedia Unit
"The universe is always one step beyond logic." -- Frank Herbert


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


[C++-sig] bump.... boost::python and __declspec(align(16))

2009-06-23 Thread Simon Pickles


Hello all,

Sorry to bump this, but i am still unable to resolve my problem 
described below.


I have seen a 2008 thread on this: 
http://archives.free.net.ph/message/20080331.203857.548691a0.ja.html


In this case it is align(8) not align(16), and the thread describes this 
issue as resolved. This leads me to believe there is an answer *out 
there* :)


Thanks for your help

Simon


Hi,

I think this is an MSVC problem caused by the bad STL implementation,
but I wondered if there was a work around.

Both my client (Win32 MSVC8) and server (ubuntu GCC 4.2.3) share the
same c++ code. It compiles and runs on GCC but fails on MSVC.

This uses the the bullet physics library and includes a type defined
like this:

__declspec(align(16)) btVector3 {/*...*/};

When building my boost::python extension with:

class_("BtVector3", init());

I get an error like this :

C:\boost_1_38_0\boost/python/converter/as_to_python_function.hpp(21) :
error C2719: 'unnamed-parameter': formal parameter with
__declspec(align('16')) won't be aligned
C:\boost_1_38_0\boost/python/to_python_converter.hpp(88) : see
reference to class template instantiation
'boost::python::converter::as_to_python_function' being compiled
with
[
T=btVector3,
/*  Huge call stack omitted for clarity */
build.cpp(155) : see reference to class template instantiation
'boost::python::class_' being compiled
with
[
W=btVector3
]

Is there a way to overcome this? switch to MinGW? :)

Thank you very much

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


Re: [C++-sig] bump.... boost::python and __declspec(align(16))

2009-06-23 Thread Stefan Seefeld

On 06/23/2009 02:05 PM, Simon Pickles wrote:


Hello all,

Sorry to bump this, but i am still unable to resolve my problem 
described below.


The problem doesn't appear to be related to boost.python, or even boost. 
You may have better luck asking on a more suitable forum.


Stefan


--

  ...ich hab' noch einen Koffer in Berlin...

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


Re: [C++-sig] bump.... boost::python and __declspec(align(16))

2009-06-23 Thread Simon Pickles

Stefan Seefeld wrote:

On 06/23/2009 02:05 PM, Simon Pickles wrote:


Hello all,

Sorry to bump this, but i am still unable to resolve my problem 
described below.


The problem doesn't appear to be related to boost.python, or even boost. 
You may have better luck asking on a more suitable forum.


Stefan




I thought someone might say that. Maybe when more experienced, I will be 
able to ascertain that myself.


Thanks Stefan.

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


Re: [C++-sig] Function handling with Boost

2009-06-23 Thread Christopher Schramm
Mmmkay, looks like I found a dirty little workaround:

I put the C++ functions, I want to expose into a dummy class:

class Dummy
{
int f1(str arg1, ...
void f2(tuple arg1, ...
}

And then create a bpy object from it:

object tmp = class_("dummy")
 .def("f1", &Dummy::f1)
 .def("f2", &Dummy::f2);

Since I don't want to have this class in Python, I now add the methods
to the namespace instead:

global["my_mod"].attr("__dict__")["f1"] = tmp.attr("__dict__")["f1"];
global["my_mod"].attr("__dict__")["f2"] = tmp.attr("__dict__")["f2"];

Doing so, I end up with callable functions f1 and f2 in my_mod's
namespace which act as if they where simple functions outside of any
class, but benefit from boost's functionalities.

The only undesirable side effect I spotted is tracebacks exposing the
dummy class name:

Boost.Python.ArgumentError: Python argument types in
dummy.f1(str, str)
did not match C++ signature:
Message(boost::python::str, boost::python::str, boost::python::str)

Note to anyone who's about to reproduce my solution: The dummy class
mustn't have a staticmethod("f1") call, otherwise you end up with a
non-callable "staticmethod" object f1 in namespace.

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


Re: [C++-sig] Function handling with Boost

2009-06-23 Thread Stefan Seefeld

On 06/23/2009 02:32 PM, Christopher Schramm wrote:

Mmmkay, looks like I found a dirty little workaround:
   


I read your original mail, but I didn't understand what you are trying 
to achieve. You certainly can export functions to python:


void foo(...);

...

bpl::def("foo", foo);


works just fine. There shouldn't be any need to wrap the function in a 
class (member function), and then create some hidden object to invoke 
it. Or am I misunderstanding what you want ?




global["my_mod"].attr("__dict__")["f1"] = tmp.attr("__dict__")["f1"];
   


Why not simply

global["my_mod"]["f1"] = ... ; ?


HTH,
Stefan

--

  ...ich hab' noch einen Koffer in Berlin...

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


[C++-sig] boost::python on Linux

2009-06-23 Thread Jim Treadway
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.

The program output is:

-- BEGIN --
Traceback (most recent call last):
 File "", line 1, in 
Boost.Python.ArgumentError: Python argument types in
   Test.test(int)
did not match C++ signature:
   test(int)
-- END --

Here is the code.  It's compiled and linked using "g++
-I/usr/include/python2.5 -lpython2.5 -lboost_python -Wall -o test
test.cpp".  On the Linux box I'm using boost-1.37.0 and python-2.5.2.

-- BEGIN --
#include 
#include 
#include 

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();

   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 --
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig


[C++-sig] Passing a c++ new'ed object back to python

2009-06-23 Thread Simon Pickles

Hello again.

Here's the setup

#python
from hybrid import ObjectManager

om = ObjectManager()
om.PassModel()


//cpp
class Model
{
~Model()
{
printf("model go boom");
}
/* */
};

class ObjectManager
{
Model* PassModel()
{
return new Model();
};
};


When I use code which parallels this structure, I see the Model 
destructor is called when the c++ object goes out of scope (fine) 
despite the object being passed to python as the argument of a function 
(not fine!)


Is there a return policy I need to provide? I am confused by the way the 
destructing class is new'ed in c++ code, not in python by using exposed c++.


Better luck this time :)

Simon





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


Re: [C++-sig] Pybindgen/pygccxml integration

2009-06-23 Thread Gustavo Carneiro
2009/6/23 Ben Fitzpatrick 

> Thanks for the suggestions!
>
> I tried the first one, just as a test. I'd like to do the second
> programatically if I can, but I just wanted to make sure annotations were
> going to fix this. It still seems to be giving me the same error:
> (...)/pure_virtual.h:5: WrapperWarning: Parameter 'int * value' error (used
> in int pure_virtual_class::get_value(int * value) [member function]):
> 
>  virtual int get_value(int* value) = 0;
>
> Here is what I changed my code to:
>
>   // -#- @value(transfer_ownership=true) -#-
>  virtual int get_value(int* value)=0;
>

Apart from the blank line, seems OK.  Do you get a "unused annotation"
warning?


>
> This appears to be similar to the declaration you pointed me to in foo.h:
> // -#- @zbr(transfer_ownership=true) -#-
> void store_zbr (Zbr *zbr);
>
> Also tried renaming it to 'foobaz' in case 'value' was a keyword. Is there
> something I'm missing?
>
> Thanks,
> Ben Fitzpatrick
>
>
> Gustavo Carneiro wrote:
>
>>
>>
>> 2009/6/23 Ben Fitzpatrick > [email protected]>>
>>
>>
>>Hi everyone,
>>
>>I'm just starting to check out pybindgen after the messages I've
>>seen floating around on this mailing list, and I have a question
>>about the pygccxml integration. I've made a pure-virtual class
>>with a pointer return type, like so:
>>
>>class pure_virtual_class
>>{
>>  virtual int get_value(int* value)=0;
>>  virtual int put_value(int value)=0;
>>
>>  static pure_virtual_class* Create();
>>};
>>
>>Normally I'd call add_function('get_value', retval(int),
>>[param('int *', 'value', transfer_ownership=True)]) (I think)
>>But since I'm scanning the headers with gccxml, I'm not sure how
>>to tell it to transfer the ownership.
>>Right now it's giving me a TypeConfigurationError during 'parse',
>>which appears to be because it doesn't know how to deal with the
>>ownership.
>>
>>I looked in the documentation and couldn't find anything
>>referencing this, and none of the examples use gccxml.
>>
>>Any ideas?
>>
>>
>> Yes, there are two possible (alternative) ways to deal with this:
>>
>>  1. Add inline annotations as C++ comments in the header file to be
>> scanned;
>>  2. Add scanning hooks to inject annotations via a function callback;
>>
>> For inline annotations, see tests/foo.h, look for the comment lines
>> containing -#-.  I must warn you, though, that annotations are kind of a
>> "brittle" system.  The annotations are searched upwards starting in the line
>> immediately before the line that gccxml reports as the line the declaration
>> is defined.  The problem here is that gccxml reports the line of a
>> declaration sometimes in weird and unexpected places.
>>
>> The other way is more complicated in terms of code, but doable with some
>> pacience.  You can see an example in ns-3 [1], in the pre_scan_hook
>> function.
>>
>>
>> [1]
>> http://code.nsnam.org/ns-3-dev/file/96811f76c3e2/bindings/python/ns3modulescan.py
>>
>> I hope this helps.
>>
>> --
>> Gustavo J. A. M. Carneiro
>> INESC Porto, Telecommunications and Multimedia Unit
>> "The universe is always one step beyond logic." -- Frank Herbert
>>
>
> ___
> Cplusplus-sig mailing list
> [email protected]
> http://mail.python.org/mailman/listinfo/cplusplus-sig
>



-- 
Gustavo J. A. M. Carneiro
INESC Porto, Telecommunications and Multimedia Unit
"The universe is always one step beyond logic." -- Frank Herbert
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig

Re: [C++-sig] boost::python on Linux

2009-06-23 Thread Stefan Seefeld

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.




-- BEGIN --
#include
#include
#include

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("Test"), initTest);


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 --
   


HTH,
Stefan

--

  ...ich hab' noch einen Koffer in Berlin...

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


Re: [C++-sig] Passing a c++ new'ed object back to python

2009-06-23 Thread Stefan Seefeld

On 06/23/2009 02:59 PM, Simon Pickles wrote:


When I use code which parallels this structure, I see the Model 
destructor is called when the c++ object goes out of scope (fine) 
despite the object being passed to python as the argument of a 
function (not fine!)


That is because by default arguments are passed by-value. If you want 
something else, you need to specify a return-value policy, such as 
manage_new_object.





Is there a return policy I need to provide? I am confused by the way 
the destructing class is new'ed in c++ code, not in python by using 
exposed c++.


boost.python wants to pass a Model, and thus makes a copy. from the 
object you pass back, then destructs it.


Regards,
Stefan


--

  ...ich hab' noch einen Koffer in Berlin...

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


Re: [C++-sig] Pybindgen/pygccxml integration

2009-06-23 Thread Ben Fitzpatrick

Gustavo Carneiro wrote:



2009/6/23 Ben Fitzpatrick >


Thanks for the suggestions!

I tried the first one, just as a test. I'd like to do the second
programatically if I can, but I just wanted to make sure
annotations were going to fix this. It still seems to be giving me
the same error:
(...)/pure_virtual.h:5: WrapperWarning: Parameter 'int * value'
error (used in int pure_virtual_class::get_value(int * value)
[member function]):


 virtual int get_value(int* value) = 0;

Here is what I changed my code to:

  // -#- @value(transfer_ownership=true) -#-

 virtual int get_value(int* value)=0;


Apart from the blank line, seems OK.  Do you get a "unused annotation" 
warning?
 
That's odd, there wasn't a blank line in my original email. There 
certainly isn't one in the file.
I do not get an unused annotation warning, or anything to suggest it 
noticed my annotation in the slightest. I am using Python2.4 on Debian 
Etch, with Pygccxml 0.9.5. The Python's a bit old, but everything else 
has been compiled or installed manually.



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


Re: [C++-sig] boost::python on Linux

2009-06-23 Thread Jim Treadway
On Tue, Jun 23, 2009 at 12:06 PM, Stefan Seefeld 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
> #include
> #include
>
> 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("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

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

Re: [C++-sig] Function handling with Boost

2009-06-23 Thread Christopher Schramm
Stefan Seefeld wrote:
> I read your original mail, but I didn't understand what you are trying
> to achieve. You certainly can export functions to python:
> 
> void foo(...);
> 
> ...
> 
> bpl::def("foo", foo);
> 
> 
> works just fine. There shouldn't be any need to wrap the function in a
> class (member function), and then create some hidden object to invoke
> it. Or am I misunderstanding what you want ?

The problem is, I'm not writing a module that shall be imported, but I'm
embedding Python and want to provide a special "built in" module so the
final result is a single monolithic binary internally starting the
interpreter, creating the module and running user defined scripts.

Since boost::python's def function has a return type of void (sure,
because there seems to be no type for python functions in bpy),
functions can only be exported within a BOOST_PYTHON_MODULE (which I
don't have) or when belonging to a class (which led to my workaround).

>> global["my_mod"].attr("__dict__")["f1"] = tmp.attr("__dict__")["f1"];
>>
>
> Why not simply
>
> global["my_mod"]["f1"] = ... ; ?

Does that have the same effect? So I don't have to use the "__dict__"
attribute? Didn't know that. As I said, I'm completely new to Python's C
API and Boost Python. I'll try that, thanks for the tip.
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig


Re: [C++-sig] Function handling with Boost

2009-06-23 Thread Stefan Seefeld

On 06/23/2009 04:03 PM, Christopher Schramm wrote:

Stefan Seefeld wrote:
   

I read your original mail, but I didn't understand what you are trying
to achieve. You certainly can export functions to python:

void foo(...);

...

bpl::def("foo", foo);


works just fine. There shouldn't be any need to wrap the function in a
class (member function), and then create some hidden object to invoke
it. Or am I misunderstanding what you want ?
 


The problem is, I'm not writing a module that shall be imported, but I'm
embedding Python and want to provide a special "built in" module so the
final result is a single monolithic binary internally starting the
interpreter, creating the module and running user defined scripts.
   


I don't see how this affects what you export to Python.



Since boost::python's def function has a return type of void (sure,
because there seems to be no type for python functions in bpy),
functions can only be exported within a BOOST_PYTHON_MODULE (which I
don't have) or when belonging to a class (which led to my workaround).
   


You need a module into which to inject the symbols you export. That is 
true no matter the (meta)type of what you export, i.e. classes, 
functions, etc.
Once you have that module set up (via BOOST_PYTHON_MODULE), you can 
instantiate the newly created Python objects (types) from within C++ 
code, without having to go through the interpreter, import, eval, or exec.



Regards,
Stefan

--

  ...ich hab' noch einen Koffer in Berlin...

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


Re: [C++-sig] Function handling with Boost

2009-06-23 Thread Christopher Schramm
Stefan Seefeld wrote:
> You need a module into which to inject the symbols you export. That is
> true no matter the (meta)type of what you export, i.e. classes,
> functions, etc.
> Once you have that module set up (via BOOST_PYTHON_MODULE), you can
> instantiate the newly created Python objects (types) from within C++
> code, without having to go through the interpreter, import, eval, or exec.

So you say I may use BOOST_PYTHON_MODULE to define a module and then
immediately work with it? Hm - I failed with that at my very first try.

What I did was using BOOST_PYTHON_MODULE and after that calling bpy's
import() to add it to my global namespace what failed due to not finding
the module.

So I assumed I would have to build my module as extension to be able to
load it. But as I said, I want to stay monolithic.

Could you please show some sample code how you think I can achieve my
target using BOOST_PYTHON_MODULE?

Here's my goal again: I want to use exec_file() with an already imported
module providing access to some C++ functions and classes - all in one
binary.
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig


Re: [C++-sig] Function handling with Boost

2009-06-23 Thread Stefan Seefeld

On 06/23/2009 04:43 PM, Christopher Schramm wrote:

Stefan Seefeld wrote:
   

You need a module into which to inject the symbols you export. That is
true no matter the (meta)type of what you export, i.e. classes,
functions, etc.
Once you have that module set up (via BOOST_PYTHON_MODULE), you can
instantiate the newly created Python objects (types) from within C++
code, without having to go through the interpreter, import, eval, or exec.
 


So you say I may use BOOST_PYTHON_MODULE to define a module and then
immediately work with it? Hm - I failed with that at my very first try.

What I did was using BOOST_PYTHON_MODULE and after that calling bpy's
import() to add it to my global namespace what failed due to not finding
the module.
   


An alternative is not to use BOOST_PYTHON_MODULE at all, but set up 
converters in ordinary C++ code. In the following I set up a Python 
interpreter in my main application, inject a (C++) base class, run a 
Python script that adds a derived class, then instantiate and run that 
derived class from C++ code. I this may just be what you want:


--

namespace bpl = boost::python;

// An abstract base class
class Base : public boost::noncopyable
{
public:
  virtual ~Base() {};
  virtual std::string hello() = 0;
};

// Familiar Boost.Python wrapper class for Base
struct BaseWrap : Base, bpl::wrapper
{
  virtual std::string hello()
  {
return this->get_override("hello")();
  }
};

int main(int, char **)
{
  Py_Initialize();

  // Install C++ <-> Python converters
  bpl::class_ base("Base");

  // Retrieve the main module
  bpl::object main = bpl::import("__main__");
  // Retrieve the main module's namespace
  bpl::object global(main.attr("__dict__"));
  // Inject 'Base' type into global dict
  global["Base"] = base;
  try
  {
// Run script
bpl::exec_file("exec.py", global, global);
// Extract 'Derived' class, and instantiate it
bpl::object derived = global["Derived"]();
// Extract reference-to-base-class
Base &d = bpl::extract(derived);
// Call 'hello'
std::cout << d.hello() << std::endl;
  }
  catch (bpl::error_already_set const &)
  {
  PyErr_Print();
  }
}


--
And the Python side:
--

class Derived(Base):

def hello(self): return 'Hello from Python !'

--

Easy, isn't it ?

Regards,
Stefan

--

  ...ich hab' noch einen Koffer in Berlin...

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


Re: [C++-sig] Pybindgen/pygccxml integration

2009-06-23 Thread Gustavo Carneiro
2009/6/23 Ben Fitzpatrick 

> Gustavo Carneiro wrote:
>
>>
>>
>> 2009/6/23 Ben Fitzpatrick > [email protected]>>
>>
>>Thanks for the suggestions!
>>
>>I tried the first one, just as a test. I'd like to do the second
>>programatically if I can, but I just wanted to make sure
>>annotations were going to fix this. It still seems to be giving me
>>the same error:
>>(...)/pure_virtual.h:5: WrapperWarning: Parameter 'int * value'
>>error (used in int pure_virtual_class::get_value(int * value)
>>[member function]):
>>>0xb780326c>
>>
>> virtual int get_value(int* value) = 0;
>>
>>Here is what I changed my code to:
>>
>>  // -#- @value(transfer_ownership=true) -#-
>>
>> virtual int get_value(int* value)=0;
>>
>>
>> Apart from the blank line, seems OK.  Do you get a "unused annotation"
>> warning?
>>
>>
> That's odd, there wasn't a blank line in my original email. There certainly
> isn't one in the file.
> I do not get an unused annotation warning, or anything to suggest it
> noticed my annotation in the slightest. I am using Python2.4 on Debian Etch,
> with Pygccxml 0.9.5. The Python's a bit old, but everything else has been
> compiled or installed manually.


Well, with Python 2.5 it prints TypeConfigurationError('some message'),
which is rather more helpful.  You are not seeing the full error message,
just the (vague) exception type.

And did you run the unit tests (./waf check) ?  If the unit tests pass,
annotations supposedly are working...


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



-- 
Gustavo J. A. M. Carneiro
INESC Porto, Telecommunications and Multimedia Unit
"The universe is always one step beyond logic." -- Frank Herbert
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig

Re: [C++-sig] Function handling with Boost

2009-06-23 Thread Christopher Schramm
Stefan Seefeld wrote:
> An alternative is not to use BOOST_PYTHON_MODULE at all, but set up
> converters in ordinary C++ code. In the following I set up a Python
> interpreter in my main application, inject a (C++) base class, run a
> Python script that adds a derived class, then instantiate and run that
> derived class from C++ code. I this may just be what you want:

Sure. That's what I'm doing (just within a submodule below main). But in
your example - how do get simple c++ functions into the python scope
which aren't members of any class? That's my problem.
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig