Commit Graph

1416 Commits

Author SHA1 Message Date
Jason Rhinelander b8ac438386 Use dynamic cast for shared_from_this holder init
Using a dynamic_cast instead of a static_cast is needed to safely cast
from a base to a derived type.  The previous static_pointer_cast isn't
safe, however, when downcasting (and fails to compile when downcasting
with virtual inheritance).

Switching this to always use a dynamic_pointer_cast shouldn't incur any
additional overhead when a static_pointer_cast is safe (i.e. when
upcasting, or self-casting): compilers don't need RTTI checks in those
cases.
2017-05-22 11:43:21 -04:00
Jason Rhinelander 35998a0314 Define both __div__ and __truediv__ for Python 2
Python 2 requires both `__div__` and `__truediv__` (and variants) for
compatibility with both regular Python 2 and Python 2 under `from
__future__ import division`.  Without both, division fails in one or the
other case.
2017-05-21 19:15:25 -04:00
Jason Rhinelander 1ac51a02f7 Minor operators.h style cleanups
- realign \ at end of macro lines
- use 'using A = B;' rather than 'typedef B A;'
- use conditional_t
2017-05-21 19:15:25 -04:00
Jason Rhinelander acad05cb13 Fix /= operator under Python 3
The Python method for /= was set as `__idiv__`, which should be
`__itruediv__` under Python 3.

This wasn't totally broken in that without it defined, Python constructs
a new object by calling __truediv__.  The operator tests, however,
didn't actually test the /= operator: when I added it, I saw an extra
construction, leading to the problem.  This commit also includes tests
for the previously untested *= operator, and adds some element-wise
vector multiplication and division operators.
2017-05-21 19:15:25 -04:00
Jason Rhinelander d2da33a34a static_assert should be testing ssize_t not size_t
The numpy strides/sizes/etc. are signed now, but the static_assert
didn't get updated to match.
2017-05-20 11:40:40 -04:00
Jason Rhinelander a4d0d95e2e Make static internals ptr pybind version specific
Under gcc, the `static internals *internals_ptr` is shared across .so's,
which breaks for obvious reasons.

This commit fixes it by moving the static pointer declaration into a
pybind-version-templated function.
2017-05-18 12:40:26 -04:00
Jason Rhinelander 731a9f6cea Fix Py_buffer leak on GetBuffer failure
Fixes #852.
2017-05-16 09:38:58 -04:00
Dean Moldovan 4567f1f82a Fix Eigen shape assertion error in dense matrix caster
Missing conformability check was causing Eigen to create a 0x0 matrix
with an error in debug mode and silent corruption in release mode.
2017-05-11 16:10:40 +02:00
Dean Moldovan 94d0a9f7bc Improve constructor resolution in variant_caster
Currently, `py::int_(1).cast<variant<double, int>>()` fills the `double`
slot of the variant. This commit switches the loader to a 2-pass scheme
in order to correctly fill the `int` slot.
2017-05-10 17:47:57 +02:00
Jason Rhinelander 93e3eac6f9 Defer None loading to second pass
Many of our `is_none()` checks in type caster loading return true, but
this should really be considered a deferral so that, for example, an
overload with a `py::none` argument would win over one that takes
`py::none` as a null option.

