1. 30 Sep, 2016 2 commits
  2. 23 Sep, 2016 1 commit
  3. 22 Sep, 2016 2 commits
  4. 19 Sep, 2016 2 commits
  5. 13 Sep, 2016 2 commits
  6. 12 Sep, 2016 1 commit
  7. 11 Sep, 2016 3 commits
  8. 10 Sep, 2016 3 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
    • Wenzel Jakob's avatar
      RAII wrapper for error state · 720136bf
      Wenzel Jakob authored
      720136bf
  9. 08 Sep, 2016 1 commit
  10. 07 Sep, 2016 6 commits
    • Ivan Smirnov's avatar
      Use handle::is_none() instead of raw ptrs · 984c7624
      Ivan Smirnov authored
      984c7624
    • Ivan Smirnov's avatar
      67b54894
    • Jason Rhinelander's avatar
      Fail static_assert when trying to reference non-referencable types · c03db9ba
      Jason Rhinelander authored
      The previous commit to address #392 triggers a compiler warning about
      returning a reference to a local variable, which is *not* a false alarm:
      the following:
      
          py::cast<int &>(o)
      
      (which happens internally in an overload declaration) really is
      returning a reference to a local, because the cast operators for the
      type_caster for numeric types returns a reference to its own member.
      
      This commit adds a static_assert to make that a compilation failure
      rather than returning a reference into about-to-be-freed memory.
      
      Incidentally, this is also a fix for #219, which is exactly the same
      issue: we can't reference numeric primitives that are cast from
      wrappers around python numeric types.
      c03db9ba
    • Ivan Smirnov's avatar
      392f16cc
    • Jason Rhinelander's avatar
      Fix type caster for heap reference types · 56f71775
      Jason Rhinelander authored
      Need to use the intrinsic type, not the raw type.
      
      Fixes #392.
      56f71775
    • Jason Rhinelander's avatar
      Allow passing base types as a template parameter · 6b52c838
      Jason Rhinelander authored
      This allows a slightly cleaner base type specification of:
      
          py::class_<Type, Base>("Type")
      
      as an alternative to
      
          py::class_<Type>("Type", py::base<Base>())
      
      As with the other template parameters, the order relative to the holder
      or trampoline types doesn't matter.
      
      This also includes a compile-time assertion failure if attempting to
      specify more than one base class (but is easily extendible to support
      multiple inheritance, someday, by updating the class_selector::set_bases
      function to set multiple bases).
      6b52c838
  11. 06 Sep, 2016 9 commits
    • Jason Rhinelander's avatar
      Allow arbitrary class_ template option ordering · 5fffe200
      Jason Rhinelander authored
      The current pybind11::class_<Type, Holder, Trampoline> fixed template
      ordering results in a requirement to repeat the Holder with its default
      value (std::unique_ptr<Type>) argument, which is a little bit annoying:
      it needs to be specified not because we want to override the default,
      but rather because we need to specify the third argument.
      
      This commit removes this limitation by making the class_ template take
      the type name plus a parameter pack of options.  It then extracts the
      first valid holder type and the first subclass type for holder_type and
      trampoline type_alias, respectively.  (If unfound, both fall back to
      their current defaults, `std::unique_ptr<type>` and `type`,
      respectively).  If any unmatched template arguments are provided, a
      static assertion fails.
      
      What this means is that you can specify or omit the arguments in any
      order:
      
          py::class_<A, PyA> c1(m, "A");
          py::class_<B, PyB, std::shared_ptr<B>> c2(m, "B");
          py::class_<C, std::shared_ptr<C>, PyB> c3(m, "C");
      
      It also allows future class attributes (such as base types in the next
      commit) to be passed as class template types rather than needing to use
      a py::base<> wrapper.
      5fffe200
    • Wenzel Jakob's avatar
      c84b37b5
    • Dean Moldovan's avatar
      Make keyword argument hold a py::object instead of T* · 60b26802
      Dean Moldovan authored
      With this change arg_t is no longer a template, but it must remain so
      for backward compatibility. Thus, a non-template arg_v is introduced,
      while a dummy template alias arg_t is there to keep old code from
      breaking. This can be remove in the next major release.
      
      The implementation of arg_v also needed to be placed a little earlier in
      the headers because it's not a template any more and unpacking_collector
      needs more than a forward declaration.
      60b26802
    • Dean Moldovan's avatar
      8fe13b88
    • Dean Moldovan's avatar
      Workaround for py::dict() constructor on MSVC · 56e86ed0
      Dean Moldovan authored
      MSVC fails to compile if the constructor is defined out-of-line.
      The error states that it cannot deduce the type of the default template
      parameter which is used for SFINAE.
      56e86ed0
    • Dean Moldovan's avatar
      Remove superseded handle::operator() overloads · 16db1bfb
      Dean Moldovan authored
      The variadic handle::operator() offers the same functionality as well
      as mixed positional, keyword, * and ** arguments. The tests are also
      superseded by the ones in `test_callbacks`.
      16db1bfb
    • Dean Moldovan's avatar
      Add py::dict() keyword constructor · 15a112f8
      Dean Moldovan authored
      15a112f8
    • Dean Moldovan's avatar
      Support keyword arguments and generalized unpacking in C++ · c743e1b1
      Dean Moldovan authored
      A Python function can be called with the syntax:
      ```python
      foo(a1, a2, *args, ka=1, kb=2, **kwargs)
      ```
      This commit adds support for the equivalent syntax in C++:
      ```c++
      foo(a1, a2, *args, "ka"_a=1, "kb"_a=2, **kwargs)
      ```
      
      In addition, generalized unpacking is implemented, as per PEP 448,
      which allows calls with multiple * and ** unpacking:
      ```python
      bar(*args1, 99, *args2, 101, **kwargs1, kz=200, **kwargs2)
      ```
      and
      ```c++
      bar(*args1, 99, *args2, 101, **kwargs1, "kz"_a=200, **kwargs2)
      ```
      c743e1b1
    • Wenzel Jakob's avatar
      minor doc & style fixes · fe34241e
      Wenzel Jakob authored
      fe34241e
  12. 04 Sep, 2016 1 commit
    • Jason Rhinelander's avatar
      Make unique_ptr's with non-default deleters work · a6495af8
      Jason Rhinelander authored
      Currently pybind11 only supports std::unique_ptr<T> holders by default
      (other holders can, of course, be declared using the macro).  PR #368
      added a `py::nodelete` that is intended to be used as:
      
          py::class_<Type, std::unique_ptr<Type, py::nodelete>> c("Type");
      
      but this doesn't work out of the box.  (You could add an explicit
      holder type declaration, but this doesn't appear to have been the
      intention of the commit).
      
      This commit fixes it by generalizing the unique_ptr type_caster to take
      both the type and deleter as template arguments, so that *any*
      unique_ptr instances are now automatically handled by pybind.  It also
      adds a test to test_smart_ptr, testing both that py::nodelete (now)
      works, and that the object is indeed not deleted as intended.
      a6495af8
  13. 12 Aug, 2016 1 commit
    • Jason Rhinelander's avatar
      Added pybind11::make_key_iterator for map iteration · 5aa85be2
      Jason Rhinelander authored
      This allows exposing a dict-like interface to python code, allowing
      iteration over keys via:
      
          for k in custommapping:
              ...
      
      while still allowing iteration over pairs, so that you can also
      implement 'dict.items()' functionality which returns a pair iterator,
      allowing:
      
          for k, v in custommapping.items():
              ...
      
      example-sequences-and-iterators is updated with a custom class providing
      both types of iteration.
      5aa85be2
  14. 11 Aug, 2016 1 commit
    • Jason Rhinelander's avatar
      Silence MSVC warning · e20fc61a
      Jason Rhinelander authored
      PR #329 generates the following warning under MSVC:
      
          ...\cast.h(202): warning C4456: declaration of 'it' hides previous local declaration
      
      This renames the second iterator to silence it.
      e20fc61a
  15. 10 Aug, 2016 1 commit
    • Jason Rhinelander's avatar
      Implement reference_internal with a keep_alive · f2ecd892
      Jason Rhinelander authored
      reference_internal requires an `instance` field to track the returned
      reference's parent, but that's just a duplication of what
      keep_alive<0,1> does, so use a keep alive to do this to eliminate the
      duplication.
      f2ecd892
  16. 09 Aug, 2016 1 commit
    • Jason Rhinelander's avatar
      Track registered instances that share a pointer address · 1b05ce5b
      Jason Rhinelander authored
      The pointer to the first member of a class instance is the same as the
      pointer to instance itself; pybind11 has some workarounds for this to
      not track registered instances that have a registered parent with the
      same address.  This doesn't work everywhere, however: issue #328 is a
      failure of this for a mutator operator which resolves its argument to
      the parent rather than the child, as is needed in #328.
      
      This commit resolves the issue (and restores tracking of same-address
      instances) by changing registered_instances from an unordered_map to an
      unordered_multimap that allows duplicate instances for the same pointer
      to be recorded, then resolves these differences by checking the type of
      each matched instance when looking up an instance.  (A
      unordered_multimap seems cleaner for this than a unordered_map<list> or
      similar because, the vast majority of the time, the instance will be
      unique).
      1b05ce5b
  17. 08 Aug, 2016 1 commit
    • Jason Rhinelander's avatar
      Move support for return values of called Python functions · ed14879a
      Jason Rhinelander authored
      Currently pybind11 always translates values returned by Python functions
      invoked from C++ code by copying, even when moving is feasible--and,
      more importantly, even when moving is required.
      
      The first, and relatively minor, concern is that moving may be
      considerably more efficient for some types.  The second problem,
      however, is more serious: there's currently no way python code can
      return a non-copyable type to C++ code.
      
      I ran into this while trying to add a PYBIND11_OVERLOAD of a virtual
      method that returns just such a type: it simply fails to compile because
      this:
      
          overload = ...
          overload(args).template cast<ret_type>();
      
      involves a copy: overload(args) returns an object instance, and the
      invoked object::cast() loads the returned value, then returns a copy of
      the loaded value.
      
      We can, however, safely move that returned value *if* the object has the
      only reference to it (i.e. if ref_count() == 1) and the object is
      itself temporary (i.e. if it's an rvalue).
      
      This commit does that by adding an rvalue-qualified object::cast()
      method that allows the returned value to be move-constructed out of the
      stored instance when feasible.
      
      This basically comes down to three cases:
      
      - For objects that are movable but not copyable, we always try the move,
        with a runtime exception raised if this would involve moving a value
        with multiple references.
      - When the type is both movable and non-trivially copyable, the move
        happens only if the invoked object has a ref_count of 1, otherwise the
        object is copied.  (Trivially copyable types are excluded from this
        case because they are typically just collections of primitive types,
        which can be copied just as easily as they can be moved.)
      - Non-movable and trivially copy constructible objects are simply
        copied.
      
      This also adds examples to example-virtual-functions that shows both a
      non-copyable object and a movable/copyable object in action: the former
      raises an exception if returned while holding a reference, the latter
      invokes a move constructor if unreferenced, or a copy constructor if
      referenced.
      
      Basically this allows code such as:
      
          class MyClass(Pybind11Class):
              def somemethod(self, whatever):
                  mt = MovableType(whatever)
                  # ...
                  return mt
      
      which allows the MovableType instance to be returned to the C++ code
      via its move constructor.
      
      Of course if you attempt to violate this by doing something like:
      
          self.value = MovableType(whatever)
          return self.value
      
      you get an exception--but right now, the pybind11-side of that code
      won't compile at all.
      ed14879a
  18. 04 Aug, 2016 1 commit
  19. 18 Jul, 2016 1 commit