1. 10 Sep, 2016 2 commits
    • 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
  2. 09 Sep, 2016 1 commit
    • Jason Rhinelander's avatar
      Implement py::init_alias<>() constructors · ec62d977
      Jason Rhinelander authored
      This commit adds support for forcing alias type initialization by
      defining constructors with `py::init_alias<arg1, arg2>()` instead of
      `py::init<arg1, arg2>()`.  Currently py::init<> only results in Alias
      initialization if the type is extended in python, or the given
      arguments can't be used to construct the base type, but can be used to
      construct the alias.  py::init_alias<>, in contrast, always invokes the
      constructor of the alias type.
      
      It looks like this was already the intention of
      `py::detail::init_alias`, which was forward-declared in
      86d825f3, but was apparently never
      finished: despite the existance of a .def method accepting it, the
      `detail::init_alias` class isn't actually defined anywhere.
      
      This commit completes the feature (or possibly repurposes it), allowing
      declaration of classes that will always initialize the trampoline which
      is (as I argued in #397) sometimes useful.
      ec62d977
  3. 08 Sep, 2016 1 commit
    • Jason Rhinelander's avatar
      Fix type alias initialization · 9c6859ee
      Jason Rhinelander authored
      Type alias for alias classes with members didn't work properly: space
      was only allocated for sizeof(type), but if we want to be able to put a
      type_alias instance there, we need sizeof(type_alias), but
      sizeof(type_alias) > sizeof(type) whenever type_alias has members.
      9c6859ee
  4. 07 Sep, 2016 4 commits
    • 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
    • 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
  5. 06 Sep, 2016 9 commits
    • Dean Moldovan's avatar
      Replace std::cout with py::print in tests · 81511be3
      Dean Moldovan authored
      With this change both C++ and Python write to sys.stdout which resolves
      the capture issues noted in #351. Therefore, the related workarounds are
      removed.
      81511be3
    • 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
      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
      Add py::str::format() method · 66aa2728
      Dean Moldovan authored
      66aa2728
    • Dean Moldovan's avatar
      Add py::print() function · 67990d9e
      Dean Moldovan authored
      Replicates Python API including keyword arguments.
      67990d9e
    • 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
  6. 05 Sep, 2016 1 commit
  7. 04 Sep, 2016 2 commits
    • 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
    • Wenzel Jakob's avatar
      minor code style checker update · 85f07e18
      Wenzel Jakob authored
      85f07e18
  8. 03 Sep, 2016 1 commit
    • Jason Rhinelander's avatar
      Make test initialization self-registering · 52f4be89
      Jason Rhinelander authored
      Adding or removing tests is a little bit cumbersome currently: the test
      needs to be added to CMakeLists.txt, the init function needs to be
      predeclared in pybind11_tests.cpp, then called in the plugin
      initialization.  While this isn't a big deal for tests that are being
      committed, it's more of a hassle when working on some new feature or
      test code for which I temporarily only care about building and linking
      the test being worked on rather than the entire test suite.
      
      This commit changes tests to self-register their initialization by
      having each test initialize a local object (which stores the
      initialization function in a static variable).  This makes changing the
      set of tests being build easy: one only needs to add or comment out
      test names in tests/CMakeLists.txt.
      
      A couple other minor changes that go along with this:
      
      - test_eigen.cpp is now included in the test list, then removed if eigen
        isn't available.  This lets you disable the eigen tests by commenting
        it out, just like all the other tests, but keeps the build working
        without eigen eigen isn't available.  (Also, if it's commented out, we
        don't even bother looking for and reporting the building with/without
        eigen status message).
      
      - pytest is now invoked with all the built test names (with .cpp changed
        to .py) so that it doesn't try to run tests that weren't built.
      52f4be89
  9. 29 Aug, 2016 1 commit
    • Jason Rhinelander's avatar
      Fix template trampoline overload lookup failure · 20978263
      Jason Rhinelander authored
      Problem
      =======
      
      The template trampoline pattern documented in PR #322 has a problem with
      virtual method overloads in intermediate classes in the inheritance
      chain between the trampoline class and the base class.
      
      For example, consider the following inheritance structure, where `B` is
      the actual class, `PyB<B>` is the trampoline class, and `PyA<B>` is an
      intermediate class adding A's methods into the trampoline:
      
          PyB<B> -> PyA<B> -> B -> A
      
      Suppose PyA<B> has a method `some_method()` with a PYBIND11_OVERLOAD in
      it to overload the virtual `A::some_method()`.  If a Python class `C` is
      defined that inherits from the pybind11-registered `B` and tries to
      provide an overriding `some_method()`, the PYBIND11_OVERLOADs declared
      in PyA<B> fails to find this overloaded method, and thus never invoke it
      (or, if pure virtual and not overridden in PyB<B>, raises an exception).
      
      This happens because the base (internal) `PYBIND11_OVERLOAD_INT` macro
      simply calls `get_overload(this, name)`; `get_overload()` then uses the
      inferred type of `this` to do a type lookup in `registered_types_cpp`.
      This is where it fails: `this` will be a `PyA<B> *`, but `PyA<B>` is
      neither the base type (`B`) nor the trampoline type (`PyB<B>`).  As a
      result, the overload fails and we get a failed overload lookup.
      
      The fix
      =======
      
      The fix is relatively simple: we can cast `this` passed to
      `get_overload()` to a `const B *`, which lets get_overload look up the
      correct class.  Since trampoline classes should be derived from `B`
      classes anyway, this cast should be perfectly safe.
      
      This does require adding the class name as an argument to the
      PYBIND11_OVERLOAD_INT macro, but leaves the public macro signatures
      unchanged.
      20978263
  10. 28 Aug, 2016 1 commit
  11. 26 Aug, 2016 1 commit
    • Jason Rhinelander's avatar
      Don't install pytest from cmake, just fail instead · dd3d56a8
      Jason Rhinelander authored
      Installing something outside the project directory from a cmake
      invocation is overly intrusive; this changes tests/CMakeLists.txt to
      just fail with an informative message instead, and changes the
      travis-ci builds to install pytest via pip or apt-get.
      dd3d56a8
  12. 25 Aug, 2016 2 commits
  13. 24 Aug, 2016 2 commits
  14. 19 Aug, 2016 7 commits