1. 08 Jun, 2017 1 commit
    • Jason Rhinelander's avatar
      Destroy internals if created during Py_Finalize() · 4edb1ce2
      Jason Rhinelander authored
      Py_Finalize could potentially invoke code that calls `get_internals()`,
      which could create a new internals object if one didn't exist.
      `finalize_interpreter()` didn't catch this because it only used the
      pre-finalize interpreter pointer status; if this happens, it results in
      the internals pointer not being properly destroyed with the interpreter,
      which leaks, and also causes a `get_internals()` under a future
      interpreter to return an internals object that is wrong in various ways.
      4edb1ce2
  2. 07 Jun, 2017 1 commit
  3. 30 May, 2017 3 commits
    • Jason Rhinelander's avatar
      std::reference_wrapper: non-generic types; no None · acedd6c7
      Jason Rhinelander authored
      This reimplements the std::reference_wrapper<T> caster to be a shell
      around the underlying T caster (rather than assuming T is a generic
      type), which lets it work for things like `std::reference_wrapper<int>`
      or anything else custom type caster with a lvalue cast operator.
      
      This also makes it properly fail when None is provided, just as an
      ordinary lvalue reference argument would similarly fail.
      
      This also adds a static assert to test that T has an appropriate type
      caster.  It triggers for casters like `std::pair`, which have
      return-by-value cast operators.  (In theory this could be supported by
      storing a local temporary for such types, but that's beyond the scope
      of this PR).
      
      This also replaces `automatic` or `take_ownership` return value policies
      with `automatic_reference` as taking ownership of a reference inside a
      reference_wrapper is not valid.
      acedd6c7
    • Jason Rhinelander's avatar
      Move reference_wrapper test from test_issues to test_python_types · 7cdf9f1a
      Jason Rhinelander authored
      test_issues is deprecated, and the following commit adds other, related
      reference_wrapper tests.
      7cdf9f1a
    • Dean Moldovan's avatar
      Filter warnings on pytest >= v3.1 · 005fde6a
      Dean Moldovan authored
      The new version of pytest now reports Python warnings by default. This
      commit filters out some third-party extension warnings which are not
      useful for pybind11 tests.
      005fde6a
  4. 29 May, 2017 3 commits
    • Dean Moldovan's avatar
      Replace PYBIND11_PLUGIN with PYBIND11_MODULE · 443ab594
      Dean Moldovan authored
      This commit also adds `doc()` to `object_api` as a shortcut for the
      `attr("__doc__")` accessor.
      
      The module macro changes from:
      ```c++
      PYBIND11_PLUGIN(example) {
          pybind11::module m("example", "pybind11 example plugin");
          m.def("add", [](int a, int b) { return a + b; });
          return m.ptr();
      }
      ```
      
      to:
      
      ```c++
      PYBIND11_MODULE(example, m) {
          m.doc() = "pybind11 example plugin";
          m.def("add", [](int a, int b) { return a + b; });
      }
      ```
      
      Using the old macro results in a deprecation warning. The warning
      actually points to the `pybind11_init` function (since attributes
      don't bind to macros), but the message should be quite clear:
      "PYBIND11_PLUGIN is deprecated, use PYBIND11_MODULE".
      443ab594
    • Yannick Jadoul's avatar
      Convenience constructor templates for buffer_info (#860) · b700c5d6
      Yannick Jadoul authored
      * Added template constructors to buffer_info that can deduce the item size, format string, and number of dimensions from the pointer type and the shape container
      
      * Implemented actual buffer_info constructor as private delegate constructor taking rvalue reference as a workaround for the evaluation order move problem on GCC 4.8
      b700c5d6
    • Dean Moldovan's avatar
      Fix buffer protocol inheritance · 427e4afc
      Dean Moldovan authored
      Fixes #878.
      427e4afc
  5. 28 May, 2017 4 commits
  6. 27 May, 2017 1 commit
    • Bruce Merry's avatar
      Avoid explicitly resetting a std::[experimental::]optional · 46dbee7d
      Bruce Merry authored
      Now that #851 has removed all multiple uses of a caster, it can just use
      the default-constructed value with needing a reset. This fixes two
      issues:
      
      1. With std::experimental::optional (at least under GCC 5.4), the `= {}`
      would construct an instance of the optional type and then move-assign
      it, which fails if the value type isn't move-assignable.
      
      2. With older versions of Boost, the `= {}` could fail because it is
      ambiguous, allowing construction of either `boost::none` or the value
      type.
      46dbee7d
  7. 25 May, 2017 3 commits
    • Bruce Merry's avatar
      Fix invalid memory access in vector insert method · eee4f4fc
      Bruce Merry authored
      The stl_bind.h wrapper for `Vector.insert` neglected to do a bounds
      check.
      eee4f4fc
    • Jason Rhinelander's avatar
      Force MSVC to compile in utf-8 mode · 8dc63ba9
      Jason Rhinelander authored
      MSVC by default uses the local codepage, which fails when it sees the
      utf-8 in test_python_types.cpp.  This adds the /utf-8 flag to the test
      suite compilation to force it to interpret source code as utf-8.
      
      Fixes #869
      8dc63ba9
    • Jason Rhinelander's avatar
      vectorize: pass-through of non-vectorizable args · f3ce00ea
      Jason Rhinelander authored
      This extends py::vectorize to automatically pass through
      non-vectorizable arguments.  This removes the need for the documented
      "explicitly exclude an argument" workaround.
      
      Vectorization now applies to arithmetic, std::complex, and POD types,
      passed as plain value or by const lvalue reference (previously only
      pass-by-value types were supported).  Non-const lvalue references and
      any other types are passed through as-is.
      
      Functions with rvalue reference arguments (whether vectorizable or not)
      are explicitly prohibited: an rvalue reference is inherently not
      something that can be passed multiple times and is thus unsuitable to
      being in a vectorized function.
      
      The vectorize returned value is also now more sensitive to inputs:
      previously it would return by value when all inputs are of size 1; this
      is now amended to having all inputs of size 1 *and* 0 dimensions.  Thus
      if you pass in, for example, [[1]], you get back a 1x1, 2D array, while
      previously you got back just the resulting single value.
      
      Vectorization of member function specializations is now also supported
      via `py::vectorize(&Class::method)`; this required passthrough support
      for the initial object pointer on the wrapping function pointer.
      f3ce00ea
  8. 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
  9. 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
  10. 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
  11. 11 May, 2017 1 commit
  12. 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
  13. 09 May, 2017 1 commit
  14. 08 May, 2017 1 commit
  15. 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
  16. 02 May, 2017 1 commit
  17. 29 Apr, 2017 5 commits
  18. 28 Apr, 2017 1 commit
    • 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