Commit Graph

570 Commits

Author SHA1 Message Date
Jason Rhinelander
c03db9bad9 Fail static_assert when trying to reference non-referencable types
The previous commit to address #392 triggers a compiler warning about
returning a reference to a local variable, which is *not* a false alarm:
the following:

    py::cast<int &>(o)

(which happens internally in an overload declaration) really is
returning a reference to a local, because the cast operators for the
type_caster for numeric types returns a reference to its own member.

This commit adds a static_assert to make that a compilation failure
rather than returning a reference into about-to-be-freed memory.

Incidentally, this is also a fix for #219, which is exactly the same
issue: we can't reference numeric primitives that are cast from
wrappers around python numeric types.
2016-09-07 16:07:59 -04:00
Ivan Smirnov
392f16ccb8 Properly format type name in error_already_set() 2016-09-07 20:36:28 +01:00
Jason Rhinelander
56f717756b Fix type caster for heap reference types
Need to use the intrinsic type, not the raw type.

Fixes #392.
2016-09-07 14:14:11 -04:00
Wenzel Jakob
8706fb9085 Intel compiler 2017 fix 2016-09-07 23:49:16 +09:00
Wenzel Jakob
6fd3132e81 Merge pull request #385 from jagerman/relax-class-arguments
Allow arbitrary class_ template option ordering
2016-09-07 23:49:00 +09:00
Jason Rhinelander
6b52c838d7 Allow passing base types as a template parameter
This allows a slightly cleaner base type specification of:

    py::class_<Type, Base>("Type")

as an alternative to

    py::class_<Type>("Type", py::base<Base>())

As with the other template parameters, the order relative to the holder
or trampoline types doesn't matter.

This also includes a compile-time assertion failure if attempting to
specify more than one base class (but is easily extendible to support
multiple inheritance, someday, by updating the class_selector::set_bases
function to set multiple bases).
2016-09-06 20:34:24 -04:00
Jason Rhinelander
5fffe200e3 Allow arbitrary class_ template option ordering
The current pybind11::class_<Type, Holder, Trampoline> fixed template
ordering results in a requirement to repeat the Holder with its default
value (std::unique_ptr<Type>) argument, which is a little bit annoying:
it needs to be specified not because we want to override the default,
but rather because we need to specify the third argument.

This commit removes this limitation by making the class_ template take
the type name plus a parameter pack of options.  It then extracts the
first valid holder type and the first subclass type for holder_type and
trampoline type_alias, respectively.  (If unfound, both fall back to
their current defaults, `std::unique_ptr<type>` and `type`,
respectively).  If any unmatched template arguments are provided, a
static assertion fails.

What this means is that you can specify or omit the arguments in any
order:

    py::class_<A, PyA> c1(m, "A");
    py::class_<B, PyB, std::shared_ptr<B>> c2(m, "B");
    py::class_<C, std::shared_ptr<C>, PyB> c3(m, "C");

It also allows future class attributes (such as base types in the next
commit) to be passed as class template types rather than needing to use
a py::base<> wrapper.
2016-09-06 12:22:13 -04:00
Wenzel Jakob
c84b37b577 fix bogus return value policy fallbacks (fixes #389) 2016-09-07 00:47:17 +09:00
Dean Moldovan
60b26802fd Make keyword argument hold a py::object instead of T*
With this change arg_t is no longer a template, but it must remain so
for backward compatibility. Thus, a non-template arg_v is introduced,
while a dummy template alias arg_t is there to keep old code from
breaking. This can be remove in the next major release.

The implementation of arg_v also needed to be placed a little earlier in
the headers because it's not a template any more and unpacking_collector
needs more than a forward declaration.
2016-09-06 16:41:50 +02:00
Dean Moldovan
8fe13b8896 Apply make_caster and intrinsic_t aliases everywhere 2016-09-06 16:41:50 +02:00
Dean Moldovan
56e86ed094 Workaround for py::dict() constructor on MSVC
MSVC fails to compile if the constructor is defined out-of-line.
The error states that it cannot deduce the type of the default template
parameter which is used for SFINAE.
2016-09-06 16:41:50 +02:00
Dean Moldovan
16db1bfbd7 Remove superseded handle::operator() overloads
The variadic handle::operator() offers the same functionality as well
as mixed positional, keyword, * and ** arguments. The tests are also
superseded by the ones in `test_callbacks`.
2016-09-06 16:41:50 +02:00
Dean Moldovan
15a112f8ff Add py::dict() keyword constructor 2016-09-06 16:41:50 +02:00
Dean Moldovan
66aa2728f4 Add py::str::format() method 2016-09-06 16:41:50 +02:00
Dean Moldovan
67990d9e19 Add py::print() function
Replicates Python API including keyword arguments.
2016-09-06 16:41:50 +02:00
Dean Moldovan
c743e1b1b4 Support keyword arguments and generalized unpacking in C++
A Python function can be called with the syntax:
```python
foo(a1, a2, *args, ka=1, kb=2, **kwargs)
```
This commit adds support for the equivalent syntax in C++:
```c++
foo(a1, a2, *args, "ka"_a=1, "kb"_a=2, **kwargs)
```

In addition, generalized unpacking is implemented, as per PEP 448,
which allows calls with multiple * and ** unpacking:
```python
bar(*args1, 99, *args2, 101, **kwargs1, kz=200, **kwargs2)
```
and
```c++
bar(*args1, 99, *args2, 101, **kwargs1, "kz"_a=200, **kwargs2)
```
2016-09-06 16:41:50 +02:00
Dean Moldovan
317524ffad Make arg_t hold a pointer instead of a copy of the value 2016-09-06 14:39:30 +02:00
Wenzel Jakob
146397ecf4 allow iterators with different RV policies (fixes #388) 2016-09-06 13:06:31 +09:00
Wenzel Jakob
fe34241e50 minor doc & style fixes 2016-09-06 13:02:29 +09:00
Sergey Lyskov
7520418e26 Adding bind_map 2016-09-05 17:11:16 -04:00
Wenzel Jakob
8ac9715f84 enum serialization support (fixes #380) 2016-09-05 17:20:50 +09:00
Wenzel Jakob
614988c875 Merge pull request #384 from jagerman/unique-ptr-non-default-deleters
Make unique_ptr's with non-default deleters work
2016-09-05 08:26:34 +09:00
Wenzel Jakob
cc4e4065b3 .. and another one 2016-09-05 08:25:10 +09:00
Jason Rhinelander
a6495af87a Make unique_ptr's with non-default deleters work
Currently pybind11 only supports std::unique_ptr<T> holders by default
(other holders can, of course, be declared using the macro).  PR #368
added a `py::nodelete` that is intended to be used as:

    py::class_<Type, std::unique_ptr<Type, py::nodelete>> c("Type");

but this doesn't work out of the box.  (You could add an explicit
holder type declaration, but this doesn't appear to have been the
intention of the commit).

This commit fixes it by generalizing the unique_ptr type_caster to take
both the type and deleter as template arguments, so that *any*
unique_ptr instances are now automatically handled by pybind.  It also
adds a test to test_smart_ptr, testing both that py::nodelete (now)
works, and that the object is indeed not deleted as intended.
2016-09-04 18:23:55 -04:00
Wenzel Jakob
f3be07c661 minor code style fixes 2016-09-04 23:03:48 +09:00
Wenzel Jakob
987be18fee Merge pull request #379 from nevion/buffer_info
Buffer info improvements
2016-09-04 23:02:20 +09:00
Jason Newton
10d46e7f73 explicitly delete copy-ctor and assignment operator 2016-09-02 18:39:47 -04:00
Jason Newton
4764698069 add move ctor and move-assignment operator 2016-09-02 18:37:13 -04:00
Jason Newton
514c6dad70 add field for ownership 2016-09-02 17:10:50 -04:00
Jason Newton
3718c38e68 default all fields in all ctors 2016-09-02 17:10:02 -04:00
Jason Rhinelander
2097826346 Fix template trampoline overload lookup failure
Problem
=======

The template trampoline pattern documented in PR #322 has a problem with
virtual method overloads in intermediate classes in the inheritance
chain between the trampoline class and the base class.

For example, consider the following inheritance structure, where `B` is
the actual class, `PyB<B>` is the trampoline class, and `PyA<B>` is an
intermediate class adding A's methods into the trampoline:

    PyB<B> -> PyA<B> -> B -> A

Suppose PyA<B> has a method `some_method()` with a PYBIND11_OVERLOAD in
it to overload the virtual `A::some_method()`.  If a Python class `C` is
defined that inherits from the pybind11-registered `B` and tries to
provide an overriding `some_method()`, the PYBIND11_OVERLOADs declared
in PyA<B> fails to find this overloaded method, and thus never invoke it
(or, if pure virtual and not overridden in PyB<B>, raises an exception).

This happens because the base (internal) `PYBIND11_OVERLOAD_INT` macro
simply calls `get_overload(this, name)`; `get_overload()` then uses the
inferred type of `this` to do a type lookup in `registered_types_cpp`.
This is where it fails: `this` will be a `PyA<B> *`, but `PyA<B>` is
neither the base type (`B`) nor the trampoline type (`PyB<B>`).  As a
result, the overload fails and we get a failed overload lookup.

The fix
=======

The fix is relatively simple: we can cast `this` passed to
`get_overload()` to a `const B *`, which lets get_overload look up the
correct class.  Since trampoline classes should be derived from `B`
classes anyway, this cast should be perfectly safe.

This does require adding the class name as an argument to the
PYBIND11_OVERLOAD_INT macro, but leaves the public macro signatures
unchanged.
2016-08-29 19:41:44 -04:00
Wenzel Jakob
5e4e477b8b minor fixes to PR #368 2016-08-28 02:03:15 +02:00
Wenzel Jakob
a3906778eb minor: renamed argument in array constructor 2016-08-28 01:55:07 +02:00
Nickolai Belakovski
6333825350 Added support for exposing classes with private destructors and corresponding documentation 2016-08-27 15:09:15 -07:00
Wenzel Jakob
324c9c521b minor Intel compiler fix 2016-08-26 16:52:45 +02:00
Ivan Smirnov
d8b11b8708 Fix dtype::strip_padding() on Intel compiler 2016-08-25 21:52:52 +01:00
Wenzel Jakob
9a777a263d numpy.h: fix test suite issues on the Intel Compiler 2016-08-25 02:18:00 +02:00
Wenzel Jakob
89f2db4596 Merge pull request #353 from aldanor/feature/generalized-iterators
Add support for iterators with different begin/end types
2016-08-25 01:47:38 +02:00
Wenzel Jakob
1ffce7422d Get pybind11 test suite to compile on the Intel compiler (more or less..)
- ICPC can't handle the NCVirt trampoline which returns a non-copyable
  type, which is likely due to a constexpr/SFINAE issue. This disables
  the test on that compiler so that at least the rest can be tested.
2016-08-25 01:43:35 +02:00
Ivan Smirnov
1c8828fe8f Fix int_ shadowing problem in detail namespace
If operators.h is included, int_ function in the `detail`
namespace will shadow pybind11::int_ type, so the fully qualified
name has to be used.
2016-08-25 00:33:02 +01:00
Ivan Smirnov
2b308e01f7 Add support for iterators with differing end type 2016-08-24 23:29:04 +01:00
Ivan Smirnov
c5a1c8a6b9 Don't require operator-> for key iterators 2016-08-24 23:27:19 +01:00
Wenzel Jakob
8de0437e46 type_caster<std::function>: allow None values in both directions 2016-08-18 11:18:12 +02:00
Glen Walker
f45bb585c3 Support keep_alive where nurse may be None
For example keep_alive<0,1>() should work where the return value may sometimes be None. At present a "Could not allocate weak reference!" exception is thrown.
Update documentation to clarify behaviour of keep_alive when nurse is None or does not support weak references.
2016-08-18 09:09:41 +12:00
Ivan Smirnov
6956b655f0 Simplify code in eigen.h using new array ctors 2016-08-15 18:41:54 +01:00
Ivan Smirnov
67b3daeea4 Always decay type param of npy_format_descriptor 2016-08-15 18:41:54 +01:00
Ivan Smirnov
edbd4cb0a7 Decay const qualifiers in is_pod_struct<> 2016-08-15 18:41:54 +01:00
Ivan Smirnov
03fb488579 format_descriptor::format() now yields std::string
This is required since format descriptors for string types that
were using PYBIND11_DESCR were causing problems on C++14 on Linux.

Although this is technically a breaking change, it shouldn't cause
problems since the only use of format strings is passing them to
buffer_info constructor which expects std::string.

Note: for non-structured types, the const char * value is still
accessible via ::value for compatibility purpose.
2016-08-15 00:40:29 +01:00
Ivan Smirnov
6715736936 Add handle::repr() method 2016-08-14 13:43:31 +01:00
Ivan Smirnov
7dcbfe228e Add a missing bytes ctor from const char * 2016-08-13 20:00:15 +01:00
Ivan Smirnov
c22fe428ed Change str/bytes cast operators to ctors 2016-08-13 19:39:11 +01:00
Ivan Smirnov
89ec7f3e79 Add (const char *, size_t) ctors for str/bytes 2016-08-13 19:38:50 +01:00
Ivan Smirnov
fd6cede7e9 Avoid extra allocations in operator str/bytes 2016-08-13 15:46:46 +01:00
Ivan Smirnov
61e3b0bd15 Use builtin str type for recarray field names 2016-08-13 12:51:31 +01:00
Ivan Smirnov
1cdd171fbc Add PYBIND11_STR_TYPE to represent builtin str 2016-08-13 12:51:31 +01:00
Ivan Smirnov
006d8b6621 Add casting operators between py::str / py::bytes 2016-08-13 12:51:31 +01:00
Ivan Smirnov
3768b6abf9 Use fully qualified name in PYBIND11_DESCR macro 2016-08-13 12:43:16 +01:00
Ivan Smirnov
ad5ca6d4e6 Added dtype from const char pointer ctor 2016-08-13 12:43:16 +01:00
Ivan Smirnov
c6257f8641 Allow nullptr in array ctors wherever possible 2016-08-13 12:43:16 +01:00
Ivan Smirnov
98ba98c06b Add a simplified buffer_info ctor for 1-D case 2016-08-13 12:43:16 +01:00
Ivan Smirnov
6636ae9d4e Also add the new ctors to py::array_t 2016-08-13 12:43:16 +01:00
Ivan Smirnov
6bb0ee1186 Add all possible ctors for py::array 2016-08-13 12:43:16 +01:00
Ivan Smirnov
d77bc8c343 Add dtype(names, offsets, formats, itemsize) ctor 2016-08-13 12:43:16 +01:00
Ivan Smirnov
fc5620afa6 Fix a segfault where func object wasn't released 2016-08-13 12:43:16 +01:00
Ivan Smirnov
01f7409550 Initial implementation of py::dtype 2016-08-13 12:43:16 +01:00
Ivan Smirnov
05cb58ade2 Cleanup: move numpy API bindings out of py::array 2016-08-13 12:43:16 +01:00
Ivan Smirnov
afb07e7e92 Code reordering / cleanup only 2016-08-13 12:43:16 +01:00
Ivan Smirnov
f9c0defed7 Add numpy wrappers for char[] and std::array<char> 2016-08-13 12:43:16 +01:00
Ivan Smirnov
103d5eadc3 Remove redundant definition 2016-08-13 12:43:16 +01:00
Ivan Smirnov
098f9aef73 Replace 4096B format buffer with std::string 2016-08-13 12:43:16 +01:00
Ivan Smirnov
b37985ee0c Fix a comment and wrong indentation 2016-08-13 12:43:16 +01:00
Ivan Smirnov
bf2510ee86 Make buffer_info::as_pybuffer a memoryview ctor 2016-08-13 12:43:16 +01:00
Ivan Smirnov
41c3399021 Update npy_format_descriptor::name() 2016-08-13 12:43:16 +01:00
Ivan Smirnov
076b953ccd Restore dtype equivalence sanity check 2016-08-13 12:43:16 +01:00
Ivan Smirnov
8f2f7cd61c Various cleanup 2016-08-13 12:43:16 +01:00
Ivan Smirnov
8fa09cb871 Strip padding fields in dtypes, update the tests 2016-08-13 12:43:16 +01:00
Ivan Smirnov
13022f1b8c Bugfix: pass struct size as itemsize to descriptor
Without this, partially bound structs will have incorrect itemsize.
2016-08-13 12:43:16 +01:00
Ivan Smirnov
eeb4c043f9 Change field descriptor offset type to size_t 2016-08-13 12:43:16 +01:00
Ivan Smirnov
511401599c Use malloc insterad of calloc for numpy arrays 2016-08-13 12:43:16 +01:00
Ivan Smirnov
5412a05cf0 Rename PYBIND11_DTYPE to PYBIND11_NUMPY_DTYPE 2016-08-13 12:43:16 +01:00
Ivan Smirnov
872bd92575 Use proper type for an int literal 2016-08-13 12:43:16 +01:00
Ivan Smirnov
4c9a160a1d Exclude double type from is_pod_struct 2016-08-13 12:43:16 +01:00
Ivan Smirnov
3b803846d5 Add a few comments throughout numpy.h 2016-08-13 12:43:16 +01:00
Ivan Smirnov
2f01f01866 Always allocate at least one element 2016-08-13 12:43:16 +01:00
Ivan Smirnov
b38ca22e94 Add a few braces for clarity 2016-08-13 12:43:16 +01:00
Ivan Smirnov
95545e6256 Change PB11_IMPL prefix to PYBIND11, add comment 2016-08-13 12:43:16 +01:00
Ivan Smirnov
7bdd74a9fb Fix PYBIND11_DTYPE to work with MSVC compiler 2016-08-13 12:43:16 +01:00
Ivan Smirnov
5dc6c5445d Cosmetic: fix indentation 2016-08-13 12:43:16 +01:00
Ivan Smirnov
73f56830f8 Add detail::is_pod_struct<T> helper 2016-08-13 12:43:16 +01:00
Ivan Smirnov
a0e37f250e npy_format_descriptor::format() - fail if unbound 2016-08-13 12:43:16 +01:00
Ivan Smirnov
5a47a16e47 Revert accidental whitespace change 2016-08-13 12:43:16 +01:00
Ivan Smirnov
40eadfeb73 Make npy_format_descriptor backwards-compat
The typenum for non-structured types is still accessible at ::value,
and the dtype object for all types is accessible at ::dtype().
2016-08-13 12:43:16 +01:00
Ivan Smirnov
95e9b12322 Prefix the FIELD_DESCRIPTOR macro 2016-08-13 12:43:16 +01:00
Ivan Smirnov
5e71e17bdf Make changes to format_descriptor backwards-compat
The format strings that are known at compile time are now accessible
via both ::value and ::format(), and format strings for everything
else is accessible via ::format(). This makes it backwards compatible.
2016-08-13 12:43:16 +01:00
Ivan Smirnov
4f164217e4 Add dtype_of<T>() function, update the tests 2016-08-13 12:43:16 +01:00
Ivan Smirnov
036e8cd32f Remove erroneous py:: prefix in numpy.h 2016-08-13 12:43:16 +01:00
Ivan Smirnov
873d267471 Prefix all macros in numpy.h to avoid name clashes 2016-08-13 12:43:16 +01:00
Ivan Smirnov
1f54cd9209 Use object instead of ptrs in numpy descriptors 2016-08-13 12:43:16 +01:00
Ivan Smirnov
2a7acb6d55 Incref descriptors properly when creating arrays 2016-08-13 12:43:16 +01:00
Ivan Smirnov
f5b166d042 Simplify npy_format_descriptor slightly 2016-08-13 12:43:16 +01:00
Ivan Smirnov
80a3785a66 Borrow field descriptors for recarray dtype 2016-08-13 12:43:16 +01:00
Ivan Smirnov
2e1565e414 Add empty recarray test, check for calloc fail 2016-08-13 12:43:16 +01:00
Ivan Smirnov
f10c84eb9b Release format descriptor args before converting 2016-08-13 12:43:16 +01:00
Ivan Smirnov
2488b32066 Add PYBIND11_DTYPE macro for registering dtypes 2016-08-13 12:43:16 +01:00
Ivan Smirnov
fab02efb10 Switch away from typenums for numpy descriptors 2016-08-13 12:43:16 +01:00
Ivan Smirnov
a67c2b52e4 Use memoryview for constructing array from buffer 2016-08-13 12:43:16 +01:00
Ivan Smirnov
ea2755ccdc Use a macro for numpy API definitions 2016-08-13 12:43:16 +01:00
Ivan Smirnov
7709d6b77d Add memoryview type 2016-08-13 12:43:16 +01:00
Ivan Smirnov
42ad328481 Change format_descriptor::value to a static func 2016-08-13 12:43:16 +01:00
Ivan Smirnov
a7e62e1ca6 Add buffer_info::as_pybuffer() method 2016-08-13 12:43:16 +01:00
Ivan Smirnov
3dd325b772 Change npy_format_descriptor typenum to static fn 2016-08-13 12:43:16 +01:00
Jason Rhinelander
5aa85be26e Added pybind11::make_key_iterator for map iteration
This allows exposing a dict-like interface to python code, allowing
iteration over keys via:

    for k in custommapping:
        ...

while still allowing iteration over pairs, so that you can also
implement 'dict.items()' functionality which returns a pair iterator,
allowing:

    for k, v in custommapping.items():
        ...

example-sequences-and-iterators is updated with a custom class providing
both types of iteration.
2016-08-11 21:22:05 -04:00
Jason Rhinelander
e20fc61a33 Silence MSVC warning
PR #329 generates the following warning under MSVC:

    ...\cast.h(202): warning C4456: declaration of 'it' hides previous local declaration

This renames the second iterator to silence it.
2016-08-11 16:23:23 -04:00
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
Wenzel Jakob
7c99ff2a00 fix segfault when passing a docstring to def_property* (fixes #222) 2016-06-02 20:34:01 +02:00
Dean Moldovan
f2b36c2ed2 Fix a couple of warnings
- Conversion warning on clang: 'long' to 'size_t'
- Unused variable warning on MSVC
2016-06-01 23:48:23 +02:00
Wenzel Jakob
38d8b8cfe2 don't allow registering a class twice (fixes #218) 2016-05-31 09:53:28 +02:00
Wenzel Jakob
5eda97d7e4 gcc fix 2016-05-30 11:45:02 +02:00
Wenzel Jakob
b569272127 quench some Eigen-related warnings 2016-05-30 11:37:07 +02:00
Wenzel Jakob
5dd33d880d fix issues with std::vector<bool> overload in STL (fixes #216) 2016-05-30 11:28:21 +02:00
Wenzel Jakob
0a07805ab6 fixed many conversion warnings on clang 2016-05-29 13:40:40 +02:00
Wenzel Jakob
c48da92561 Merge branch 'cygwin' of https://github.com/BorisSchaeling/pybind11 into BorisSchaeling-cygwin 2016-05-29 12:46:21 +02:00
Boris Schäling
20ee935203 Use decltype to deduce return type of PyThread_create_key 2016-05-28 12:26:18 +02:00
Boris Schäling
8965644536 Make examples build and run on Cygwin 2016-05-26 22:42:07 +02:00
Wenzel Jakob
69e1a5c91b quenched warnings on windows, comments in gil_scoped_release 2016-05-26 14:29:31 +02:00
Wenzel Jakob
86d825f330 Redesigned virtual call mechanism and user-facing syntax (breaking change!)
Sergey Lyskov pointed out that the trampoline mechanism used to override
virtual methods from within Python caused unnecessary overheads when
instantiating the original (i.e. non-extended) class.

This commit removes this inefficiency, but some syntax changes were
needed to achieve this. Projects using this features will need to make a
few changes:

In particular, the example below shows the old syntax to instantiate a
class with a trampoline:

class_<TrampolineClass>("MyClass")
    .alias<MyClass>()
    ....

This is what should be used now:

class_<MyClass, std::unique_ptr<MyClass, TrampolineClass>("MyClass")
    ....

Importantly, the trampoline class is now specified as the *third*
argument to the class_ template, and the alias<..>() call is gone. The
second argument with the unique pointer is simply the default holder
type used by pybind11.
2016-05-26 13:36:24 +02:00
Wenzel Jakob
60abf299c6 minor cleanups in pytypes.h 2016-05-26 10:47:11 +02:00
Yung-Yu Chen
114bfeb762 pybind11::args should have been derived from tuple
args was derived from list, but cpp_function::dispatcher sends a tuple to it->impl (line #346 and #392 in pybind11.h).  As a result args::size() and args::operator[] don't work at all.  On my mac args::size() returns -1.  Making args a subclass of tuple fixes it.
2016-05-25 21:09:35 +08:00
Wenzel Jakob
1e3be73a52 PYBIND11_OVERLOAD_NAME and PYBIND11_OVERLOAD_PURE_NAME (fixes #205) 2016-05-24 23:42:14 +02:00
Wenzel Jakob
b437867338 eigen.h: relax access to members 2016-05-24 21:40:03 +02:00
Andreas Bergmeier
16d4394845 Increase available information on invocation error. 2016-05-24 09:19:44 +02:00
Wenzel Jakob
a970a579b2 eigen.h: return compile time vectors as 1D NumPy arrays 2016-05-20 12:01:03 +02:00
Wenzel Jakob
b47a9de035 ability to prevent force casts in numpy arguments 2016-05-19 16:02:09 +02:00
Sergey Lyskov
065526a369 Refactoring bind_vector to take std::string to avoid capture of potentially temporary char const * when calling vector_if_insertion_operator 2016-05-17 23:30:58 -04:00
Wenzel Jakob
3f200fab22 don't implicitly convert doubles to ints 2016-05-17 15:35:29 +02:00
Wenzel Jakob
d1bfc4e0f8 support __bool__ on Python 2.x and 3.x 2016-05-16 18:52:50 +02:00
Wenzel Jakob
00c7d6ccc7 Merge branch 'stl_bind' 2016-05-16 12:33:20 +02:00
Wenzel Jakob
25c03cecfa stl_bind redesign & cleanup pass 2016-05-16 12:12:58 +02:00
Wenzel Jakob
affb9f40c5 quench conversion warnings on windows 2016-05-16 00:04:20 +02:00
Wenzel Jakob
deeab558ba quench strict aliasing warnings 2016-05-16 00:04:20 +02:00
Wenzel Jakob
a380ed9259 consider __setstate__ as a constructor 2016-05-16 00:04:17 +02:00
Sergey Lyskov
26aca3d8ad Adding vector ’extend’ member function 2016-05-15 20:46:07 +02:00
Sergey Lyskov
9ee4f92b06 Refactoring vector_binder so it now a function 2016-05-15 20:46:07 +02:00
Sergey Lyskov
2de5af998d Adding workarounds for MSVC 2015 2016-05-15 20:46:07 +02:00
Sergey Lyskov
7b50cbd870 Fixing VC build, apparently MSVC implementation of std::is_default_constructible had no bool() operator 2016-05-15 20:46:07 +02:00
Sergey Lyskov
a95bde1ea5 Adding documentation for value_error. Fixing various formatting issues. Removing redundant binding for C++ style methods. Adding bindings for iterator and slicing protocol. Extending examples. 2016-05-15 20:46:07 +02:00
Sergey Lyskov
25ac21903a properly adding value_error exception 2016-05-15 20:46:07 +02:00
Sergey Lyskov
a315c7a25a Adding Python-style modifiers for vector. Wrapping has_insertion_operator_implementation in _MSC_VER ifdef’s. Adding ‘!=0’ instead of bool cast’s. 2016-05-15 20:46:07 +02:00
Sergey Lyskov
eae7744c0e adding stl_binders 2016-05-15 20:46:07 +02:00
Wenzel Jakob
178c8a899d nicer type_caster::load() calling conventions 2016-05-15 20:23:27 +02:00
Wenzel Jakob
5984baafd0 redesigned cpp_function constructor; significant space savings 2016-05-15 20:23:27 +02:00