1. 29 Apr, 2017 4 commits
  2. 28 Apr, 2017 4 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
    • 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. 27 Apr, 2017 3 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
    • Jason Rhinelander's avatar
      Expose more instance management functions · 92900995
      Jason Rhinelander authored
      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).
      92900995
  4. 19 Apr, 2017 1 commit
    • Jason Rhinelander's avatar
      Add static_assert-raising error for overload_cast in c++11 · fb50ce1f
      Jason Rhinelander authored
      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.
      fb50ce1f
  5. 18 Apr, 2017 1 commit
    • 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
  6. 14 Apr, 2017 1 commit
  7. 13 Apr, 2017 4 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
      Move buffer_info to its own header · dbb4c5b5
      Jason Rhinelander authored
      Upcoming changes to buffer_info make it need some things declared in
      common.h; it also feels a bit misplaced in common.h (which is arguably
      too large already), so move it out.  (Separating this and the subsequent
      changes into separate commits to make the changes easier to distinguish
      from the move.)
      dbb4c5b5
    • 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
  8. 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
  9. 08 Apr, 2017 1 commit
    • Jason Rhinelander's avatar
      Add static_assert to holder casters · 501135fa
      Jason Rhinelander authored
      The holder casters assume but don't check that a `holder<type>`'s `type`
      is really a `type_caster_base<type>`; this adds a static_assert to make
      sure this is really the case, to turn things like
      `std::shared_ptr<array>` into a compilation failure.
      
      Fixes #785
      501135fa
  10. 07 Apr, 2017 1 commit
  11. 06 Apr, 2017 1 commit
    • Jason Rhinelander's avatar
      Remove object::borrowed/stolen · 16c86638
      Jason Rhinelander authored
      PR #771 deprecated them as they can cause linking failures (#770), but
      the deprecation tags cause warnings on GCC 5.x through 6.2.x.  Removing
      them entirely will break backwards-compatibility consequences, but the
      effects should be minimal (only code that was inheriting from `object`
      could get at them at all as they are protected).
      
      Fixes #777
      16c86638
  12. 05 Apr, 2017 2 commits
    • Ivan Smirnov's avatar
      Fix -Wmissing-braces warning · 7348c407
      Ivan Smirnov authored
      7348c407
    • 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
  13. 02 Apr, 2017 3 commits
  14. 01 Apr, 2017 1 commit
  15. 30 Mar, 2017 1 commit
  16. 28 Mar, 2017 2 commits
    • Dean Moldovan's avatar
      Support raw string literals as input for py::eval (#766) · 194d8b99
      Dean Moldovan authored
      * Support raw string literals as input for py::eval
      * Dedent only when needed
      194d8b99
    • 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
  17. 26 Mar, 2017 1 commit
  18. 22 Mar, 2017 6 commits
    • Wenzel Jakob's avatar
      d405b1b3
    • Dean Moldovan's avatar
      f0e58a69
    • Wenzel Jakob's avatar
      Nicer API to pass py::capsule destructor (#752) · b16421ed
      Wenzel Jakob authored
      * nicer py::capsule destructor mechanism
      * added destructor-only version of capsule & tests
      * added documentation for module destructors (fixes #733)
      b16421ed
    • 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
    • Dean Moldovan's avatar
      Support class-specific operator new and delete · 0d765f4a
      Dean Moldovan authored
      Fixes #754.
      0d765f4a
  19. 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