Commit Graph

87 Commits

Author SHA1 Message Date
Jason Rhinelander
ed14879a19 Move support for return values of called Python functions
Currently pybind11 always translates values returned by Python functions
invoked from C++ code by copying, even when moving is feasible--and,
more importantly, even when moving is required.

The first, and relatively minor, concern is that moving may be
considerably more efficient for some types.  The second problem,
however, is more serious: there's currently no way python code can
return a non-copyable type to C++ code.

I ran into this while trying to add a PYBIND11_OVERLOAD of a virtual
method that returns just such a type: it simply fails to compile because
this:

    overload = ...
    overload(args).template cast<ret_type>();

involves a copy: overload(args) returns an object instance, and the
invoked object::cast() loads the returned value, then returns a copy of
the loaded value.

We can, however, safely move that returned value *if* the object has the
only reference to it (i.e. if ref_count() == 1) and the object is
itself temporary (i.e. if it's an rvalue).

This commit does that by adding an rvalue-qualified object::cast()
method that allows the returned value to be move-constructed out of the
stored instance when feasible.

This basically comes down to three cases:

- For objects that are movable but not copyable, we always try the move,
  with a runtime exception raised if this would involve moving a value
  with multiple references.
- When the type is both movable and non-trivially copyable, the move
  happens only if the invoked object has a ref_count of 1, otherwise the
  object is copied.  (Trivially copyable types are excluded from this
  case because they are typically just collections of primitive types,
  which can be copied just as easily as they can be moved.)
- Non-movable and trivially copy constructible objects are simply
  copied.

This also adds examples to example-virtual-functions that shows both a
non-copyable object and a movable/copyable object in action: the former
raises an exception if returned while holding a reference, the latter
invokes a move constructor if unreferenced, or a copy constructor if
referenced.

Basically this allows code such as:

    class MyClass(Pybind11Class):
        def somemethod(self, whatever):
            mt = MovableType(whatever)
            # ...
            return mt

which allows the MovableType instance to be returned to the C++ code
via its move constructor.

Of course if you attempt to violate this by doing something like:

    self.value = MovableType(whatever)
    return self.value

you get an exception--but right now, the pybind11-side of that code
won't compile at all.
2016-08-08 13:47:37 -04:00
Dean Moldovan
ed23dda93b Adopt PEP 484 type hints for C++ types exported to Python 2016-08-04 23:47:07 +02:00
Wenzel Jakob
6969e7c1ba exception handler tweaks (fixes #284) 2016-07-18 10:46:41 +02:00
Pim Schellart
5a7d17ff16 Add support for user defined exception translators 2016-07-11 17:33:04 -04:00
Jason Rhinelander
8469f751cb Add _<bool>("s1", "s2") ternary & use TYPE_CASTER
This commit adds an additional _ template function for compile-time
selection between two description strings.  This in turn allows the
elimination of needing two name() methods in type_caster<arithmetic
types> and type_caster<eigen types>, which allows them to start using
PYBIND11_TYPE_CASTER instead, simplifying their code by eliminating all
the code that they are duplicating from the macro.
2016-07-06 00:40:54 -04:00
Merlin Nimier-David
68ca3b6343 more informative error message when py::make_tuple fails 2016-07-01 20:35:35 +02:00
Wenzel Jakob
0006259508 switched internal usage of cast_error exception to reference_cast_error 2016-07-01 16:07:35 +02:00
Wenzel Jakob
472ffbba96 more informative error message when py::cast fails 2016-07-01 16:07:24 +02:00
Wenzel Jakob
37e1f61f54 allow passing a 'return value policy' to handle::operator() 2016-06-22 14:29:13 +02:00
Wenzel Jakob
0a07805ab6 fixed many conversion warnings on clang 2016-05-29 13:40:40 +02:00
Wenzel Jakob
69e1a5c91b quenched warnings on windows, comments in gil_scoped_release 2016-05-26 14:29:31 +02:00
Wenzel Jakob
3f200fab22 don't implicitly convert doubles to ints 2016-05-17 15:35:29 +02:00
Wenzel Jakob
178c8a899d nicer type_caster::load() calling conventions 2016-05-15 20:23:27 +02:00
Wenzel Jakob
6c03beb867 enable *args and **kwargs notation (closes #190) 2016-05-08 14:34:09 +02:00
Wenzel Jakob
ad69634907 minor cleanups 2016-05-03 13:36:32 +02:00
Wenzel Jakob
aadc2f3d8c cast(): use cast_op_type mechanism 2016-05-03 13:27:57 +02:00
Wenzel Jakob
bd57eb484d don't try to cast 'None' into a C++ lvalue reference 2016-05-01 14:42:20 +02:00
Wenzel Jakob
2bc946bd7a copy/move constructor detection workaround (MSVC 2015 bug) 2016-05-01 10:28:00 +02:00
Wenzel Jakob
8edfa0c10f further simplifications to shared_ptr caster 2016-04-30 23:01:06 +02:00
Wenzel Jakob
ffd85b46d8 return value policy fix for functions returning std::shared_ptr<> (fixes #187) 2016-04-30 22:41:05 +02:00
Wenzel Jakob
772c6d54d6 enable passing C++ instances to void*-valued arguments 2016-04-30 21:28:45 +02:00
Wenzel Jakob
e8b9dd263c fixed docstring generation for void pointers 2016-04-30 19:35:24 +02:00
Wenzel Jakob
fd7cf51a56 enabled casting of const void* 2016-04-30 19:25:37 +02:00
Wenzel Jakob
06f56ee1e9 opaque type redesign 2016-04-28 17:49:46 +02:00
Wenzel Jakob
f7b5874ca3 new move value policy 2016-04-25 23:04:27 +02:00
Wenzel Jakob
fbafdea672 a few more GIL-related compatibility fixes 2016-04-25 17:48:56 +02:00
Wenzel Jakob
39e97e6a49 significant redesign of GIL state handling 2016-04-25 03:27:32 +02:00
Wenzel Jakob
dbe43ffcce completed implicit type casters for reference_wrapper 2016-04-21 12:21:14 +02:00
Wenzel Jakob
f54ded74f1 added caster for std::reference_wrapper (fixes #171) 2016-04-20 17:00:57 +02:00
Wenzel Jakob
e65e58716e slight generalization of cast_op_type 2016-04-18 21:22:22 +02:00
Wenzel Jakob
8cb6cb33ef minor cleanups in common.h; updated author info and copyright year 2016-04-18 10:53:38 +02:00
Wenzel Jakob
1dc940d4b7 quenched -pendantic warnings (closes #167) 2016-04-18 10:41:44 +02:00
Wenzel Jakob
8bd31c71e4 New automatic return value policy for cat() and make_tuple(), fixed an ambiguity issue 2016-04-14 15:00:30 +02:00
Wenzel Jakob
d7efa4ff7b return best representation of polymorphic types (fixes #105) 2016-04-13 13:51:33 +02:00
Wenzel Jakob
505466ff0e added variadic make_tuple() function 2016-04-13 02:49:03 +02:00
Wenzel Jakob
9f0dfce84b returning unique pointers is now allowed 2016-04-06 17:38:18 +02:00
Wenzel Jakob
a3ee1a4591 improved pair type caster (fixes #155) 2016-04-06 11:51:00 +02:00
Wenzel Jakob
b90e68de7e another corner case (fixes #151) 2016-03-27 00:19:32 +01:00
Wenzel Jakob
08927e9809 tabs->spaces 2016-03-26 23:38:46 +01:00
Wenzel Jakob
9883ec01d7 allow nullptr string values (closes #138) 2016-03-26 23:37:51 +01:00
Wenzel Jakob
d3349af48b modularized logic in preceding change, fixed issue with char (fixes #150) 2016-03-26 23:04:10 +01:00
Wenzel Jakob
0772967ecf allow nullptr when passing void* values 2016-03-26 20:41:28 +01:00
Wenzel Jakob
de1bca864e added feature to pass void* pointers by popular demand 2016-03-26 17:51:09 +01:00
Wenzel Jakob
12cf543804 address compilation issue in MSVC 2016-03-26 17:29:33 +01:00
Wenzel Jakob
048373c1b4 explicitly route type casting operations to desired casting operator (fixes #147) 2016-03-26 17:21:19 +01:00
Wenzel Jakob
eda978e003 support for opaque types 2016-03-15 15:07:55 +01:00
Wenzel Jakob
17cdb06c1b fix severe regression involving character arrays (fixes #137) 2016-03-10 13:24:10 +01:00
Wenzel Jakob
81dfd2c51f Working type casters for wide strings and wide characters 2016-03-08 20:48:07 +01:00
Wenzel Jakob
309a85ba59 support std::shared_ptr<const X> and types that indirectly derive from std::enable_shared_from_this 2016-03-08 17:59:10 +01:00
Wenzel Jakob
77586fd6a2 add version suffix to internal pybind11 data structures to avoid conflicts with future versions 2016-03-06 13:50:26 +01:00