Re: [C++-sig] accessing nontrivial types

2010-02-05 Thread Anders Wallin
> 3)
> bp::list getter (const MyClass& self)
> {
>        bp::list L
>        //move the values in self.ilist to L
>        return L
> }
> void setter (const MyClass& self, bp::list L)
> {
>        //move the values in L to self.ilist
> }
>
> class_("Myclass")
>        .def_readwrite("i", &Myclass::i)
>        .def_readwrite("f", &Myclass::f)
>        .add_property ("ilist", &getter, &setter)
> ;
>
> That's the gist of it. I am not sure if 1 works and 2 will be difficult, so I 
> recommend 3

Thanks, this works. Incidentally, "getter" seems to be a defined
function somewhere else, so I had better luck naming it "listgetter"
or something similar.

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


Re: [C++-sig] Getting a b::p::object for a PyObject and vice-versa.

2010-02-05 Thread Ralf W. Grosse-Kunstleve
What you do looks right.

bp::object(bp::handle<>(c_object))

may be a little shorter in some places. (Watch out for compilers confusing your 
code with
function declarations.)

bpobj.ptr() will give you the PyObject* directly.
bphdl.get() does the same for handle<>.

(Improvements are always welcome, but a complete set of patches is a lot of work
and I doubt even wrapping the entire Python API will make much of a practical
difference.)

Ralf



- Original Message 
From: Murray Cumming 
To: cplusplus-sig@python.org
Sent: Thu, February 4, 2010 12:16:41 PM
Subject: [C++-sig] Getting a b::p::object for a PyObject and vice-versa.

Can someone confirm that this is correct:

1.
To get a boost::python::object that wraps an existing PyObject* (when
you get a PyObject from a C function not wrapped in boost::python), can
someone confirm that this is correct:
Either:
a)
  PyObject* c_object = get_the_c_object(); //returns a reference.
  boost::python::handle<> handle(c_object);
  boost::python::object cpp_object(handle);
or
b)
  PyObject* c_object = get_the_c_object(); //returns no extra reference.
  boost::python::handle<> handle(boost::python::borrowed(cObject));
  boost::python::object cpp_object(handle);

The code for b) can be simplified by doing 
  boost::python::object cpp_object(boost::python::borrowed(cObject))
but I don't see a way to simplify a)

I also see allow_null here, but like borrowed, it's not documented:
http://www.boost.org/doc/libs/1_41_0/libs/python/doc/v2/handle.html#allow_null-spec
so I can only guess at its purpose.

In general I wish this was much simpler and stated clearly somewhere.


2.
How can I get the underlying PyObject* from a boost::python::object
(when I need it to call a C function not wrapped in boost::python)?

I guessed at this, but it doesn't seem to be working for me:
  boost::python::extract extractor(cpp_object);
  if(extractor.check())
  {
PyObject* c_object = extractor;
  }
___
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig