1. 24 May, 2017 2 commits
    • Jason Rhinelander's avatar
      Allow py::arg().none(false) argument attribute · 4e1e4a58
      Jason Rhinelander authored
      This attribute lets you disable (or explicitly enable) passing None to
      an argument that otherwise would allow it by accepting
      a value by raw pointer or shared_ptr.
      4e1e4a58
    • Jason Rhinelander's avatar
      Add movable cast support to type casters · 813d7e86
      Jason Rhinelander authored
      This commit allows type_casters to allow their local values to be moved
      away, rather than copied, when the type caster instance itself is an rvalue.
      
      This only applies (automatically) to type casters using
      PYBIND11_TYPE_CASTER; the generic type type casters don't own their own
      pointer, and various value casters (e.g. std::string, std::pair,
      arithmetic types) already cast to an rvalue (i.e. they return by value).
      
      This updates various calling code to attempt to get a movable value
      whenever the value is itself coming from a type caster about to be
      destroyed: for example, when constructing an std::pair or various stl.h
      containers.  For types that don't support value moving, the cast_op
      falls back to an lvalue cast.
      
      There wasn't an obvious place to add the tests, so I added them to
      test_copy_move_policies, but also renamed it to drop the _policies as it
      now tests more than just policies.
      813d7e86
  2. 22 May, 2017 2 commits
    • Bruce Merry's avatar
      Support pointers to member functions in def_buffer. · fe0cf8b7
      Bruce Merry authored
      Closes #857, by adding overloads to def_buffer that match pointers to
      member functions and wrap them in lambdas.
      fe0cf8b7
    • Jason Rhinelander's avatar
      Use dynamic cast for shared_from_this holder init · b8ac4383
      Jason Rhinelander authored
      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.
      b8ac4383
  3. 21 May, 2017 1 commit
    • Jason Rhinelander's avatar
      Fix /= operator under Python 3 · acad05cb
      Jason Rhinelander authored
      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.
      acad05cb
  4. 11 May, 2017 1 commit
  5. 10 May, 2017 4 commits
    • Dean Moldovan's avatar
      Improve constructor resolution in variant_caster · 94d0a9f7
      Dean Moldovan authored
      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.
      94d0a9f7
    • Jason Rhinelander's avatar
      Defer None loading to second pass · 93e3eac6
      Jason Rhinelander authored
      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).
      93e3eac6
    • Bruce Merry's avatar
      Allow std::complex field with PYBIND11_NUMPY_DTYPE (#831) · b82c0f0a
      Bruce Merry authored
      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.
      b82c0f0a
    • Bruce Merry's avatar
      Support arrays inside PYBIND11_NUMPY_DTYPE (#832) · 8e0d832c
      Bruce Merry authored
      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.
      8e0d832c
  6. 09 May, 2017 1 commit
  7. 08 May, 2017 1 commit
  8. 07 May, 2017 5 commits
    • Dean Moldovan's avatar
      36f0a15a
    • Cris Luengo's avatar
    • Jason Rhinelander's avatar
      Use numpy rather than Eigen for copying · b68959e8
      Jason Rhinelander authored
      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.
      b68959e8
    • Cris Luengo's avatar
      Making a copy when casting a numpy array with negative strides to Eigen. · 627da3f1
      Cris Luengo authored
      `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.
      627da3f1
    • Cris Luengo's avatar
      d400f60c
  9. 02 May, 2017 1 commit
  10. 29 Apr, 2017 5 commits
  11. 28 Apr, 2017 3 commits
    • Jason Rhinelander's avatar
      Fix ambiguous initialize_list arguments · 51d18aa2
      Jason Rhinelander authored
      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.
      51d18aa2
    • Jason Rhinelander's avatar
      Don't let PyInstanceMethod hide itself · 0a90b2db
      Jason Rhinelander authored
      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`.
      0a90b2db
    • Jason Rhinelander's avatar
      Fix Python 3 `bytes` conversion to std::string/char* · a7f704b3
      Jason Rhinelander authored
      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.
      a7f704b3
  12. 27 Apr, 2017 2 commits
    • Jason Rhinelander's avatar
      Track base class pointers of instances · 1f8a100d
      Jason Rhinelander authored
      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.
      1f8a100d
    • Jason Rhinelander's avatar
      Fix downcasting of base class pointers · 14e70650
      Jason Rhinelander authored
      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().
      14e70650
  13. 18 Apr, 2017 2 commits
    • Jason Rhinelander's avatar
      Don't allow mixed static/non-static overloads · d355f2fc
      Jason Rhinelander authored
      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.
      d355f2fc
    • Jason Rhinelander's avatar
      Keep skipping buffer tests on pypy · 90bac963
      Jason Rhinelander authored
      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).
      90bac963
  14. 15 Apr, 2017 1 commit
    • Jason Rhinelander's avatar
      Fixed bad_arg_def imports · 2d14c1c5
      Jason Rhinelander authored
      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).
      2d14c1c5
  15. 13 Apr, 2017 2 commits
    • Jason Rhinelander's avatar
      Accept abitrary containers and iterators for shape/strides · 5f383862
      Jason Rhinelander authored
      This adds support for constructing `buffer_info` and `array`s using
      arbitrary containers or iterator pairs instead of requiring a vector.
      
      This is primarily needed by PR #782 (which makes strides signed to
      properly support negative strides, and will likely also make shape and
      itemsize to avoid mixed integer issues), but also needs to preserve
      backwards compatibility with 2.1 and earlier which accepts the strides
      parameter as a vector of size_t's.
      
      Rather than adding nearly duplicate constructors for each stride-taking
      constructor, it seems nicer to simply allow any type of container (or
      iterator pairs).  This works by replacing the existing vector arguments
      with a new `detail::any_container` class that handles implicit
      conversion of arbitrary containers into a vector of the desired type.
      It can also be explicitly instantiated with a pair of iterators (e.g.
      by passing {begin, end} instead of the container).
      5f383862
    • Jason Rhinelander's avatar
      array: set exception message on failure · 5749b502
      Jason Rhinelander authored
      When attempting to get a raw array pointer we return nullptr if given a
      nullptr, which triggers an error_already_set(), but we haven't set an
      exception message, which results in "Unknown internal error".
      
      Callers that want explicit allowing of a nullptr here already handle it
      (by clearing the exception after the call).
      5749b502
  16. 09 Apr, 2017 1 commit
    • Jason Rhinelander's avatar
      Fix Eigen argument doc strings · e9e17746
      Jason Rhinelander authored
      Many of the Eigen type casters' name() methods weren't wrapping the type
      description in a `type_descr` object, which thus wasn't adding the
      "{...}" annotation used to identify an argument which broke the help
      output by skipping eigen arguments.
      
      The test code I had added even had some (unnoticed) broken output (with
      the "arg0: " showing up in the return value).
      
      This commit also adds test code to ensure that named eigen arguments
      actually work properly, despite the invalid help output.  (The added
      tests pass without the rest of this commit).
      e9e17746
  17. 07 Apr, 2017 1 commit
  18. 06 Apr, 2017 1 commit
    • Dean Moldovan's avatar
      Fix test_cmake_build failure with bare python exe name (fix #783) · 555dc4f0
      Dean Moldovan authored
      Besides appearing in the CMake GUI, the `:FILENAME` specifier changes
      behavior as well:
      
      cmake -DPYTHON_EXECUTABLE=python ..  # FAIL, can't find python
      cmake -DPYTHON_EXECUTABLE=/path/to/python ..  # OK
      cmake -DPYTHON_EXECUTABLE:FILENAME=python ..  # OK
      cmake -DPYTHON_EXECUTABLE:FILENAME=/path/to/python ..  # OK
      555dc4f0
  19. 05 Apr, 2017 1 commit
    • Jason Rhinelander's avatar
      Improve make_tuple error message under debugging · 6906b270
      Jason Rhinelander authored
      When make_tuple fails (for example, when print() is called with a
      non-convertible argument, as in #778) the error message a less helpful
      than it could be:
      
          make_tuple(): unable to convert arguments of types 'std::tuple<type1, type2>' to Python object
      
      There is no actual std::tuple involved (only a parameter pack and a
      Python tuple), but it also doesn't immediately reveal which type caused
      the problem.
      
      This commit changes the debugging mode output to show just the
      problematic type:
      
          make_tuple(): unable to convert argument of type 'type2' to Python object
      6906b270
  20. 02 Apr, 2017 2 commits
    • Dean Moldovan's avatar
      Add a scope guard call policy · 1ac19036
      Dean Moldovan authored
      ```c++
      m.def("foo", foo, py::call_guard<T>());
      ```
      
      is equivalent to:
      
      ```c++
      m.def("foo", [](args...) {
          T scope_guard;
          return foo(args...); // forwarded arguments
      });
      ```
      1ac19036
    • Roman Miroshnychenko's avatar
      Add a method to check Python exception types (#772) · 83a8a977
      Roman Miroshnychenko authored
      This commit adds `error_already_set::matches()` convenience method to
      check if the exception trapped by `error_already_set` matches a given
      Python exception type. This will address #700 by providing a less
      verbose way to check exceptions.
      83a8a977
  21. 28 Mar, 2017 1 commit