1. 14 Dec, 2016 3 commits
    • Jason Rhinelander's avatar
      Remove duplicate protected:/private: · b11b1446
      Jason Rhinelander authored
      b11b1446
    • Jason Rhinelander's avatar
      Remove useless `convert` argument from argument_loader · 12ce07a2
      Jason Rhinelander authored
      Since the argument loader split off from the tuple converter, it is
      never called with a `convert` argument set to anything but true.  This
      removes the argument entirely, passing a literal `true` from within
      `argument_loader` to the individual value casters.
      12ce07a2
    • Jason Rhinelander's avatar
      Work around gcc 7 ICE · 23e59c86
      Jason Rhinelander authored
      Current g++ 7 snapshot fails to compile pybind under -std=c++17 with:
      
      ```
      $ make
      [  3%] Building CXX object tests/CMakeFiles/pybind11_tests.dir/pybind11_tests.cpp.o
      In file included from /home/jagerman/src/pybind11/tests/pybind11_tests.h:2:0,
                       from /home/jagerman/src/pybind11/tests/pybind11_tests.cpp:10:
      /home/jagerman/src/pybind11/include/pybind11/pybind11.h: In instantiation of 'pybind11::cpp_function::initialize(Func&&, Return (*)(Args ...), const Extra& ...)::<lambda(pybind11::detail::function_record*, pybind11::handle, pybind11::handle, pybind11::handle)> [with Func = pybind11::cpp_function::cpp_function(Return (Class::*)(Arg ...), const Extra& ...) [with Return = int; Class = ConstructorStats; Arg = {}; Extra = {pybind11::name, pybind11::is_method, pybind11::sibling}]::<lambda(ConstructorStats*)>; Return = int; Args = {ConstructorStats*}; Extra = {pybind11::name, pybind11::is_method, pybind11::sibling}]':
      /home/jagerman/src/pybind11/include/pybind11/pybind11.h:120:22:   required from 'struct pybind11::cpp_function::initialize(Func&&, Return (*)(Args ...), const Extra& ...) [with Func = pybind11::cpp_function::cpp_function(Return (Class::*)(Arg ...), const Extra& ...) [with Return = int; Class = ConstructorStats; Arg = {}; Extra = {pybind11::name, pybind11::is_method, pybind11::sibling}]::<lambda(ConstructorStats*)>; Return = int; Args = {ConstructorStats*}; Extra = {pybind11::name, pybind11::is_method, pybind11::sibling}]::<lambda(struct pybind11::detail::function_record*, class pybind11::handle, class pybind11::handle, class pybind11::handle)>'
      /home/jagerman/src/pybind11/include/pybind11/pybind11.h:120:19:   required from 'void pybind11::cpp_function::initialize(Func&&, Return (*)(Args ...), const Extra& ...) [with Func = pybind11::cpp_function::cpp_function(Return (Class::*)(Arg ...), const Extra& ...) [with Return = int; Class = ConstructorStats; Arg = {}; Extra = {pybind11::name, pybind11::is_method, pybind11::sibling}]::<lambda(ConstructorStats*)>; Return = int; Args = {ConstructorStats*}; Extra = {pybind11::name, pybind11::is_method, pybind11::sibling}]'
      /home/jagerman/src/pybind11/include/pybind11/pybind11.h:62:9:   required from 'pybind11::cpp_function::cpp_function(Return (Class::*)(Arg ...), const Extra& ...) [with Return = int; Class = ConstructorStats; Arg = {}; Extra = {pybind11::name, pybind11::is_method, pybind11::sibling}]'
      /home/jagerman/src/pybind11/include/pybind11/pybind11.h:984:22:   required from 'pybind11::class_<type_, options>& pybind11::class_<type_, options>::def(const char*, Func&&, const Extra& ...) [with Func = int (ConstructorStats::*)(); Extra = {}; type_ = ConstructorStats; options = {}]'
      /home/jagerman/src/pybind11/tests/pybind11_tests.cpp:24:47:   required from here
      /home/jagerman/src/pybind11/include/pybind11/pybind11.h:147:9: sorry, unimplemented: unexpected AST of kind cleanup_stmt
               };
               ^
      /home/jagerman/src/pybind11/include/pybind11/pybind11.h:147:9: internal compiler error: in potential_constant_expression_1, at cp/constexpr.c:5593
      0x84c52a potential_constant_expression_1
      	../../src/gcc/cp/constexpr.c:5593
      0x84c3c0 potential_constant_expression_1
      	../../src/gcc/cp/constexpr.c:5154
      0x645511 finish_function(int)
      	../../src/gcc/cp/decl.c:15527
      0x66e80b instantiate_decl(tree_node*, int, bool)
      	../../src/gcc/cp/pt.c:22558
      0x6b61e2 instantiate_class_template_1
      	../../src/gcc/cp/pt.c:10444
      0x6b61e2 instantiate_class_template(tree_node*)
      	../../src/gcc/cp/pt.c:10514
      0x75a676 complete_type(tree_node*)
      	../../src/gcc/cp/typeck.c:133
      0x67d5a4 tsubst_copy_and_build(tree_node*, tree_node*, int, tree_node*, bool, bool)
      	../../src/gcc/cp/pt.c:17516
      0x67ca19 tsubst_copy_and_build(tree_node*, tree_node*, int, tree_node*, bool, bool)
      	../../src/gcc/cp/pt.c:16655
      0x672cce tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool)
      	../../src/gcc/cp/pt.c:16140
      0x6713dc tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool)
      	../../src/gcc/cp/pt.c:15408
      0x671915 tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool)
      	../../src/gcc/cp/pt.c:15394
      0x671fc0 tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool)
      	../../src/gcc/cp/pt.c:15618
      0x66e97f tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool)
      	../../src/gcc/cp/pt.c:15379
      0x66e97f instantiate_decl(tree_node*, int, bool)
      	../../src/gcc/cp/pt.c:22536
      0x6ba0cb instantiate_pending_templates(int)
      	../../src/gcc/cp/pt.c:22653
      0x6fd7f8 c_parse_final_cleanups()
      	../../src/gcc/cp/decl2.c:4512
      ```
      
      which looks a lot like https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77545.
      
      The error seems to be that it gets confused about the `std::tuple<...>
      value` in argument_loader: it is apparently not being initialized
      properly.  Adding a default constructor with an explicit
      default-initialization of `value` works around the problem.
      23e59c86
  2. 12 Dec, 2016 1 commit
    • Jason Rhinelander's avatar
      Adds automatic casting on assignment of non-pyobject types (#551) · 3f1ff3f4
      Jason Rhinelander authored
      This adds automatic casting when assigning to python types like dict,
      list, and attributes.  Instead of:
      
          dict["key"] = py::cast(val);
          m.attr("foo") = py::cast(true);
          list.append(py::cast(42));
      
      you can now simply write:
      
          dict["key"] = val;
          m.attr("foo") = true;
          list.append(42);
      
      Casts needing extra parameters (e.g. for a non-default rvp) still
      require the py::cast() call. set::add() is also supported.
      
      All usage is channeled through a SFINAE implementation which either just returns or casts. 
      
      Combined non-converting handle and autocasting template methods via a
      helper method that either just returns (handle) or casts (C++ type).
      3f1ff3f4
  3. 07 Dec, 2016 1 commit
  4. 03 Dec, 2016 3 commits
    • 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
    • Dean Moldovan's avatar
      Accept any sequence type as std::tuple or std::pair · 107285b3
      Dean Moldovan authored
      This is more Pythonic and compliments the std::vector and std::list
      casters which also accept sequences.
      107285b3
    • Dean Moldovan's avatar
      Split up tuple caster and function argument loader · 719c1733
      Dean Moldovan authored
      This is needed in order to allow the tuple caster to accept any sequence
      while keeping the argument loader fast. There is also very little overlap
      between the two classes which makes the separation clean. It’s also good
      practice not to have completely new functionality in a specialization.
      719c1733
  5. 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
  6. 24 Nov, 2016 1 commit
  7. 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
  8. 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
  9. 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 ...
      617fbcfc
  10. 12 Nov, 2016 1 commit
  11. 07 Nov, 2016 1 commit
  12. 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
  13. 27 Oct, 2016 1 commit
  14. 24 Oct, 2016 1 commit
  15. 23 Oct, 2016 1 commit
  16. 22 Oct, 2016 1 commit
  17. 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
  18. 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
  19. 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
  20. 30 Sep, 2016 2 commits
  21. 23 Sep, 2016 1 commit
  22. 22 Sep, 2016 2 commits
  23. 19 Sep, 2016 2 commits
  24. 13 Sep, 2016 2 commits