1. 03 Dec, 2016 2 commits
  2. 25 Nov, 2016 2 commits
    • Jason Rhinelander's avatar
      Simplify cast_op return type (#532) · 14bfe622
      Jason Rhinelander authored
      Using a complicated declval here was pointlessly complicated: we
      already know the type, because that's what cast_op_type<T> is in the
      first place.  (The declval also broke MSVC).
      14bfe622
    • Jason Rhinelander's avatar
      Clean up cast operator invocations (#531) · db86f7f2
      Jason Rhinelander authored
      This adds a `detail::cast_op<T>(caster)` function which handles the
      rather verbose:
      
          caster.operator typename CasterType::template cast_op_type<T>()
      
      which allows various places to use the shorter and clearer:
      
          cast_op<T>(caster)
      
      instead of the full verbose cast operator invocation.
      db86f7f2
  3. 24 Nov, 2016 1 commit
  4. 20 Nov, 2016 1 commit
    • Dean Moldovan's avatar
      Always use return_value_policy::move for rvalues (#510) · d079f41c
      Dean Moldovan authored
      Fixes #509.
      
      The move policy was already set for rvalues in PR #473, but this only
      applied to directly cast user-defined types. The problem is that STL
      containers cast values indirectly and the rvalue information is lost.
      Therefore the move policy was not set correctly. This commit fixes it.
      
      This also makes an additional adjustment to remove the `copy` policy
      exception: rvalues now always use the `move` policy. This is also safe
      for copy-only rvalues because the `move` policy has an internal fallback
      to copying.
      d079f41c
  5. 17 Nov, 2016 3 commits
    • 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
  6. 15 Nov, 2016 2 commits
    • Ivan Smirnov's avatar
      Add type casters for nullopt_t, fix none refcount (#499) · 425b4970
      Ivan Smirnov authored
      * Incref returned None in std::optional type caster
      
      * Add type casters for nullopt_t
      
      * Add a test for nullopt_t
      425b4970
    • Jason Rhinelander's avatar
      Fix stl_bind to support movable, non-copyable value types (#490) · 617fbcfc
      Jason Rhinelander authored
      This commit includes the following changes:
      
      * Don't provide make_copy_constructor for non-copyable container
      
      make_copy_constructor currently fails for various stl containers (e.g.
      std::vector, std::unordered_map, std::deque, etc.) when the container's
      value type (e.g. the "T" or the std::pair<K,T> for a map) is
      non-copyable.  This adds an override that, for types that look like
      containers, also requires that the value_type be copyable.
      
      * stl_bind.h: make bind_{vector,map} work for non-copy-constructible types
      
      Most stl_bind modifiers require copying, so if the type isn't copy
      constructible, we provide a read-only interface instead.
      
      In practice, this means that if the type is non-copyable, it will be,
      for all intents and purposes, read-only from the Python side (but
      currently it simply fails to compile with such a container).
      
      It is still possible for the caller to provide an interface manually
      (by defining methods on the returned class_ object), but this isn't
      something stl_bind can handle because the C++ code to construct values
      is going to be highly dependent on the container value_type.
      
      * stl_bind: copy only for arithmetic value types
      
      For non-primitive types, we may well be copying some complex type, when
      returning by reference is more appropriate.  This commit returns by
      internal reference for all but basic arithmetic types.
      
      * Return by reference whenever possible
      
      Only if we definitely can't--i.e. std::vector<bool>--because v[i]
      returns something that isn't a T& do we copy; for everything else, we
      return by reference.
      
      For the map case, we can always return by reference (at least for the
      default stl map/unordered_map).
      617fbcfc
  7. 12 Nov, 2016 1 commit
  8. 07 Nov, 2016 1 commit
  9. 01 Nov, 2016 1 commit
    • Dean Moldovan's avatar
      Make reference(_internal) the default return value policy for properties (#473) · 03f627eb
      Dean Moldovan authored
      * Make reference(_internal) the default return value policy for properties
      
      Before this, all `def_property*` functions used `automatic` as their
      default return value policy. This commit makes it so that:
      
       * Non-static properties use `reference_interal` by default, thus
         matching `def_readonly` and `def_readwrite`.
      
       * Static properties use `reference` by default, thus matching
         `def_readonly_static` and `def_readwrite_static`.
      
      In case `cpp_function` is passed to any `def_property*`, its policy will
      be used instead of any defaults. User-defined arguments in `extras`
      still have top priority and will override both the default policies and
      the ones from `cpp_function`.
      
      Resolves #436.
      
      * Almost always use return_value_policy::move for rvalues
      
      For functions which return rvalues or rvalue references, the only viable
      return value policies are `copy` and `move`. `reference(_internal)` and
      `take_ownership` would take the address of a temporary which is always
      an error.
      
      This commit prevents possible user errors by overriding the bad rvalue
      policies with `move`. Besides `move`, only `copy` is allowed, and only
      if it's explicitly selected by the user.
      
      This is also a necessary safety feature to support the new default
      return value policies for properties: `reference(_internal)`.
      03f627eb
  10. 27 Oct, 2016 1 commit
  11. 24 Oct, 2016 1 commit
  12. 23 Oct, 2016 1 commit
  13. 22 Oct, 2016 1 commit
  14. 20 Oct, 2016 5 commits
    • Ben North's avatar
      Fix wrapper's 'value' and 'owned' if ctor missing · 24a2054d
      Ben North authored
      type_caster_generic::cast(): The values of
      
          wrapper->value
          wrapper->owned
      
      are incorrect in the case that a return value policy of 'copy' is
      requested but there is no copy-constructor.  (Similarly 'move'.)  In
      particular, if the source object is a static instance, the destructor of
      the 'object' 'inst' leads to class_::dealloc() which incorrectly
      attempts to 'delete' the static instance.
      
      This commit re-arranges the code to be clearer as to what the values of
      'value' and 'owned' should be in the various cases.  Behaviour is
      different to previous code only in two situations:
      
      policy = copy but no copy-ctor: Old code leaves 'value = src, owned =
      true', which leads to trouble.  New code leaves 'value = nullptr, owned
      = false', which is correct.
      
      policy = move but no move- or copy-ctor: old code leaves 'value = src,
      owned = true', which leads to trouble.  New code leaves 'value =
      nullptr, owned = false', which is correct.
      24a2054d
    • Ivan Smirnov's avatar
      ccc69f91
    • Ivan Smirnov's avatar
      85e16262
    • Ivan Smirnov's avatar
      Add support for "direct" converters · c275ee6b
      Ivan Smirnov authored
      c275ee6b
    • Dean Moldovan's avatar
      Support std::shared_ptr holder type out of the box · 5d28dd11
      Dean Moldovan authored
      With this there is no more need for manual user declarations like
      `PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>)`. Existing ones
      will still compile without error -- they will just be ignored silently.
      
      Resolves #446.
      5d28dd11
  15. 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
  16. 08 Oct, 2016 1 commit
    • Wenzel Jakob's avatar
      unpacking_collector: allow nullptr-valued kwargs argument · e71ab8f4
      Wenzel Jakob authored
      This fixes an issue that can arise when forwarding (*args, **kwargs)
      captured from a pybind11-bound function call to another Python function.
      When the initial function call includes no keyword arguments, the
      py::kwargs field is set to nullptr and causes a crash later on.
      e71ab8f4
  17. 30 Sep, 2016 2 commits
  18. 23 Sep, 2016 1 commit
  19. 22 Sep, 2016 2 commits
  20. 19 Sep, 2016 2 commits
  21. 13 Sep, 2016 2 commits
  22. 12 Sep, 2016 1 commit
  23. 11 Sep, 2016 3 commits
  24. 10 Sep, 2016 2 commits
    • Dean Moldovan's avatar
    • Dean Moldovan's avatar
      Make error_already_set fetch and hold the Python error · 135ba8de
      Dean Moldovan authored
      This clears the Python error at the error_already_set throw site, thus
      allowing Python calls to be made in destructors which are triggered by
      the exception. This is preferable to the alternative, which would be
      guarding every Python API call with an error_scope.
      
      This effectively flips the behavior of error_already_set. Previously,
      it was assumed that the error stays in Python, so handling the exception
      in C++ would require explicitly calling PyErr_Clear(), but nothing was
      needed to propagate the error to Python. With this change, handling the
      error in C++ does not require a PyErr_Clear() call, but propagating the
      error to Python requires an explicit error_already_set::restore().
      
      The change does not break old code which explicitly calls PyErr_Clear()
      for cleanup, which should be the majority of user code. The need for an
      explicit restore() call does break old code, but this should be mostly
      confined to the library and not user code.
      135ba8de