Commit Graph

307 Commits

Author SHA1 Message Date
Jason Rhinelander
f2ecd8927e Implement reference_internal with a keep_alive
reference_internal requires an `instance` field to track the returned
reference's parent, but that's just a duplication of what
keep_alive<0,1> does, so use a keep alive to do this to eliminate the
duplication.
2016-08-10 12:08:04 -04:00
Jason Rhinelander
1b05ce5bc0 Track registered instances that share a pointer address
The pointer to the first member of a class instance is the same as the
pointer to instance itself; pybind11 has some workarounds for this to
not track registered instances that have a registered parent with the
same address.  This doesn't work everywhere, however: issue #328 is a
failure of this for a mutator operator which resolves its argument to
the parent rather than the child, as is needed in #328.

This commit resolves the issue (and restores tracking of same-address
instances) by changing registered_instances from an unordered_map to an
unordered_multimap that allows duplicate instances for the same pointer
to be recorded, then resolves these differences by checking the type of
each matched instance when looking up an instance.  (A
unordered_multimap seems cleaner for this than a unordered_map<list> or
similar because, the vast majority of the time, the instance will be
unique).
2016-08-09 17:57:59 -04:00
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
cb6cd6954b Fix signedness warnings 2016-08-05 00:06:28 +02:00
Dean Moldovan
ed23dda93b Adopt PEP 484 type hints for C++ types exported to Python 2016-08-04 23:47:07 +02:00
Dean Moldovan
ecced6c5ae Use generic arg names for functions without explicitly named arguments
Example signatures (old => new):
  foo(int) => foo(arg0: int)
  bar(Object, int) => bar(self: Object, arg0: int)

The change makes the signatures uniform for named and unnamed arguments
and it helps static analysis tools reconstruct function signatures from
docstrings.

This also tweaks the signature whitespace style to better conform to
PEP 8 for annotations and default arguments:
  " : " => ": "
  " = " => "="
2016-08-04 23:45:24 +02:00
Jason Rhinelander
9ffb3dda5f Eigen support for special matrix objects
Functions returning specialized Eigen matrices like Eigen::DiagonalMatrix and
Eigen::SelfAdjointView--which inherit from EigenBase but not
DenseBase--isn't currently allowed; such classes are explicitly copyable
into a Matrix (by definition), and so we can support functions that
return them by copying the value into a Matrix then casting that
resulting dense Matrix into a numpy.ndarray.  This commit does exactly
that.
2016-08-04 15:24:41 -04:00
Wenzel Jakob
19637536ac Merge pull request #315 from jagerman/eigen-stride-fix
Fix eigen copying of non-standard stride values
2016-08-04 19:47:17 +02:00
Jason Rhinelander
8657f3083a Fix eigen copying of non-standard stride values
Some Eigen objects, such as those returned by matrix.diagonal() and
matrix.block() have non-standard stride values because they are
basically just maps onto the underlying matrix without copying it (for
example, the primary diagonal of a 3x3 matrix is a vector-like object
with .src equal to the full matrix data, but with stride 4).  Returning
such an object from a pybind11 method breaks, however, because pybind11
assumes vectors have stride 1, and that matrices have strides equal to
the number of rows/columns or 1 (depending on whether the matrix is
stored column-major or row-major).

This commit fixes the issue by making pybind11 use Eigen's stride
methods when copying the data.
2016-08-04 13:21:39 -04:00
Jason Rhinelander
d41a273031 Only support ==/!= int on unscoped enums
This makes the Python interface mirror the C++ interface:
pybind11-exported scoped enums aren't directly comparable to the
underlying integer values.
2016-08-04 00:21:37 -04:00
Jason Rhinelander
613541947a Fix scoped enums and add scoped enum example
PR #309 broke scoped enums, which failed to compile because the added:

    value == value2

comparison isn't valid for a scoped enum (they aren't implicitly
convertible to the underlying type).  This commit fixes it by
explicitly converting the enum value to its underlying type before
doing the comparison.

It also adds a scoped enum example to the constants-and-functions
example that triggers the problem fixed in this commit.
2016-08-04 00:01:39 -04:00
Jason Rhinelander
5fd5074a0b Add support for Eigen::Ref<...> function arguments
Eigen::Ref is a common way to pass eigen dense types without needing a
template, e.g. the single definition `void
func(Eigen::Ref<Eigen::MatrixXd> x)` can be called with any double
matrix-like object.

The current pybind11 eigen support fails with internal errors if
attempting to bind a function with an Eigen::Ref<...> argument because
Eigen::Ref<...> satisfies the "is_eigen_dense" requirement, but can't
compile if actually used: Eigen::Ref<...> itself is not default
constructible, and so the argument std::tuple containing an
Eigen::Ref<...> isn't constructible, which results in compilation
failure.

This commit adds support for Eigen::Ref<...> by giving it its own
type_caster implementation which consists of an internal type_caster of
the referenced type, load/cast methods that dispatch to the internal
type_caster, and a unique_ptr to an Eigen::Ref<> instance that gets
set during load().

There is, of course, no performance advantage for pybind11-using code of
using Eigen::Ref<...>--we are allocating a matrix of the derived type
when loading it--but this has the advantage of allowing pybind11 to bind
transparently to C++ methods taking Eigen::Refs.
2016-08-03 16:50:22 -04:00
Pim Schellart
3d079fbd54 Fix zero valued enum comparison error 2016-08-03 10:36:22 -04:00
Pim Schellart
e5b42ef1fe Enable comparisons between enums and their underlying types 2016-08-02 11:33:48 -04:00
Trygve Laugstøl
3572bc3e82 Changes accessor::operator=() to throw error_already_set() instead of using pybind11_fail().
PyObject_SetItem and PyObject_SetAttr both throws an exception on
failure so this will show the underlying exception instead of masking
it.

