[C++-sig] from-python converter for aligned class

2012-02-12 Thread Václav Šmilauer
I wrote a custom to-python converter for an aligned struct (It is a 
128-bit aligned vector type from the http://eigen.tuxfamily.org 
library). I followed 
http://www.boost.org/doc/libs/1_39_0/libs/python/doc/v2/faq.html#custom_string), 
the converter looks like this:


class AlignedType{ /*... */} __attribute__((aligned(16)));
struct converter{
   converter(){ 
py::converter::registry::push_back(&convertible,&construct,py::type_id());

   static void* convertible(PyObject* obj_ptr){ /*...*/ }
   static void construct(PyObject* obj_ptr, 
py::converter::rvalue_from_python_stage1_data* data){
  void* 
storage=((py::converter::rvalue_from_python_storage*)(data))->storage.bytes;
  // !! this creates AlignedType instance at a possibly 
unaligned address !!

  new (storage) AlignedType;
  data->convertible=storage;
   }
};

I am getting crashes due to mis-alignment of the resulting object, 
because it is created at a possible unaligned address. Is there a way 
around this? Can I allocate another chunk of memory, or enforce the 
alignment before it is allocated?


Best regards, Vaclav


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


Re: [C++-sig] boost::python handles boost::shared_ptr specially...

2012-02-12 Thread VáclavŠmilauer
> Will std::shared_ptr just work also?
I asked this question some time ago here:
http://stackoverflow.com/questions/6568952/c0x-stdshared-ptr-vs-boostshared-ptr

To summarize: you need to define free get_pointer functions for T=your
shared_ptr type and it will work. I eventyallu did not go that way because
boost::serialization (which I also needed) will not work with std::shared_ptr.


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


Re: [C++-sig] Boost converter for passing std::vector object by reference into a function??

2012-02-12 Thread VáclavŠmilauer

> One could imagine writing a fancy converter that would allow the first 
> form by making a temporary std::vector, passing that to the C++ 
> function, and then copying the elements in the vector back into the 
> Python list.  But that's potentially a very expensive sequence of 
> operations, and it's actually impossible to do that through a registered 
> converter in Boost.Python.

In some cases, such a converter makes sense, I wrote one (see 
http://bazaar.launchpad.net/~eudoxos/+junk/tr2/view/head:/py/wrapper/customConverters.cpp#L142,
then its specializations for various types below). Where
std::vector arg is expected, it will check whether the python
object is a sequence and then convert is to the c++ vector. Of course the
objects are being copied, not referenced, unless those objects are shared_ptr's,
in which case you get reference semantics you asked for.

Cheers, vaclav


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


Re: [C++-sig] from-python converter for aligned class

2012-02-12 Thread Jim Bosch

On 02/12/2012 12:49 PM, Václav Šmilauer wrote:

I wrote a custom to-python converter for an aligned struct (It is a
128-bit aligned vector type from the http://eigen.tuxfamily.org
library). I followed
http://www.boost.org/doc/libs/1_39_0/libs/python/doc/v2/faq.html#custom_string),
the converter looks like this:

class AlignedType{ /*... */} __attribute__((aligned(16)));
struct converter{
converter(){
py::converter::registry::push_back(&convertible,&construct,py::type_id());

static void* convertible(PyObject* obj_ptr){ /*...*/ }
static void construct(PyObject* obj_ptr,
py::converter::rvalue_from_python_stage1_data* data){
void*
storage=((py::converter::rvalue_from_python_storage*)(data))->storage.bytes;

// !! this creates AlignedType instance at a possibly unaligned address !!
new (storage) AlignedType;
data->convertible=storage;
}
};

I am getting crashes due to mis-alignment of the resulting object,
because it is created at a possible unaligned address. Is there a way
around this? Can I allocate another chunk of memory, or enforce the
alignment before it is allocated?



Hmm.  You might be able to make this work with another class that holds 
the AlignedType and has an implicit conversion to it:


struct AlignedHolder {

operator AlignedType const & () const {
return *p;
}

std::auto_ptr p;
};

You'd then write an rvalue converter for AlignedHolder, but register it 
with type_id().  Boost.Python will see the type_id for 
AlignedType and try to use it when you have an "AlignedType const &" 
argument, but it will try to pass the AlignedHolder object to your 
function...at which point the implicit conversion will kick in.


I haven't tested this, and it involves really tricking Boost.Python, but 
I think it might work.


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


[C++-sig] boost python & context manager/with statement

2012-02-12 Thread Avi Bahra
Does boost python support context management using the with
statement?  In my case I need to manage __enter__/__exit__ on
the c++ side. Could not find any examples for this in the
documentation.

Any help appreciated.

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


Re: [C++-sig] boost::python handles boost::shared_ptr specially...

2012-02-12 Thread Jim Bosch

On 02/12/2012 01:01 PM, VáclavŠmilauer wrote:

Will std::shared_ptr just work also?

I asked this question some time ago here:
http://stackoverflow.com/questions/6568952/c0x-stdshared-ptr-vs-boostshared-ptr

To summarize: you need to define free get_pointer  functions for T=your
shared_ptr type and it will work. I eventyallu did not go that way because
boost::serialization (which I also needed) will not work with std::shared_ptr.



This would be a good start; it would make the std::shared_ptr support at 
least as good as, say, std::auto_ptr.  It wouldn't be quite enough in 
some cases, though, because Boost.Python does a lot of special things 
for boost::shared_ptr, related to fact that it can have custom deleters.


In particular, if you have a Boost.Python-wrapped C++ object that is 
held *by-value* inside a PyObject*, you can actually pass that to a C++ 
function by boost::shared_ptr, because Boost.Python creates that 
shared_ptr on the fly with a custom deleter that holds the PyObject*. 
That's should also be possible with std::shared_ptr, and Boost.Python 
would need to be told about that too.


Jim

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


[C++-sig] boost python and boost/thread.hpp

2012-02-12 Thread Guillaume Carbonneau
Hi,

I'm having compilation errors as soon as I give my classes member variables 
that come from the boost thread library.
Wrapping those in shared_ptr seems to work.

Any idea why? Is there a special wrapping directive I should give it to?


#include 
#include 
#include 


struct Hello {
Hello(){}

bool run(int argc, char **argv, char **envp);
std::string hello_string_;
std::vector hello_vector_ ;
size_t hello_num_;
boost::shared_ptr hello_shared_ptr_string_;
bool hello_bool_;
boost::shared_ptr condition_variable_shared_ptr_;

// does not compile
//boost::condition_variable condition_variable_;
};


char const* greet()
{
return "hello, world";
}


BOOST_PYTHON_MODULE(hello)
{
using namespace boost::python;
def("greet", greet);
class_("Hello");
}




$ make
g++ -c -o hello.o 
-I/System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6/ 
-Wall -O3 -fPIC -g hello.cpp
/usr/local/include/boost/python/object/value_holder.hpp: In constructor 
‘boost::python::objects::value_holder::value_holder(PyObject*, A0) [with 
A0 = boost::reference_wrapper, Value = Hello]’:
/usr/local/include/boost/python/object/make_instance.hpp:71:  instantiated from 
‘static Holder* boost::python::objects::make_instance::construct(void*, PyObject*, boost::reference_wrapper) [with T 
= Hello, Holder = boost::python::objects::value_holder]’
/usr/local/include/boost/python/object/make_instance.hpp:45:  instantiated from 
‘static PyObject* boost::python::objects::make_instance_impl::execute(Arg&) [with Arg = const boost::reference_wrapper, T = Hello, Holder = boost::python::objects::value_holder, 
Derived = boost::python::objects::make_instance >]’
/usr/local/include/boost/python/object/class_wrapper.hpp:29:  instantiated from 
‘static PyObject* boost::python::objects::class_cref_wrapper::convert(const Src&) [with Src = Hello, MakeInstance = 
boost::python::objects::make_instance >]’
/usr/local/include/boost/python/converter/as_to_python_function.hpp:27:  
instantiated from ‘static PyObject* 
boost::python::converter::as_to_python_function::convert(const 
void*) [with T = Hello, ToPython = 
boost::python::objects::class_cref_wrapper > >]’
/usr/local/include/boost/python/to_python_converter.hpp:87:  instantiated from 
‘boost::python::to_python_converter::to_python_converter() [with T = Hello, Conversion = 
boost::python::objects::class_cref_wrapper > >, bool has_get_pytype = true]’
/usr/local/include/boost/python/object/class_wrapper.hpp:26:  instantiated from 
‘static void boost::python::objects::class_metadata::maybe_register_class_to_python(T2*, mpl_::false_) [with T2 = Hello, T = 
Hello, X1 = boost::python::detail::not_specified, X2 = 
boost::python::detail::not_specified, X3 = 
boost::python::detail::not_specified]’
/usr/local/include/boost/python/object/class_metadata.hpp:229:  instantiated 
from ‘static void boost::python::objects::class_metadata::register_aux2(T2*, Callback) [with T2 = Hello, Callback = 
boost::integral_constant, T = Hello, X1 = 
boost::python::detail::not_specified, X2 = 
boost::python::detail::not_specified, X3 = 
boost::python::detail::not_specified]’
/usr/local/include/boost/python/object/class_metadata.hpp:219:  instantiated 
from ‘static void boost::python::objects::class_metadata::register_aux(void*) [with T = Hello, X1 = 
boost::python::detail::not_specified, X2 = 
boost::python::detail::not_specified, X3 = 
boost::python::detail::not_specified]’
/usr/local/include/boost/python/object/class_metadata.hpp:205:  instantiated 
from ‘static void boost::python::objects::class_metadata::register_() [with T = Hello, X1 = boost::python::detail::not_specified, X2 
= boost::python::detail::not_specified, X3 = 
boost::python::detail::not_specified]’
/usr/local/include/boost/python/class.hpp:496:  instantiated from ‘void 
boost::python::class_::initialize(const DefVisitor&) [with 
DefVisitor = boost::python::init, 
W = Hello, X1 = boost::python::detail::not_specified, X2 = 
boost::python::detail::not_specified, X3 = 
boost::python::detail::not_specified]’
/usr/local/include/boost/python/class.hpp:629:  instantiated from 
‘boost::python::class_::class_(const char*, const char*) [with W 
= Hello, X1 = boost::python::detail::not_specified, X2 = 
boost::python::detail::not_specified, X3 = 
boost::python::detail::not_specified]’
hello.cpp:32:  instantiated from here
/usr/local/include/boost/python/object/value_holder.hpp:137: error: no matching 
function for call to ‘Hello::Hello(const Hello&)’
hello.cpp:7: note: candidates are: Hello::Hello()
hello.cpp:6: note:  Hello::Hello(Hello&)
make: *** [hello.o] Error 1


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

Re: [C++-sig] boost python & context manager/with statement

2012-02-12 Thread Jim Bosch

On 02/12/2012 01:25 PM, Avi Bahra wrote:

Does boost python support context management using the with
statement?  In my case I need to manage __enter__/__exit__ on
the c++ side. Could not find any examples for this in the
documentation.



Do you mean that you want to create an object that *has* __enter__ and 
__exit__ methods in C++, or that you want to write something like a 
"with" statement in C++?


If it's the former, you can do that pretty easily - just call your 
methods "__enter__" and "__exit__" and give them the right signatures, 
and Python will use them quite happily.


If it's the latter, I'm afraid there's no syntactic sugar for it.  It's 
probably easiest to fake it by calling __enter__ and __exit__ and 
wrapping it all in a C++ try/catch block (if you raise a Python 
exception, it gets thrown in C++ as boost::python::error_already_set). 
But C++ itself doesn't really have a context management syntax, so there 
isn't really a good way to simulate the Python one.


HTH

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


Re: [C++-sig] boost python and boost/thread.hpp

2012-02-12 Thread Stefan Seefeld
On 02/12/2012 01:34 PM, Guillaume Carbonneau wrote:
> Hi,
>
> I'm having compilation errors as soon as I give my classes member
> variables that come from the boost thread library.
> Wrapping those in shared_ptr seems to work.

By default, boost.python treats objects as copyable. That doesn't work
if some of your members aren't copyable themselves. You need to figure
out the semantics of your struct 'Hello'. Do you want it to be copyable,
than you need to change the definition of the non-copyable members, such
as you have done.

However, you could as well make the wrapped type non-copyable, in which
case that's not necessary.

>
> Any idea why? Is there a special wrapping directive I should give it to?

See http://www.boost.org/doc/libs/1_48_0/libs/python/doc/v2/class.html

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] boost python and boost/thread.hpp

2012-02-12 Thread Jim Bosch

On 02/12/2012 01:34 PM, Guillaume Carbonneau wrote:

Hi,

I'm having compilation errors as soon as I give my classes member
variables that come from the boost thread library.
Wrapping those in shared_ptr seems to work.

Any idea why? Is there a special wrapping directive I should give it to?



The problem is that boost::condition_variable doesn't have a copy 
constructor.  When you declare a class with Boost.Python, it assumes it 
can be returned by value (which requires a copy constructor).  The 
boost::condition_variable data member prevents the compiler from 
generating an implicit copy constructor.


You can solve this by instead using:

class_("Hello");

That tells Boost.Python not to assume this class can be returned by 
value.  If it makes sense to define an explicit copy constructor for 
your class, that will work too.


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


[C++-sig] Runtime errors after trying to use debug Python build with Boost.Python

2012-02-12 Thread Adam Preble
I am trying to use Python with debug symbols so I can peak into it when I
get runtime errors related to Boost.Python wrappers.  I can't seem to get
this to work.  I think this is more a building thing than anything else so
I figure I'd share what I've done:

1. Acquire the source for Stackless Python 2.6.5.  Mind you, this release
has been working for me standalone.
2. Build Stackless Python, or at least the "relevant parts." I don't have
all the dependencies and according to the instructions, I should find what
I need within in the python and pythoncore projects.  I'm building on
Windows, using the PcBuild project.
3. Copy over python.exe, python26.lib, and the python_d.dll to my Python26
directory.  The build generates everything as python_d.exe, python26_d.lib,
and all that.  I have them all named instead as just python.exe, and
python26.exe; I keep python_d.dll since it seems to look for that
specifically at runtime.
4. I rebuild Boost.Python.  I make sure to specifically overwrite:

boost_python_vc100-mt-gd-1_47.lib
boost_python_vc100-mt-gd-1_47.dll
libboost_python_vc100-mt-gd-1_47.lib

5. I rebuild my project, and launch it.

Along inside Py_Initialize I hit an assert:
Assertion failed: (op->_ob_prev == NULL) == (op->_ob_next == NULL), file
..\Objects\object.c, line 65

I figure the assert doesn't mean anything to anybody, so I hoped instead
you could see my procedure and point out where I botched something.  FWIW,
the debug python.exe otherwise seems to be working fine by itself.  I can
start it up and run some Python scripts through it.
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig