1. 24 Feb, 2017 2 commits
    • Jason Rhinelander's avatar
      array: fix base handling · f86dddf7
      Jason Rhinelander authored
      numpy arrays aren't currently properly setting base: by setting `->base`
      directly, the base doesn't follow what numpy expects and documents (that
      is, following chained array bases to the root array).
      
      This fixes the behaviour by using numpy's PyArray_SetBaseObject to set
      the base instead, and then updates the tests to reflect the fixed
      behaviour.
      f86dddf7
    • Jason Rhinelander's avatar
      Change numpy constants to non-deprecated versions · 88fff9d1
      Jason Rhinelander authored
      A few of pybind's numpy constants are using the numpy-deprecated names
      (without "ARRAY_" in them); updated our names to be consistent with
      current numpy code.
      88fff9d1
  2. 17 Feb, 2017 1 commit
    • Jason Rhinelander's avatar
      Revert noexcept deduction in favour of better SFINAE on lambda functions (#677) · 1d7998e3
      Jason Rhinelander authored
      noexcept deduction, added in PR #555, doesn't work with clang's
      -std=c++1z; and while it works with g++, it isn't entirely clear to me
      that it is required to work in C++17.
      
      What should work, however, is that C++17 allows implicit conversion of a
      `noexcept(true)` function pointer to a `noexcept(false)` (i.e.  default,
      noexcept-not-specified) function pointer.  That was breaking in pybind11
      because the cpp_function template used for lambdas provided a better
      match (i.e. without requiring an implicit conversion), but it then
      failed.
      
      This commit takes a different approach of using SFINAE on the lambda
      function to prevent it from matching a non-lambda object, which then
      gets implicit conversion from a `noexcept` function pointer to a
      `noexcept(false)` function pointer.  This much nicer solution also gets
      rid of the C++17 NOEXCEPT macros, and works in both clang and g++.
      1d7998e3
  3. 14 Feb, 2017 2 commits
  4. 08 Feb, 2017 1 commit
    • Matthew Woehlke's avatar
      Avoid C-style const casts (#659) · e15fa9f9
      Matthew Woehlke authored
      * Avoid C-style const casts
      
      Replace C-style casts that discard `const` with `const_cast` (and, where
      necessary, `reinterpret_cast` as well).
      
      * Warn about C-style const-discarding casts
      
      Change pybind11_enable_warnings to also enable `-Wcast-qual` (warn if a
      C-style cast discards `const`) by default. The previous commit should
      have gotten rid of all of these (at least, all the ones that tripped in
      my build, which included the tests), and this should discourage more
      from newly appearing.
      e15fa9f9
  5. 06 Feb, 2017 1 commit
    • Wenzel Jakob's avatar
      renamed _check -> check_ · 0defac59
      Wenzel Jakob authored
      (Identifiers starting with underscores are reserved by the standard)
      Also fixed a typo in a comment.
      0defac59
  6. 31 Jan, 2017 1 commit
    • Jason Rhinelander's avatar
      Numpy: better compilation errors, long double support (#619) · f7f5bc8e
      Jason Rhinelander authored
      * Clarify PYBIND11_NUMPY_DTYPE documentation
      
      The current documentation and example reads as though
      PYBIND11_NUMPY_DTYPE is a declarative macro along the same lines as
      PYBIND11_DECLARE_HOLDER_TYPE, but it isn't.  The changes the
      documentation and docs example to make it clear that you need to "call"
      the macro.
      
      * Add satisfies_{all,any,none}_of<T, Preds>
      
      `satisfies_all_of<T, Pred1, Pred2, Pred3>` is a nice legibility-enhanced
      shortcut for `is_all<Pred1<T>, Pred2<T>, Pred3<T>>`.
      
      * Give better error message for non-POD dtype attempts
      
      If you try to use a non-POD data type, you get difficult-to-interpret
      compilation errors (about ::name() not being a member of an internal
      pybind11 struct, among others), for which isn't at all obvious what the
      problem is.
      
      This adds a static_assert for such cases.
      
      It also changes the base case from an empty struct to the is_pod_struct
      case by no longer using `enable_if<is_pod_struct>` but instead using a
      static_assert: thus specializations avoid the base class, POD types
      work, and non-POD types (and unimplemented POD types like std::array)
      get a more informative static_assert failure.
      
      * Prefix macros with PYBIND11_
      
      numpy.h uses unprefixed macros, which seems undesirable.  This prefixes
      them with PYBIND11_ to match all the other macros in numpy.h (and
      elsewhere).
      
      * Add long double support
      
      This adds long double and std::complex<long double> support for numpy
      arrays.
      
      This allows some simplification of the code used to generate format
      descriptors; the new code uses fewer macros, instead putting the code as
      different templated options; the template conditions end up simpler with
      this because we are now supporting all basic C++ arithmetic types (and
      so can use is_arithmetic instead of is_integral + multiple
      different specializations).
      
      In addition to testing that it is indeed working in the test script, it
      also adds various offset and size calculations there, which
      fixes the test failures under x86 compilations.
      f7f5bc8e
  7. 03 Jan, 2017 1 commit
  8. 14 Dec, 2016 1 commit
    • Jason Rhinelander's avatar
      Support binding noexcept function/methods in C++17 · 6e036e78
      Jason Rhinelander authored
      When compiling in C++17 mode the noexcept specifier is part of the
      function type.  This causes a failure in pybind11 because, by omitting
      a noexcept specifier when deducing function return and argument types,
      we are implicitly making `noexcept(false)` part of the type.
      
      This means that functions with `noexcept` fail to match the function
      templates in cpp_function (and other places), and we get compilation
      failure (we end up trying to fit it into the lambda function version,
      which fails since a function pointer has no `operator()`).
      
      We can, however, deduce the true/false `B` in noexcept(B), so we don't
      need to add a whole other set of overloads, but need to deduce the extra
      argument when under C++17.  That will *not* work under pre-C++17,
      however.
      
      This commit adds two macros to fix the problem: under C++17 (with the
      appropriate feature macro set) they provide an extra `bool NoExceptions`
      template argument and provide the `noexcept(NoExceptions)` deduced
      specifier.  Under pre-C++17 they expand to nothing.
      
      This is needed to compile pybind11 with gcc7 under -std=c++17.
      6e036e78
  9. 03 Dec, 2016 1 commit
    • Dean Moldovan's avatar
      Use C++14 index_sequence when possible · 8c85a857
      Dean Moldovan authored
      Newer standard libraries use compiler intrinsics for std::index_sequence
      which makes it ‘free’. This prevents hitting instantiation limits for
      recursive templates (-ftemplate-depth).
      8c85a857
  10. 22 Nov, 2016 3 commits
  11. 17 Nov, 2016 4 commits
    • Dean Moldovan's avatar
      Improve consistency of array and array_t with regard to other pytypes · 4de27102
      Dean Moldovan authored
      * `array_t(const object &)` now throws on error
      * `array_t::ensure()` is intended for casters —- old constructor is
        deprecated
      * `array` and `array_t` get default constructors (empty array)
      * `array` gets a converting constructor
      * `py::isinstance<array_T<T>>()` checks the type (but not flags)
      
      There is only one special thing which must remain: `array_t` gets
      its own `type_caster` specialization which uses `ensure` instead
      of a simple check.
      4de27102
    • Dean Moldovan's avatar
      Add py::reinterpret_borrow<T>()/steal<T>() for low-level unchecked casts · c7ac16bb
      Dean Moldovan authored
      The pytype converting constructors are convenient and safe for user
      code, but for library internals the additional type checks and possible
      conversions are sometimes not desired. `reinterpret_borrow<T>()` and
      `reinterpret_steal<T>()` serve as the low-level unsafe counterparts
      of `cast<T>()`.
      
      This deprecates the `object(handle, bool)` constructor.
      
      Renamed `borrowed` parameter to `is_borrowed` to avoid shadowing
      warnings on MSVC.
      c7ac16bb
    • Dean Moldovan's avatar
      Add default and converting constructors for all concrete Python types · e18bc02f
      Dean Moldovan authored
      * Deprecate the `py::object::str()` member function since `py::str(obj)`
        is now equivalent and preferred
      
      * Make `py::repr()` a free function
      
      * Make sure obj.cast<T>() works as expected when T is a Python type
      
      `obj.cast<T>()` should be the same as `T(obj)`, i.e. it should convert
      the given object to a different Python type. However, `obj.cast<T>()`
      usually calls `type_caster::load()` which only checks the type without
      doing any actual conversion. That causes a very unexpected `cast_error`.
      This commit makes it so that `obj.cast<T>()` and `T(obj)` are the same
      when T is a Python type.
      
      * Simplify pytypes converting constructor implementation
      
      It's not necessary to maintain a full set of converting constructors
      and assignment operators + const& and &&. A single converting const&
      constructor will work and there is no impact on binary size. On the
      other hand, the conversion functions can be significantly simplified.
      e18bc02f
    • Dean Moldovan's avatar
      Add py::isinstance<T>(obj) for generalized Python type checking · b4498ef4
      Dean Moldovan authored
      Allows checking the Python types before creating an object instead of
      after. For example:
      ```c++
      auto l = list(ptr, true);
      if (l.check())
         // ...
      ```
      The above is replaced with:
      ```c++
      if (isinstance<list>(ptr)) {
          auto l = reinterpret_borrow(ptr);
          // ...
      }
      ```
      
      This deprecates `py::object::check()`. `py::isinstance()` covers the
      same use case, but it can also check for user-defined types:
      ```c++
      class Pet { ... };
      py::class_<Pet>(...);
      
      m.def("is_pet", [](py::object obj) {
          return py::isinstance<Pet>(obj); // works as expected
      });
      ```
      b4498ef4
  12. 16 Nov, 2016 1 commit
  13. 08 Nov, 2016 1 commit
  14. 03 Nov, 2016 3 commits
  15. 01 Nov, 2016 1 commit
  16. 23 Oct, 2016 1 commit
  17. 22 Oct, 2016 4 commits
  18. 20 Oct, 2016 4 commits
  19. 16 Oct, 2016 1 commit
    • Jason Rhinelander's avatar
      Disable most implicit conversion constructors · 12d76600
      Jason Rhinelander authored
      We have various classes that have non-explicit constructors that accept
      a single argument, which is implicitly making them implicitly
      convertible from the argument.  In a few cases, this is desirable (e.g.
      implicit conversion of std::string to py::str, or conversion of double
      to py::float_); in many others, however, it is unintended (e.g. implicit
      conversion of size_t to some pre-declared py::array_t<T> type).
      
      This disables most of the unwanted implicit conversions by marking them
      `explicit`, and comments the ones that are deliberately left implicit.
      12d76600
  20. 13 Oct, 2016 2 commits
  21. 12 Oct, 2016 1 commit
    • Wenzel Jakob's avatar
      Permit creation of NumPy arrays with a "base" object that owns the data · 369e9b39
      Wenzel Jakob authored
      This patch adds an extra base handle parameter to most ``py::array`` and
      ``py::array_t<>`` constructors. If specified along with a pointer to
      data, the base object will be registered within NumPy, which increases
      the base's reference count. This feature is useful to create shallow
      copies of C++ or Python arrays while ensuring that the owners of the
      underlying can't be garbage collected while referenced by NumPy.
      
      The commit also adds a simple test function involving a ``wrap()``
      function that creates shallow copies of various N-D arrays.
      369e9b39
  22. 07 Oct, 2016 1 commit
  23. 23 Sep, 2016 1 commit
  24. 22 Sep, 2016 1 commit