1. 29 Apr, 2017 1 commit
  2. 28 Apr, 2017 2 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
      Add numpy version check (#819) · ce494d65
      Jason Rhinelander authored
      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).
      ce494d65
  3. 13 Apr, 2017 3 commits
    • Jason Rhinelander's avatar
      Make any_container implicitly constructible from arithmetic values · 201796d9
      Jason Rhinelander authored
      This further reduces the constructors required in buffer_info/numpy by
      removing the need for the constructors that take a single size_t and
      just forward it on via an initializer_list to the container-accepting
      constructor.
      
      Unfortunately, in `array` one of the constructors runs into an ambiguity
      problem with the deprecated `array(handle, bool)` constructor (because
      both the bool constructor and the any_container constructor involve an
      implicit conversion, so neither has precedence), so a forwarding
      constructor is kept there (until the deprecated constructor is
      eventually removed).
      201796d9
    • 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
  4. 28 Mar, 2017 1 commit
    • Jason Rhinelander's avatar
      Deprecated borrowed/stolen for borrowed_t{}/stolen_t{} (#771) · 6db60cd9
      Jason Rhinelander authored
      The constexpr static instances can cause linking failures if the
      compiler doesn't optimize away the reference, as reported in #770.
      
      There's no particularly nice way of fixing this in C++11/14: we can't
      inline definitions to match the declaration aren't permitted for
      non-templated static variables (C++17 *does* allows "inline" on
      variables, but that obviously doesn't help us.)
      
      One solution that could work around it is to add an extra inherited
      subclass to `object`'s hierarchy, but that's a bit of a messy solution
      and was decided against in #771 in favour of just deprecating (and
      eventually dropping) the constexpr statics.
      
      Fixes #770.
      6db60cd9
  5. 26 Mar, 2017 1 commit
  6. 22 Mar, 2017 2 commits
    • Jason Rhinelander's avatar
      array-unchecked: add runtime dimension support and array-compatible methods · 773339f1
      Jason Rhinelander authored
      The extends the previous unchecked support with the ability to
      determine the dimensions at runtime.  This incurs a small performance
      hit when used (versus the compile-time fixed alternative), but is still considerably
      faster than the full checks on every call that happen with
      `.at()`/`.mutable_at()`.
      773339f1
    • Jason Rhinelander's avatar
      array: add unchecked access via proxy object · 423a49b8
      Jason Rhinelander authored
      This adds bounds-unchecked access to arrays through a `a.unchecked<Type,
      Dimensions>()` method.  (For `array_t<T>`, the `Type` template parameter
      is omitted).  The mutable version (which requires the array have the
      `writeable` flag) is available as `a.mutable_unchecked<...>()`.
      
      Specifying the Dimensions as a template parameter allows storage of an
      std::array; having the strides and sizes stored that way (as opposed to
      storing a copy of the array's strides/shape pointers) allows the
      compiler to make significant optimizations of the shape() method that it
      can't make with a pointer; testing with nested loops of the form:
      
          for (size_t i0 = 0; i0 < r.shape(0); i0++)
              for (size_t i1 = 0; i1 < r.shape(1); i1++)
                  ...
                      r(i0, i1, ...) += 1;
      
      over a 10 million element array gives around a 25% speedup (versus using
      a pointer) for the 1D case, 33% for 2D, and runs more than twice as fast
      with a 5D array.
      423a49b8
  7. 21 Mar, 2017 2 commits
    • Jason Rhinelander's avatar
      vectorize: trivial handling for F-order arrays · b0292c1d
      Jason Rhinelander authored
      This extends the trivial handling to support trivial handling for
      Fortran-order arrays (i.e. column major): if inputs aren't all
      C-contiguous, but *are* all F-contiguous, the resulting array will be
      F-contiguous and we can do trivial processing.
      
      For anything else (e.g. C-contiguous, or inputs requiring non-trivial
      processing), the result is in (numpy-default) C-contiguous layout.
      b0292c1d
    • Jason Rhinelander's avatar
      Stop forcing c-contiguous in py::vectorize · ae5a8f7e
      Jason Rhinelander authored
      The only part of the vectorize code that actually needs c-contiguous is
      the "trivial" broadcast; for non-trivial arguments, the code already
      uses strides properly (and so handles C-style, F-style, neither, slices,
      etc.)
      
      This commit rewrites `broadcast` to additionally check for C-contiguous
      storage, then takes off the `c_style` flag for the arguments, which
      will keep the functionality more or less the same, except for no longer
      requiring an array copy for non-c-contiguous input arrays.
      
      Additionally, if we're given a singleton slice (e.g. a[0::4, 0::4] for a
      4x4 or smaller array), we no longer fail triviality because the trivial
      code path never actually uses the strides on a singleton.
      ae5a8f7e
  8. 14 Mar, 2017 1 commit
  9. 13 Mar, 2017 1 commit
  10. 06 Mar, 2017 1 commit
  11. 28 Feb, 2017 1 commit
  12. 24 Feb, 2017 3 commits
    • Jason Rhinelander's avatar
      Change array's writeable exception to a ValueError · fd751703
      Jason Rhinelander authored
      Numpy raises ValueError when attempting to modify an array, while
      py::array is raising a RuntimeError.  This changes the exception to a
      std::domain_error, which gets mapped to the expected ValueError in
      python.
      fd751703
    • 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
  13. 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
  14. 14 Feb, 2017 2 commits
  15. 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
  16. 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
  17. 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
  18. 03 Jan, 2017 1 commit
  19. 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
  20. 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
  21. 22 Nov, 2016 3 commits
  22. 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
  23. 16 Nov, 2016 1 commit
  24. 08 Nov, 2016 1 commit
  25. 03 Nov, 2016 3 commits