Fixes #303.
2016-08-01 08:45:16 +02:00
Wenzel Jakob
a771e362df check for NOMINMAX issue on windows (fixes #291) 2016-07-19 17:47:59 +02:00
Wenzel Jakob
4a87933be9 descr<> fix for int-to-string conversion 2016-07-19 11:59:37 +02:00
Wenzel Jakob
1f66a58427 pybind11.h: minor cleanups (no functionality change) 2016-07-18 10:47:10 +02:00
Wenzel Jakob
6969e7c1ba exception handler tweaks (fixes #284) 2016-07-18 10:46:41 +02:00
Jason Rhinelander
4e45e1805b Fix #283: don't print first arg of constructor
This changes the exception error message of a bad-arguments error to
suppress the constructor argument when the failure is a constructor.

This changes both the "Invoked with: " output to omit the object
instances, and rewrites the constructor signature to make it look
like a constructor (changing the first argument to the object name, and
removing the ' -> NoneType' return type.
2016-07-17 17:47:05 -04:00
Wenzel Jakob
a720a6046e updated changelog with v1.8.1, updated release instructions 2016-07-12 18:03:17 +02:00
Wenzel Jakob
c47d498c35 fix rare GC issue during type creation (fixes #277) 2016-07-11 23:41:15 +02:00
Wenzel Jakob
3c6ada3a48 Merge pull request #273 from lsst-dm/master
Add support for user defined exception translators
2016-07-11 23:38:21 +02:00
Pim Schellart
5a7d17ff16 Add support for user defined exception translators 2016-07-11 17:33:04 -04:00
Wenzel Jakob
954b7932fe avoid C++ -> Python -> C++ overheads when passing around function objects 2016-07-10 10:44:44 +02:00
Wenzel Jakob
5ba89c340c quench warnings in eigen.h 2016-07-09 15:44:54 +02:00
Wenzel Jakob
678d59d21f Python 2.7 fixes for eval() 2016-07-08 15:14:48 +02:00
Wenzel Jakob
464c43513e further eval() improvements 2016-07-08 15:08:30 +02:00
Wenzel Jakob
dc13cf0e54 quench warning on windows 2016-07-08 11:07:02 +02:00
Wenzel Jakob
0d3fc3566a complete rewrite of eval/exec patch 2016-07-08 10:52:10 +02:00
Klemens Morgenstern
c6ad2c4993 added exec functions 2016-07-08 10:05:24 +02:00
Jason Rhinelander
0b12f91fa3 Only disable placement-new warning under gcc >= 6
Otherwise this would create unknown option warnings under g++ < 6.
2016-07-07 16:26:04 -04:00
Jason Rhinelander
cae0e00947 Disable -Wplacement-new warning false alarm
GCC-6 adds a -Wplacement-new warning that warns for placement-new into a
space that is too small, which is sometimes being triggered here (e.g.
example5 always generates the warning under g++-6).  It's a false
warning, however: the line immediately before just checked the size, and
so this line is never going to actually be reached in the cases where
the GCC warning is being triggered.

This localizes the warning disabling just to this one spot as there are
other placement-new uses in pybind11 where this warning could warn about
legitimate future problems.
2016-07-07 16:11:42 -04:00
Jason Rhinelander
4609beb46e Merge remote-tracking branch 'upstream/master' into ternary-description 2016-07-06 00:49:49 -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
Ben North
93594a3857 Fix handling of one-dimensional input arrays
In eigen.h, type_caster<Type>::load():  For the 'ndim == 1' case, use
the 'InnerStride' type because there is only an inner stride for a
vector.  Choose between (n_elts x 1) or (1 x n_elts) according to
whether we're constructing a Vector or a RowVector.
2016-07-05 21:13:24 +01: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
hbruintjes
70d2e57b09 Cast to derived holder using container type when using std::enable_shared_from_this.
Allows const types to be used by shared_ptr
2016-07-01 12:39:55 +02:00
Wenzel Jakob
2353b9b8fa quench C4459 warning on MSVC2015 2016-06-27 16:05:46 +02:00
Wenzel Jakob
37e1f61f54 allow passing a 'return value policy' to handle::operator() 2016-06-22 14:29:13 +02:00
Wenzel Jakob
4a53d38bd4 only deprecate call() function if C++14 is available 2016-06-22 14:02:27 +02:00
Ivan Smirnov
daed1abc9c Switch to using prefix increment in make_iterator 2016-06-17 22:50:34 +01:00
Ivan Smirnov
3ae5bd787f Remove extraneous semicolon (-pedantic warning) 2016-06-17 22:29:10 +01:00
Jerry Gamache
b8f2d1886c Remove unnecessary braces. 2016-06-15 12:55:34 -04:00
Jerry Gamache
c6e0cdfa54 Allow pybind11::arg to have 0, false, or "" as default values. 2016-06-15 12:48:15 -04:00
Wenzel Jakob
f85c52901e starting to work on release v1.9 2016-06-14 15:24:47 +02:00
Dean Moldovan
9e62558d52 Check the number of named arguments at compile time 2016-06-04 00:27:32 +02:00
Dean Moldovan
96017dd7cd Add _a literal for named arguments 2016-06-03 23:15:22 +02:00