From finjulhich at gmail.com Sat Sep 5 17:40:29 2015 From: finjulhich at gmail.com (MM) Date: Sat, 5 Sep 2015 16:40:29 +0100 Subject: [C++-sig] boost python exposing const* Message-ID: I have a c++ class T that I succesfully expose via: class_ ... later on, I have struct S { const T* underlying; }; class_("S"). .def_readonly("underlying", &S::underlying) ; >From python, when I call: s.underlying where s is a python instance of S, i get the following error: TypeError: No to_python (by-value) converter found for C++ type: T const* Do I need another converter for T const* in particular? or for both T* and T const*? Does the class_ above only expose T itself, not pointers to it? Thanks MM -------------- next part -------------- An HTML attachment was scrubbed... URL: From finjulhich at gmail.com Tue Sep 8 00:02:46 2015 From: finjulhich at gmail.com (MM) Date: Mon, 7 Sep 2015 23:02:46 +0100 Subject: [C++-sig] Why isn't python-exposed boost::gregorian::date available everywhere? Message-ID: I thought I'd post this SO question here as well, thanks: I exposed boost::gregorian::date with the following: date_from_python_date{}; to_python_converter{}; where *date_to_python_date* is a struct with the right convert function: it converts it to a python datetime.date. Some c++ functions return date and calling them from python works. At a later stage, I have a c++ class class F {/// ....public: boost::gregorian::date start;}; which I register with: class_>("F") .def_readwrite("start", &F::start, "FTD"); I do this after having python-registered date. I then obtain an instance f of the F wrapper. But then, when I print f.start The error is: No Python class registered for C++ class boost::gregorian::date -------------- next part -------------- An HTML attachment was scrubbed... URL: From Holger.Joukl at LBBW.de Wed Sep 9 10:04:30 2015 From: Holger.Joukl at LBBW.de (Holger Joukl) Date: Wed, 9 Sep 2015 10:04:30 +0200 Subject: [C++-sig] boost python exposing const* In-Reply-To: References: Message-ID: Hi, > I have a c++ class T that I succesfully expose via: > > class_ ... > > later on, I have > > struct S { > ? const T* underlying; > }; > > class_("S"). > ? ? .def_readonly("underlying", &S::underlying) > ; > > From python, when I call: > > s.underlying > > where s is a python instance of S, i get the following error: > > TypeError: No to_python (by-value) converter found for C++ type: T const* > > Do I need another converter for T const* in particular? or for both > T* and T const*? > Does the class_ above only expose T itself, not pointers to it? Using add_property() instead should be your friend, to be able to use a call policy. E.g.: #include namespace bp = boost::python; // classes to expose struct A { }; struct B { B() : underlying(NULL) { } A const * underlying; }; // expose to python BOOST_PYTHON_MODULE(properties) { bp::class_("A") ; bp::class_("B") // Won't work: "No to_python (by-value) converter found... //.def_readonly("underlying", &B::underlying) .add_property("underlying", bp::make_getter(&B::underlying, bp::return_internal_reference<>())) ; } See FAQ entry also: http://www.boost.org/doc/libs/1_59_0/libs/python/doc/v2/faq.html#topythonconversionfailed Holger Landesbank Baden-Wuerttemberg Anstalt des oeffentlichen Rechts Hauptsitze: Stuttgart, Karlsruhe, Mannheim, Mainz HRA 12704 Amtsgericht Stuttgart From danielthebrake at gmail.com Thu Sep 17 23:41:27 2015 From: danielthebrake at gmail.com (Daniel Brake) Date: Thu, 17 Sep 2015 17:41:27 -0400 Subject: [C++-sig] formatting str of exposed type Message-ID: Hi Boost Python, I would like a C++ class which I am exposing through Boost.Python (initially,the boost::multiprecision::mpfr_float, with expression templates turned off -- eventually a whole pile of types depending on mpfr_float) to be able to respond to the %precision n command, for example, from ipython. Additionally, I would like to be able to use a format spec such as print('{0:.40}'.format(b)) to print a variable b with 40 digits of precision. Even better, I would like to be able to put a standard formatting letter in the format string preceding the .format call. My best guess would be to def("format",...) as a free function for my class_, but I also guess that this is a standard operation, and I would rather use a standard solution. Parsing the formatting string myself doesn't sound like much fun, and am hoping for a little insight from the mailing list. Thanks very much, Daniel Brake University of Notre Dame -------------- next part -------------- An HTML attachment was scrubbed... URL: From nat at lindenlab.com Fri Sep 18 16:01:38 2015 From: nat at lindenlab.com (Nat Goodspeed) Date: Fri, 18 Sep 2015 10:01:38 -0400 Subject: [C++-sig] formatting str of exposed type In-Reply-To: References: Message-ID: On Thu, Sep 17, 2015 at 5:41 PM, Daniel Brake wrote: > I would like a C++ class which I am exposing through Boost.Python > (initially,the boost::multiprecision::mpfr_float, with expression templates > turned off -- eventually a whole pile of types depending on mpfr_float) to > be able to respond to the %precision n command, for example, from ipython. > Additionally, I would like to be able to use a format spec such as > > print('{0:.40}'.format(b)) > > to print a variable b with 40 digits of precision. Even better, I would > like to be able to put a standard formatting letter in the format string > preceding the .format call. > > My best guess would be to def("format",...) as a free function for my > class_, but I also guess that this is a standard operation, and I would > rather use a standard solution. Parsing the formatting string myself > doesn't sound like much fun, and am hoping for a little insight from the > mailing list. What if you provide a __format__ function, as described in PEP 3101: https://www.python.org/dev/peps/pep-3101/ (section "Controlling Formatting on a Per-Type Basis")? From stefan at seefeld.name Fri Sep 18 16:15:07 2015 From: stefan at seefeld.name (Stefan Seefeld) Date: Fri, 18 Sep 2015 10:15:07 -0400 Subject: [C++-sig] formatting str of exposed type In-Reply-To: References: Message-ID: <55FC1C6B.4070905@seefeld.name> On 09/17/2015 05:41 PM, Daniel Brake wrote: > > Hi Boost Python, > > I would like a C++ class which I am exposing through > Boost.Python (initially,the boost::multiprecision::mpfr_float, with > expression templates turned off -- eventually a whole pile of types > depending on mpfr_float) to be able to respond to the %precision n > command, for example, from ipython. Additionally, I would like to be > able to use a format spec such as > > print('{0:.40}'.format(b)) > > to print a variable b with 40 digits of precision. Even better, I > would like to be able to put a standard formatting letter in the > format string preceding the .format call. > > My best guess would be to def("format",...) as a free function for my > class_, but I also guess that this is a standard operation, and I > would rather use a standard solution. Parsing the formatting string > myself doesn't sound like much fun, and am hoping for a little insight > from the mailing list. > Note that in the invocation above, 'format' is a method of 'str', not the type that's being formatted. Thus, if b is an instance of your mpfr_float, you don't have to re-implement 'format'. What I'd rather do is provide a "__str__" member function (you could use the standard mechanism involving the "str(self)" syntax from Boost.Python) that just invokes the above. Depending on how flexible you want this to be, you could hard-code the formatting spec into your wrapper class, or add a 'format' attribute that's being used, and which could then be customized from within Python. The possibilities are endless... Stefan -- ...ich hab' noch einen Koffer in Berlin... From kseehart at micron.com Fri Sep 25 00:36:02 2015 From: kseehart at micron.com (Ken Seehart (kseehart) [CONT - Type 2]) Date: Thu, 24 Sep 2015 22:36:02 +0000 Subject: [C++-sig] Magic boost conversion unicorn hook? Message-ID: <5F3158EBBEB74A47A5287FE2FA0EDE8859B673DA@NTXBOIMBX03.micron.com> I have a python class that wraps a boost.python object. I am using a boost.python library function that requires the raw boost.python object. I want to be able to pass the python object to the boost api function. Is there a hook available on the python side that instructs boost to perform a conversion? Hypothetically, it would be something like this: class EggWrapper(object): def __init__(self, egg): object.__setattr__(self, '_egg', egg) def __getattribute__(self, name): egg = object.__getattribute__(self, '_egg') if name=='_egg': return egg if name in EggWrapper.__dict__: return object.__getattribute__(self, name) return getattr(egg, name) def __setattr__(self, name, value): egg = object.__getattribute__(self, '_egg') setattr(egg, name, value) def magic_boost_conversion_unicorn_hook(self): 'this is automatically called by boost to convert arguments' egg = object.__getattribute__(self, '_egg') return egg import myboostlib egg = EggWrapper(myboostlib.getEgg()) myboostlib.spam(egg) Where the signature for spam is: spam(class boost::python::api::object) And myboostlib.getEgg() returns a boost::python::api::object - I do not have the ability to modify the library or any C/C++ code, the solution must be entirely on the python side. - I can't change the calling convention. The wrapped object must be used (i.e. I can't just say myboostlib.spam(egg._egg), though that is the desired result). So the solution should be incorporated into the EggWrapper class. - I'm only interested in solutions that reflect expert knowledge of Boost.Python, since I believe I have already considered all the trivial solutions. So my question is, does magic_boost_conversion_unicorn_hook() exist, and if so, how is it spelled? Thanks, Ken -------------- next part -------------- An HTML attachment was scrubbed... URL: From stefan at seefeld.name Fri Sep 25 17:20:48 2015 From: stefan at seefeld.name (Stefan Seefeld) Date: Fri, 25 Sep 2015 11:20:48 -0400 Subject: [C++-sig] Magic boost conversion unicorn hook? In-Reply-To: <5F3158EBBEB74A47A5287FE2FA0EDE8859B673DA@NTXBOIMBX03.micron.com> References: <5F3158EBBEB74A47A5287FE2FA0EDE8859B673DA@NTXBOIMBX03.micron.com> Message-ID: <56056650.2080403@seefeld.name> On 24.09.2015 18:36, Ken Seehart (kseehart) [CONT - Type 2] wrote: > > I have a python class that wraps a boost.python object. I am using a > boost.python library function that requires the raw boost.python > object. I want to be able to pass the python object to the boost api > function. > I don't entirely understand what you are saying. What do you mean by "boost.python object", and what by "boost.python library function" ? Casting from Python to C++ types is typically done automatically by Boost.Python. To give an example: You typcally start with a C++ class such as class Foo {...}; which is exposed to Python via the usual magic. Likewise, you may have a C++ function taking "Foo" arguments (by value or by reference), which equally can be exposed to Python. In Python, you can manipulate objects of type "Foo" (including subclassing Foo), and pass them to these functions, and Boost.Python will take care of the required conversion. Could you please outline in a little more detail what it is that you are attempting to do ? It sounds to me like you might be missing some fundamental aspects of what Boost.Python does, as explicit type conversion is rarely necessary, in particular from the Python side. Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin... From bluescarni at gmail.com Sat Sep 26 19:30:53 2015 From: bluescarni at gmail.com (Francesco Biscani) Date: Sat, 26 Sep 2015 19:30:53 +0200 Subject: [C++-sig] Magic boost conversion unicorn hook? In-Reply-To: <56056650.2080403@seefeld.name> References: <5F3158EBBEB74A47A5287FE2FA0EDE8859B673DA@NTXBOIMBX03.micron.com> <56056650.2080403@seefeld.name> Message-ID: On 25 September 2015 at 17:20, Stefan Seefeld wrote: > I don't entirely understand what you are saying. What do you mean by > "boost.python object", and what by "boost.python library function" ? > I think he has a C++ class that was exposed to Python, and which is included as a member of a pure Python class. I think he would like to be able to "implicitly convert" said pure Python class to the type of the exposed C++ class, so that it can be used directly as an argument to exposed C++ functions that expect the exposed C++ class as argument. Maybe a possible solution would be to register a Python converter for your wrapping class? This blog post was helpful to me when I had to learn how to write custom to/from Python converters: http://misspent.wordpress.com/2009/09/27/how-to-write-boost-python-converters Hope that helps! Cheers, Francesco. -------------- next part -------------- An HTML attachment was scrubbed... URL: From bmerry at ska.ac.za Sun Sep 27 13:38:50 2015 From: bmerry at ska.ac.za (Bruce Merry) Date: Sun, 27 Sep 2015 13:38:50 +0200 Subject: [C++-sig] Call policy to store reference Message-ID: Hi I don't know if something like this has been discussed before, but I thought I'd contribute it in case it is useful. I found that with_custodian_and_ward didn't suit my needs, because (a) the association it creates is permanent, even if the underlying association is later broken, leading to handle leaks; (b) I had some issues with destructor ordering when the interpreter exits (sorry, I don't recall the details). I've created an alternative call policy that requires a wrapper custodian class with a handle<> data member, which is set to a reference to the ward object. This is suitable for wrapping functions that set an underlying C++ reference in the custodian, replacing any previous reference rather than adding to it. The code is below - I've only written a _postcall version, but it should be possible to write a non-postcall version too. At the moment it is part of a GPLv3+ project, but I'm working on getting permission from my employer to release this code under the Boost License as well, to make it easier to use in other Boost.Python-based projects (or to incorporate into Boost.Python itself). You can see it in action in spead2 (e.g. https://github.com/ska-sa/spead2/blob/v0.3.0/src/py_send.cpp). template T::*handle_ptr, std::size_t custodian, std::size_t ward, class BasePolicy_ = boost::python::default_call_policies> struct store_handle_postcall : BasePolicy_ { static_assert(custodian != ward, "object must not hold reference to itself"); static PyObject* postcall(PyObject *args, PyObject *result) { std::size_t arity = PyTuple_GET_SIZE(args); if (custodian > arity || ward > arity) { PyErr_SetString(PyExc_IndexError, "store_handle_postcall: argument index out of range"); return nullptr; } result = BasePolicy_::postcall(args, result); if (result == nullptr) return nullptr; PyObject *owner = custodian > 0 ? PyTuple_GET_ITEM(args, custodian - 1) : result; PyObject *child = ward > 0 ? PyTuple_GET_ITEM(args, ward - 1) : result; boost::python::extract extractor(owner); try { T &target = extractor(); target.*handle_ptr = boost::python::handle<>(boost::python::borrowed(child)); } catch (boost::python::error_already_set) { Py_XDECREF(result); return nullptr; } return result; } }; Bruce -- Bruce Merry Senior Science Processing Developer SKA South Africa