Miscellaneous Boost.Python Utilities

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Miscellaneous Boost.Python Utilities

Jim Bosch-2
In the hope that others might find them useful, and perhaps contribute
some of their own, I've posted a collection of miscellaneous
Boost.Python extensions I've been using at the Boost sandbox:

http://svn.boost.org/svn/boost/sandbox/python_extensions

Features include:

- Conversions between STL containers and Boost.Range iterator ranges and
Python builtins like list, tuple, and dict.

- Conversions between Boost.Fusion sequences and Python tuples.

- A third solution to the const shared_ptr problem (see
http://language-binding.net/pyplusplus/troubleshooting_guide/shared_ptr/shared_ptr.html)
that isn't evil (I don't think) and doesn't involve changing the
Boost.Python library itself - it simply doesn't use
register_ptr_to_python to register the conversion.

More info is available in the README file.

Also, I'm sure many of the regulars on this list have their own similar
extensions, and I'd love to see them collected in one place.  If you'd
like to add your collection to mine, or have a suggestion for some other
place I should put my collection, please let me know.  I would like to
limit this collection to simple things that don't depend on non-Boost
libraries, however.


Jim Bosch
_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Reply | Threaded
Open this post in threaded view
|

Re: Miscellaneous Boost.Python Utilities

Brian O'Kennedy-2
Hi Jim,

I've been trying out your python extensions and find them very useful
to ease the wrapping process. I did however find a problem with the
behaviour of the container_from_python_sequence converter.

When having two overloaded functions, one of which expects a container
of TestObjects and the other a single TestObject, I get this error in
Python when calling the overloaded function with a single TestObject.
  TypeError: object of type 'TestObject' has no len()
This is due to the use of the len() function on a non-sequence in the
'convertible' method of container_from_python_sequence.

The other issue is that the type of objects in the sequence is only
checked if the sequence has more-than-one item, and not when it only
contains one. I've made a couple of changes, included in the patch
below.

(A) Check if the input is in fact a sequence.
(B) Check the contained type when the sequence length is non-zero

Cheers
 Brian


-----------------container.hpp----------------------------------
@@ -37,8 +37,10 @@
struct container_from_python_sequence {

static void* convertible(PyObject * obj) {
         try {
+            if (!PySequence_Check(obj))
+              return NULL;
             object sequence(handle<>(borrowed(obj)));
-            if (len(sequence) > 1) {
+            if (len(sequence) > 0) {
                 if (!extract<Value>(sequence[0]).check())
                     return NULL;
             }
---------------------------------------------------------

On 14 May 2010 01:53, Jim Bosch <[hidden email]> wrote:

>
> In the hope that others might find them useful, and perhaps contribute some of their own, I've posted a collection of miscellaneous Boost.Python extensions I've been using at the Boost sandbox:
>
> http://svn.boost.org/svn/boost/sandbox/python_extensions
>
> Features include:
>
> - Conversions between STL containers and Boost.Range iterator ranges and Python builtins like list, tuple, and dict.
>
> - Conversions between Boost.Fusion sequences and Python tuples.
>
> - A third solution to the const shared_ptr problem (see http://language-binding.net/pyplusplus/troubleshooting_guide/shared_ptr/shared_ptr.html) that isn't evil (I don't think) and doesn't involve changing the Boost.Python library itself - it simply doesn't use register_ptr_to_python to register the conversion.
>
> More info is available in the README file.
>
> Also, I'm sure many of the regulars on this list have their own similar extensions, and I'd love to see them collected in one place.  If you'd like to add your collection to mine, or have a suggestion for some other place I should put my collection, please let me know.  I would like to limit this collection to simple things that don't depend on non-Boost libraries, however.
>
>
> Jim Bosch
> _______________________________________________
> Cplusplus-sig mailing list
> [hidden email]
> http://mail.python.org/mailman/listinfo/cplusplus-sig
_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig