Commit Graph

448 Commits

Author SHA1 Message Date
Wenzel Jakob
bad589a477 deprecated py::base<>, added a macro for improved readability 2016-09-19 13:43:47 +02:00
Wenzel Jakob
e99ebaedcf nicer error message for invalid function arguments 2016-09-19 13:43:43 +02:00
Jason Rhinelander
b3794f1087 Added py::register_exception for simple case (#296)
The custom exception handling added in PR #273 is robust, but is overly
complex for declaring the most common simple C++ -> Python exception
mapping that needs only to copy `what()`.  This add a simpler
`py::register_exception<CppExp>(module, "PyExp");` function that greatly
simplifies the common basic case of translation of a simple CppException
into a simple PythonException, while not removing the more advanced
capabilities of defining custom exception handlers.
2016-09-16 08:04:15 +02:00
Jason Rhinelander
4a4fb396e7 Fix build under debug mode
Take load_type by nested type_caster template arguments instead of by
full type_caster type.
2016-09-12 16:21:40 -04:00
Wenzel Jakob
f22683806e Merge pull request #400 from jagerman/add-ref-virtual-macros
Add a way to deal with copied value references
2016-09-12 06:32:39 +09:00
Jason Rhinelander
3e4fe6c0a8 Store a static type_caster rather than the basic type 2016-09-11 12:17:41 -04:00
Jason Rhinelander
f3f53e2b03 Removed unused/unwanted public ref_cast 2016-09-11 11:36:33 -04:00
Wenzel Jakob
b2eda9ac7c Merge pull request #408 from dean0x7d/exc-destructors
Fix Python C API calls in desctuctors triggered by error_already_set
2016-09-11 21:33:33 +09:00
Wenzel Jakob
e3c297f03e Merge pull request #407 from wjakob/fix_iterator
parameterize iterators by return value policy (fixes #388)
2016-09-11 20:02:32 +09:00
Jason Rhinelander
7dfb932e70 Update OVERLOAD macros to support ref/ptr return type overloads
This adds a static local variable (in dead code unless actually needed)
in the overload code that is used for storage if the overload is for
some convert-by-value type (such as numeric values or std::string).

This has limitations (as written up in the advanced doc), but is better
than simply not being able to overload reference or pointer methods.
2016-09-11 01:21:53 -04:00
Ivan Smirnov
f2a0ad5855 array: add direct data access and indexing methods 2016-09-10 16:24:00 +01:00
Ivan Smirnov
91b3d681ad Expose some dtype/array attributes via NumPy C API 2016-09-10 16:24:00 +01:00
Dean Moldovan
f69071ca4b Make it easier to add new binding of builtin Python exceptions 2016-09-10 16:14:36 +02:00
Dean Moldovan
135ba8deaf Make error_already_set fetch and hold the Python error
This clears the Python error at the error_already_set throw site, thus
allowing Python calls to be made in destructors which are triggered by
the exception. This is preferable to the alternative, which would be
guarding every Python API call with an error_scope.

This effectively flips the behavior of error_already_set. Previously,
it was assumed that the error stays in Python, so handling the exception
in C++ would require explicitly calling PyErr_Clear(), but nothing was
needed to propagate the error to Python. With this change, handling the
error in C++ does not require a PyErr_Clear() call, but propagating the
error to Python requires an explicit error_already_set::restore().

The change does not break old code which explicitly calls PyErr_Clear()
for cleanup, which should be the majority of user code. The need for an
explicit restore() call does break old code, but this should be mostly
confined to the library and not user code.
2016-09-10 12:08:32 +02:00
Wenzel Jakob
b212f6c416 parameterize iterators by return value policy (fixes #388) 2016-09-10 17:16:16 +09:00
Wenzel Jakob
720136bfa7 RAII wrapper for error state 2016-09-10 16:32:17 +09:00
Wenzel Jakob
1f2e417d8c Merge pull request #403 from jagerman/alias-initialization
Implement py::init_alias<>() constructors
2016-09-10 16:12:19 +09:00
Wenzel Jakob
382484ae56 operators should return NotImplemented given unsupported input (fixes #393) 2016-09-10 15:34:26 +09:00
Jason Rhinelander
ec62d977c4 Implement py::init_alias<>() constructors
This commit adds support for forcing alias type initialization by
defining constructors with `py::init_alias<arg1, arg2>()` instead of
`py::init<arg1, arg2>()`.  Currently py::init<> only results in Alias
initialization if the type is extended in python, or the given
arguments can't be used to construct the base type, but can be used to
construct the alias.  py::init_alias<>, in contrast, always invokes the
constructor of the alias type.

It looks like this was already the intention of
`py::detail::init_alias`, which was forward-declared in
86d825f330, but was apparently never
finished: despite the existance of a .def method accepting it, the
`detail::init_alias` class isn't actually defined anywhere.

This commit completes the feature (or possibly repurposes it), allowing
declaration of classes that will always initialize the trampoline which
is (as I argued in #397) sometimes useful.
2016-09-09 03:04:09 -04:00
Jason Rhinelander
5aa2cd5eb9 Template simplifications
Switch count_t to use constexpr_sum (under non-MSVC), and then make
all_of_t/any_of_t use it instead of doing the sum itself.

For MSVC, count_t is still done using template recursion, but
all_of_t/any_of_t can also make use of it.
2016-09-08 17:59:50 -04:00
Wenzel Jakob
260b26b3d6 Merge pull request #399 from jagerman/fix-alias-initialization
Fix type alias initialization
2016-09-09 00:39:43 +09:00
Jason Rhinelander
9c6859ee6e Fix type alias initialization
Type alias for alias classes with members didn't work properly: space
was only allocated for sizeof(type), but if we want to be able to put a
type_alias instance there, we need sizeof(type_alias), but
sizeof(type_alias) > sizeof(type) whenever type_alias has members.
2016-09-08 11:10:18 -04:00
Wenzel Jakob
9d7f7a38a7 fixed Py_None reference couting 2016-09-08 22:53:18 +09:00
Wenzel Jakob
5812d64ba2 Merge pull request #394 from jagerman/fix-ref-heap-casts
Fix ref heap casts
2016-09-08 09:05:15 +09:00
Wenzel Jakob
587aa328c6 Merge pull request #395 from aldanor/feature/error-already-set-message
error_already_set improvements
2016-09-08 09:03:41 +09:00
Ivan Smirnov
984c762485 Use handle::is_none() instead of raw ptrs 2016-09-07 21:16:26 +01:00
Ivan Smirnov
f5e8b6d9cb Add handle::is_none() method 2016-09-07 21:16:19 +01:00
Ivan Smirnov
67b54894b2 Set error if it's not set in error_already_set() 2016-09-07 21:10:16 +01:00
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