1. 18 Aug, 2020 1 commit
  2. 16 Aug, 2020 1 commit
  3. 20 Jul, 2020 1 commit
  4. 14 Nov, 2019 1 commit
  5. 12 May, 2019 1 commit
  6. 04 Aug, 2017 1 commit
    • Jason Rhinelander's avatar
      Fix builtin exception handlers to work across modules · d5981729
      Jason Rhinelander authored
      The builtin exception handler currently doesn't work across modules
      under clang/libc++ for builtin pybind exceptions like
      `pybind11::error_already_set` or `pybind11::stop_iteration`: under
      RTLD_LOCAL module loading clang considers each module's exception
      classes distinct types.  This then means that the base exception
      translator fails to catch the exceptions and the fall through to the
      generic `std::exception` handler, which completely breaks things like
      `stop_iteration`: only the `stop_iteration` of the first module loaded
      actually works properly; later modules raise a RuntimeError with no
      message when trying to invoke their iterators.
      
      For example, two modules defined like this exhibit the behaviour under
      clang++/libc++:
      
      z1.cpp:
          #include <pybind11/pybind11.h>
          #include <pybind11/stl_bind.h>
          namespace py = pybind11;
          PYBIND11_MODULE(z1, m) {
              py::bind_vector<std::vector<long>>(m, "IntVector");
          }
      
      z2.cpp:
          #include <pybind11/pybind11.h>
          #include <pybind11/stl_bind.h>
          namespace py = pybind11;
          PYBIND11_MODULE(z2, m) {
              py::bind_vector<std::vector<double>>(m, "FloatVector");
          }
      
      Python:
          import z1, z2
          for i in z2.FloatVector():
              pass
      
      results in:
          Traceback (most recent call last):
            File "zs.py", line 2, in <module>
              for i in z2.FloatVector():
          RuntimeError
      
      This commit fixes the issue by adding a new exception translator each
      time the internals pointer is initialized from python builtins: this
      generally means the internals data was initialized by some other
      module.  (The extra translator(s) are skipped under libstdc++).
      d5981729
  7. 29 Jul, 2017 2 commits
    • Jason Rhinelander's avatar
      Simplify error_already_set · 1682b673
      Jason Rhinelander authored
      `error_already_set` is more complicated than it needs to be, partly
      because it manages reference counts itself rather than using
      `py::object`, and partly because it tries to do more exception clearing
      than is needed.  This commit greatly simplifies it, and fixes #927.
      
      Using `py::object` instead of `PyObject *` means we can rely on
      implicit copy/move constructors.
      
      The current logic did both a `PyErr_Clear` on deletion *and* a
      `PyErr_Fetch` on creation.  I can't see how the `PyErr_Clear` on
      deletion is ever useful: the `Fetch` on creation itself clears the
      error, so the only way doing a `PyErr_Clear` on deletion could do
      anything if is some *other* exception was raised while the
      `error_already_set` object was alive--but in that case, clearing some
      other exception seems wrong.  (Code that is worried about an exception
      handler raising another exception would already catch a second
      `error_already_set` from exception code).
      
      The destructor itself called `clear()`, but `clear()` was a little bit
      more paranoid that needed: it called `restore()` to restore the
      currently captured error, but then immediately cleared it, using the
      `PyErr_Restore` to release the references.  That's unnecessary: it's
      valid for us to release the references manually.  This updates the code
      to simply release the references on the three objects (preserving the
      gil acquire).
      
      `clear()`, however, also had the side effect of clearing the current
      error, even if the current `error_already_set` didn't have a current
      error (e.g. because of a previous `restore()` or `clear()` call).  I
      don't really see how clearing the error here can ever actually be
      useful: the only way the current error could be set is if you called
      `restore()` (in which case the current stored error-related members have
      already been released), or if some *other* code raised the error, in
      which case `clear()` on *this* object is clearing an error for which it
      shouldn't be responsible.
      
      Neither of those seem like intentional or desirable features, and
      manually requesting deletion of the stored references similarly seems
      pointless, so I've just made `clear()` an empty method and marked it
      deprecated.
      
      This also fixes a minor potential issue with the destruction: it is
      technically possible for `value` to be null (though this seems likely to
      be rare in practice); this updates the check to look at `type` which
      will always be non-null for a `Fetch`ed exception.
      
      This also adds error_already_set round-trip throw tests to the test
      suite.
      1682b673
    • Jason Rhinelander's avatar
      abcf43d5
  8. 27 Jun, 2017 1 commit
  9. 02 Apr, 2017 1 commit
    • Roman Miroshnychenko's avatar
      Add a method to check Python exception types (#772) · 83a8a977
      Roman Miroshnychenko authored
      This commit adds `error_already_set::matches()` convenience method to
      check if the exception trapped by `error_already_set` matches a given
      Python exception type. This will address #700 by providing a less
      verbose way to check exceptions.
      83a8a977
  10. 20 Nov, 2016 1 commit
  11. 16 Sep, 2016 1 commit
    • Jason Rhinelander's avatar
      Added py::register_exception for simple case (#296) · b3794f10
      Jason Rhinelander authored
      The custom exception handling added in PR #273 is robust, but is overly
      complex for declaring the most common simple C++ -> Python exception
      mapping that needs only to copy `what()`.  This add a simpler
      `py::register_exception<CppExp>(module, "PyExp");` function that greatly
      simplifies the common basic case of translation of a simple CppException
      into a simple PythonException, while not removing the more advanced
      capabilities of defining custom exception handlers.
      b3794f10
  12. 10 Sep, 2016 1 commit
    • 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
  13. 07 Sep, 2016 1 commit
  14. 19 Aug, 2016 1 commit
    • Dean Moldovan's avatar
      Port tests to pytest · a0c1ccf0
      Dean Moldovan authored
      Use simple asserts and pytest's powerful introspection to make testing
      simpler. This merges the old .py/.ref file pairs into simple .py files
      where the expected values are right next to the code being tested.
      
      This commit does not touch the C++ part of the code and replicates the
      Python tests exactly like the old .ref-file-based approach.
      a0c1ccf0