This keeps None-accepting for the `!convert` pass only for std::optional
and void casters.  (The `char` caster already deferred None; this just
extends that behaviour to other casters).
2017-05-10 10:44:19 -04:00
Jason Rhinelander 7fb01ecd9c Fix gcc 7 warning
Under gcc 7 with -std=c++11, compilation results in several of the
following warnings:

    In file included from /home/jagerman/src/pybind11/tests/test_sequences_and_iterators.cpp:13:0:
    /home/jagerman/src/pybind11/include/pybind11/operators.h: In function ‘pybind11::detail::op_<(pybind11::detail::op_id)0, (pybind11::detail::op_type)0, pybind11::detail::self_t, pybind11::detail::self_t> pybind11::detail::operator+(const pybind11::detail::self_t&, const pybind11::detail::self_t&)’:
    /home/jagerman/src/pybind11/include/pybind11/operators.h:78:76: warning: inline declaration of ‘pybind11::detail::op_<(pybind11::detail::op_id)0, (pybind11::detail::op_type)0, pybind11::detail::self_t, pybind11::detail::self_t> pybind11::detail::operator+(const pybind11::detail::self_t&, const pybind11::detail::self_t&)’ follows declaration with attribute noinline [-Wattributes]
     inline op_<op_##id, op_l, self_t, self_t> op(const self_t &, const self_t &) {         \
                                                                                ^
    /home/jagerman/src/pybind11/include/pybind11/operators.h:109:1: note: in expansion of macro ‘PYBIND11_BINARY_OPERATOR’
     PYBIND11_BINARY_OPERATOR(add,       radd,         operator+,    l + r)
     ^~~~~~~~~~~~~~~~~~~~~~~~
    In file included from /home/jagerman/src/pybind11/include/pybind11/cast.h:15:0,
                     from /home/jagerman/src/pybind11/include/pybind11/attr.h:13,
                     from /home/jagerman/src/pybind11/include/pybind11/pybind11.h:36,
                     from /home/jagerman/src/pybind11/tests/pybind11_tests.h:2,
                     from /home/jagerman/src/pybind11/tests/test_sequences_and_iterators.cpp:11:
    /home/jagerman/src/pybind11/include/pybind11/descr.h:116:36: note: previous definition of ‘pybind11::detail::descr pybind11::detail::operator+(pybind11::detail::descr&&, pybind11::detail::descr&&)’ was here
         PYBIND11_NOINLINE descr friend operator+(descr &&d1, descr &&d2) {
                                        ^~~~~~~~

This appears to be happening because gcc is considering implicit
construction of `descr` in some places using addition of two
`descr`-compatible arguments in the `descr.h` c++11 fallback code.
There's no particular reason that this operator needs to be a friend
function: this commit changes it to an rvalue-context member function
operator, which avoids the warning.
2017-05-10 10:42:23 -04:00
Bruce Merry b82c0f0a2d Allow std::complex field with PYBIND11_NUMPY_DTYPE (#831)
This exposed a few underlying issues:

1. is_pod_struct was too strict to allow this. I've relaxed it to
require only trivially copyable and standard layout, rather than POD
(which additionally requires a trivial constructor, which std::complex
violates).

2. format_descriptor<std::complex<T>>::format() returned numpy format
strings instead of PEP3118 format strings, but register_dtype
feeds format codes of its fields to _dtype_from_pep3118. I've changed it
to return PEP3118 format codes. format_descriptor is a public type, so
this may be considered an incompatible change.

3. register_structured_dtype tried to be smart about whether to mark
fields as unaligned (with ^). However, it's examining the C++ alignment,
rather than what numpy (or possibly PEP3118) thinks the alignment should
be. For complex values those are different. I've made it mark all fields
as ^ unconditionally, which should always be safe even if they are
aligned, because we explicitly mark the padding.
2017-05-10 11:36:24 +02:00
Bruce Merry 8e0d832c7d Support arrays inside PYBIND11_NUMPY_DTYPE (#832)
Resolves #800.

Both C++ arrays and std::array are supported, including mixtures like
std::array<int, 2>[4]. In a multi-dimensional array of char, the last
dimension is used to construct a numpy string type.
2017-05-10 10:21:01 +02:00
Dean Moldovan 78f1dcf98f Fix std::nullptr_t caster (#840)
* Fix compilation error with std::nullptr_t

* Enable conversion from None to std::nullptr_t and std::nullopt_t

Fixes #839.
2017-05-09 23:30:05 +02:00
Jason Rhinelander 77710ff01c Make PYBIND11_CPP_STANDARD work under MSVC
Under MSVC we were ignoring PYBIND11_CPP_STANDARD and simply not
passing any standard (which makes MSVC default to its C++14 mode).

MSVC 2015u3 added the `/std:c++14` and `/std:c++latest` flags; the
latter, under MSVC 2017, enables some C++17 features (such as
`std::optional` and `std::variant`), so it is something we need to
start supporting under MSVC.

This makes the PYBIND11_CPP_STANDARD cmake variable work under MSVC,
defaulting it to /std:c++14 (matching the default -std=c++14 for
non-MSVC).

It also adds a new appveyor test running under MSVC 2017 with
/std:c++latest, which runs (and passes) the
`std::optional`/`std::variant` tests.

Also updated the documentation to clarify the c++ flags and add show
MSVC flag examples.
2017-05-09 16:41:47 -04:00
Jason Rhinelander ca0e82b79f Update PYBIND11_CPP1{4,7} macros for MSVC
The PYBIND11_CPP14 macro started out as a guard for the compile-time
path code in `descr.h`, but has since come to mean other things.  This
means that while the `descr.h` check has just checked the
`PYBIND11_CPP14` macro, various other places now check `PYBIND11_CPP14
|| _MSC_VER`.  This reverses that by now setting the CPP14 macro when
MSVC is trying to support C++14, but disabling the `descr.h` C++14 code
(which still fails under MSVC 2017).

The CPP17 macro also gets enabled when MSVC 2017 is compiling with
/std:c++latest (the default is /std:c++14), which enables
`std::optional` and `std::variant` support under MSVC.
2017-05-09 16:41:47 -04:00
Jason Rhinelander d15b217f1a Only disable -Wnoexcept-type on gcc >= 7 2017-05-09 14:46:15 -04:00
Jason Rhinelander 616e3d8fa3 attr.h: Fix header copy-and-paste typo 2017-05-09 13:50:34 -04:00
Jason Rhinelander 88ebc49be6 gcc 7 disable warning
GCC 7 generates (when compiling in C++11/14 mode) warnings such as:

    mangled name for ‘pybind11::class_<type_, options>&
    pybind11::class_<type_, options>::def(const char*, Func&&, const Extra&
    ...) [with Func = int (test_exc_sp::C::*)(int) noexcept; Extra = {};
    type_ = test_exc_sp::C; options = {}]’ will change in C++17 because the
    exception specification is part of a function type [-Wnoexcept-type]

There's nothing we can actually do in the code to avoid this, so just
disable the warning.
2017-05-09 13:41:51 -04:00
Dean Moldovan 076c738641 Add py::exec() as a shortcut for py::eval<py::eval_statements>() 2017-05-08 20:46:16 +02:00
Dean Moldovan 0c4e0372a3 Improve PYBIND11_DEPRECATED by showing the message on all compilers
GCC supports `deprecated(msg)` since v4.5 and VS supports the standard
[[deprecated(msg)]] since 2015 RTM.

The deprecated constructor change from `= default` to `{}` is
a workaround for a VS2015 bug.
2017-05-08 01:53:07 +02:00
Dean Moldovan 36f0a15a49 Deprecate handle::operator== in favor of object_api::is 2017-05-08 01:53:07 +02:00
Cris Luengo 30d43c4992 Now `shape`, `size`, `ndims` and `itemsize` are also signed integers. 2017-05-08 01:50:21 +02:00
Jason Rhinelander b68959e822 Use numpy rather than Eigen for copying
We're current copy by creating an Eigen::Map into the input numpy
array, then assigning that to the basic eigen type, effectively having
Eigen do the copy.  That doesn't work for negative strides, though:
Eigen doesn't allow them.

This commit makes numpy do the copying instead by allocating the eigen
type, then having numpy copy from the input array into a numpy reference
into the eigen object's data.  This also saves a copy when type
conversion is required: numpy can do the conversion on-the-fly as part
of the copy.

Finally this commit also makes non-reference parameters respect the
convert flag, declining the load when called in a noconvert pass with a
convertible, but non-array input or an array with the wrong dtype.
2017-05-08 01:50:21 +02:00
Cris Luengo 627da3f135 Making a copy when casting a numpy array with negative strides to Eigen.
`EigenConformable::stride_compatible` returns false if the strides are
negative. In this case, do not use `EigenConformable::stride`, as it
is {0,0}. We cannot write negative strides in this element, as Eigen
will throw an assertion if we do.

The `type_caster` specialization for regular, dense Eigen matrices now
does a second `array_t::ensure` to copy data in case of negative strides.
I'm not sure that this is the best way to implement this.

I have added "TODO" tags linking these changes to Eigen bug #747, which,
when fixed, will allow Eigen to accept negative strides.
2017-05-08 01:50:21 +02:00
Cris Luengo d400f60c96 Python buffer objects can have negative strides. 2017-05-08 01:50:21 +02:00
Jason Rhinelander 2b941b38b4 Add missing header to setup.py 2017-05-07 12:19:19 -04:00
Jason Rhinelander 271b27ff18 Remove obsolete comment 2017-05-02 15:21:39 -04:00
uentity 083a0219b5 array: implement array resize 2017-04-29 15:19:45 -04:00
Dean Moldovan 4ffa76ec56 Add type caster for std::variant and other variant-like classes 2017-04-29 17:31:30 +02:00
Jason Rhinelander a01b6b805c functional: support bound methods
If a bound std::function is invoked with a bound method, the implicit
bound self is lost because we use `detail::get_function` to unbox the
function.  This commit amends the code to use py::function and only
unboxes in the special is-really-a-c-function case.  This makes bound
methods stay bound rather than unbinding them by forcing extraction of
the c function.
2017-04-29 10:43:17 -04:00
Wenzel Jakob 7653a115bd pytest target: add USE_TERMINAL flag
The added flag enables non-buffered console output when using Ninja
2017-04-29 16:35:28 +02:00
Wenzel Jakob e6fd2cd5ab enum_: fix implicit conversion on Python 2.7
Enumerations on Python 2.7 were not always implicitly converted to
integers (depending on the target size). This patch adds a __long__
conversion function (only enabled on 2.7) which fixes this issue.

The attached test case fails without this patch.
2017-04-29 16:35:28 +02:00
Jason Rhinelander 51d18aa252 Fix ambiguous initialize_list arguments
This removes the convert-from-arithemtic-scalar constructor of
any_container as it can result in ambiguous calls, as in:

    py::array_t<float>({ 1, 2 })

which could be intepreted as either of:

    py::array_t<float>(py::array_t<float>(1, 2))
    py::array_t<float>(py::detail::any_container({ 1, 2 }))

Removing the convert-from-arithmetic constructor reduces the number of
implicit conversions, avoiding the ambiguity for array and array_t.
This also re-adds the array/array_t constructors taking a scalar
argument for backwards compatibility.
2017-04-28 14:12:06 -04:00
Jason Rhinelander 2761f78f11 travis-ci: Remove clang 4/c++17 from allow_failures
The job is using the released clang and stable-branch libc++, which
wasn't the case when it was added.  Leave the g++7/c++17 in
allow_failures for now as it's still a pre-release compiler (and pulled
from debian experimental).
2017-04-28 13:10:00 -04:00
Jason Rhinelander 0a90b2db71 Don't let PyInstanceMethod hide itself
Python 3's `PyInstanceMethod_Type` hides itself via its `tp_descr_get`,
which prevents aliasing methods via `cls.attr("m2") = cls.attr("m1")`:
instead the `tp_descr_get` returns a plain function, when called on a
class, or a `PyMethod`, when called on an instance.  Override that
behaviour for pybind11 types with a special bypass for
`PyInstanceMethod_Types`.
2017-04-28 11:18:58 -04:00
Jason Rhinelander a7f704b39b Fix Python 3 `bytes` conversion to std::string/char*
The Unicode support added in 2.1 (PR #624) inadvertently broke accepting
`bytes` as std::string/char* arguments.  This restores it with a
separate path that does a plain conversion (i.e. completely bypassing
all the encoding/decoding code), but only for single-byte string types.
2017-04-28 11:14:14 -04:00
Jason Rhinelander ce494d65de Add numpy version check (#819)
The numpy API constants can check past the end of the API array if the
numpy version is too old thus causing a segfault.  The current list of
functions requires numpy >= 1.7.0, so this adds a check and exception if
numpy is too old.

The added feature version API element was added in numpy 1.4.0, so this
could still segfault if loaded in 1.3.0 or earlier, but given that
1.4.0 was released at the end of 2009, it seems reasonable enough to
not worry about that case.  (1.7.0 was released in early 2013).
2017-04-28 14:57:13 +02:00
Jason Rhinelander 1f8a100d38 Track base class pointers of instances
This commits adds base class pointers of offset base classes (i.e. due
to multiple inheritance) to `registered_instances` so that if such a
pointer is returned we properly recognize it as an existing instance.

Without this, returning a base class pointer will cast to the existing
instance if the pointer happens to coincide with the instance pointer,
but constructs a new instance (quite possibly with a segfault, if
ownership is applied) for unequal base class pointers due to multiple
inheritance.
2017-04-27 09:12:41 -04:00
Jason Rhinelander 14e70650fe Fix downcasting of base class pointers
When we are returned a base class pointer (either directly or via
shared_from_this()) we detect its runtime type (using `typeid`), then
end up essentially reinterpret_casting the pointer to the derived type.
This is invalid when the base class pointer was a non-first base, and we
end up with an invalid pointer.  We could dynamic_cast to the
most-derived type, but if *that* type isn't pybind11-registered, the
resulting pointer given to the base `cast` implementation isn't necessarily valid
to be reinterpret_cast'ed back to the backup type.

This commit removes the "backup" type argument from the many-argument
`cast(...)` and instead does the derived-or-pointer type decision and
type lookup in type_caster_base, where the dynamic_cast has to be to
correctly get the derived pointer, but also has to do the type lookup to
ensure that we don't pass the wrong (derived) pointer when the backup
type (i.e. the type caster intrinsic type) pointer is needed.

Since the lookup is needed before calling the base cast(), this also
changes the input type to a detail::type_info rather than doing a
(second) lookup in cast().
2017-04-27 09:12:41 -04:00
Jason Rhinelander 929009954b Expose more instance management functions
This breaks up the instance management functions in class_support.h a
little bit so that other pybind11 code can use it.  In particular:

- added make_new_instance() which does what pybind11_object_new does,
  but also allows instance allocation without `value` allocation.  This
  lets `cast.h` use the same instance allocation rather than having its
  own separate implementation.
- instance registration is now moved to a
  `register_instance()`/deregister_instance()` pair (rather than having
  individual code add or remove things from `registered_instances`
  directory).
- clear_instance() does everything `pybind11_object_dealloc()` needs
  except for the deallocation; this is helpful for factory construction
  which needs to be able to replace the internals of an instance without
  deallocating it.
- clear_instance() now also calls `dealloc` when `holder_constructed`
  is true, even if `value` is false.  This can happen in factory
  construction when the pointer is moved from one instance to another,
  but the holder itself is only copied (i.e. for a shared_ptr holder).
2017-04-27 09:12:41 -04:00
Dean Moldovan deb85f46ef Switch to regular breathe package on Travis
Breathe was previously installed from the git master because of a bug
in the released version. The fix has since migrated to PyPI.

[skip appveyor]
2017-04-22 23:30:33 +02:00
Jason Rhinelander fb50ce1fef Add static_assert-raising error for overload_cast in c++11
I got some unexpected errors from code using `overload_cast` until I
realized that I'd configured the build with -std=c++11.

This commit adds a fake `overload_cast` class in C++11 mode that
triggers a static_assert failure indicating that C++14 is needed.
2017-04-18 22:06:25 -04:00
Jason Rhinelander d355f2fcca Don't allow mixed static/non-static overloads
We currently fail at runtime when trying to call a method that is
overloaded with both static and non-static methods.  This is something
python won't allow: the object is either a function or an instance, and
can't be both.
2017-04-18 17:17:47 -04:00
Jason Rhinelander 90bac96321 Keep skipping buffer tests on pypy
Adding numpy to the pypy test exposed a segfault caused by the buffer
tests in test_stl_binders.py: the first such test was explicitly skipped
on pypy, but the second (test_vector_buffer_numpy) which also seems to
cause an occasional segfault was just marked as requiring numpy.

Explicitly skip it on pypy as well (until a workaround, fix, or pypy fix
are found).
2017-04-18 14:21:31 -04:00
Jason Rhinelander d170731f6f Remove unneeded exports
Various bash variables that are only used in the travis-ci script and
don't need to propagate (e.g. to cmake) are being pointlessly exported;
this removes these `export`s.
2017-04-18 14:21:31 -04:00
Jason Rhinelander f0c7c00815 Switch clang-4.0 build to trusty; cache libc++
This uses the trusty container rather than docker for the clang 4.0
build.  It also caches the local libc++ installation so that it doesn't
need to be compiled every time, which should speed up the job
considerably.
2017-04-18 14:21:31 -04:00
Jason Rhinelander 3d5005168b travis-ci: switch to trusty; cache pip packages
This applies several changes to the non-docker travis-ci builds:

- Make all builds use trusty rather than precise.  pybind can't really
  build in precise anyway (we install essentially the entire toolchain
  backported from trusty on every build), and so this saves needing to
  install all the backported packages during the build setup.
- Updated the 3.5 build to 3.6 (via deadsnakes, which didn't backport
  3.6 to ubuntu releases earlier than trusty).
- As a result of the switch to trusty, the BAREBONES build now picks up
  the (default installed) python 3.5 installation.
- Invoke pip everywhere via $PYTHON -m pip rather than the pip
  executable, which saves us having to figure out what the pip
  executable is, and ensures that we are using the correct pip.
- Install packages with `pip --user` rather than in a virtualenv.
- Add the local user python package archive to the travis-ci cache
  (rather than the pip cache).  This saves needing to install packages
  during installation (unless there are updates, in which case the
  package and the cache are updated).
- Install numpy and scipy on the pypy build.  This has to build from
  source (and so blas and fortran need to be installed on the build),
  but given the above caching, the build will only be slow for the first
  build after a new numpy/scipy release.  This testing is valuable:
  numpy has various behaviour differences under pypy.
- Added set -e/+e around the before_install/install blocks so that a
  failure here (e.g. a pip install failure or dependency download
  failure) triggers a build failure.
- Update eigen version to latest (3.3.3), mainly to be consistent with
  the appveyor build.
- The travis trusty environment has an upgraded cmake, so this
  downgrades cmake (to the stock trusty version) on the first couple
  jobs so that we're still including some cmake 2.8.12 testing.
2017-04-18 14:21:31 -04:00
Jason Rhinelander b4cbd7a1fe Appveyor: Disable fast_finish for now
While builds are randomly failing, fast_finish is counterproductive.

Will reenable this once appveyor stabilizes.
2017-04-17 11:57:46 -04:00
Jason Rhinelander 2d14c1c5db Fixed bad_arg_def imports
Don't try to define these in the issues submodule, because that fails
if testing without issues compiled in (e.g. using
cmake -DPYBIND11_TEST_OVERRIDE=test_methods_and_attributes.cpp).
2017-04-15 11:12:41 -04:00