Commit Graph

1036 Commits

Author SHA1 Message Date
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
Jason Rhinelander
116d37c9ba Use 'override' rather than 'virtual' for overrides
Minor change that makes this example more compliant with the C++ Core
Guidelines.
2016-09-11 01:16:19 -04:00
Jason Rhinelander
6eca083e4c Fix doc typo
"trampoline" is doubled in the first sentence.
2016-09-11 01:16:19 -04:00
Wenzel Jakob
f217c04195 Merge pull request #402 from aldanor/feature/numpy-c-api
Add array methods via C API
2016-09-11 08:34:04 +09:00
Ivan Smirnov
aca6bcaea5 Add tests for array data access /index methods 2016-09-10 16:42:17 +01: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
3d1bb29ee5 Merge pull request #406 from wjakob/master
operators should return NotImplemented given unsupported input (fixes #393)
2016-09-10 16:02:53 +09:00
Wenzel Jakob
382484ae56 operators should return NotImplemented given unsupported input (fixes #393) 2016-09-10 15:34:26 +09:00
Wenzel Jakob
8d38ebed91 Merge pull request #405 from jagerman/retry-apt-get
apt-get tweaks for the docker/debian builds
2016-09-10 13:46:57 +09:00
Jason Rhinelander
bf14cea7c7 apt-get tweaks for the debian builds
- Try to update and upgrade twice (with a brief pause between attempts)
  to deal with occassional spurious server failures or repository race
  conditions.  Do the same for the main package install.
- Use dist-upgrade instead of upgrade for updating the image
- Add -q to the upgrade and install commands to make apt less verbose.
2016-09-10 00:04:09 -04: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
Wenzel Jakob
356bf94a85 Merge pull request #401 from jagerman/templates-simplification
Small template simplifications
2016-09-09 08:44:33 +09: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
Wenzel Jakob
39577e8c62 Merge pull request #396 from aldanor/feature/is_none
Add handle::is_none()
2016-09-08 09:03:02 +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
Wenzel Jakob
837fda20d7 Merge pull request #391 from dean0x7d/cout-to-print
Replace std::cout with py::print in tests
2016-09-07 16:44:14 +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
Dean Moldovan
81511be341 Replace std::cout with py::print in tests
With this change both C++ and Python write to sys.stdout which resolves
the capture issues noted in #351. Therefore, the related workarounds are
removed.
2016-09-07 01:25:27 +02: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
Wenzel Jakob
a3dbdc67f5 Merge pull request #372 from dean0x7d/keywords
Keyword arguments and generalized unpacking for C++ API
2016-09-07 00:31:03 +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
625bd48a91 Document calling function with keyword arguments from C++ 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
6f017cf658 basics.rst: minor table update for Eigen types 2016-09-06 14:13:35 +09:00