1. 21 Jul, 2021 1 commit
    • jesse-sony's avatar
      Feature/local exception translator (#2650) · d65edfb0
      jesse-sony authored
      * Create a module_internals struct
      
      Since we now have two things that are going to be module local, it felt
      correct to add a struct to manage them.
      
      * Add local exception translators
      
      These are added via the  register_local_exception_translator function
      and are then applied before the global translators
      
      * Add unit tests to show the local exception translator works
      
      * Fix a bug in the unit test with the string value of KeyError
      
      * Fix a formatting issue
      
      * Rename registered_local_types_cpp()
      
      Rename it to get_registered_local_types_cpp() to disambiguate from the
      new member of module_internals
      
      * Add additional comments to new local exception code path
      
      * Add a register_local_exception function
      
      * Add additional unit tests for register_local_exception
      
      * Use get_local_internals like get_internals
      
      * Update documentation for new local exception feature
      
      * Add back a missing space
      
      * Clean-up some issues in the docs
      
      * Remove the code duplication when translating exceptions
      
      Separated out the exception processing into a standalone function in the
      details namespace.
      
      Clean-up some comments as per PR notes as well
      
      * Remove the code duplication in register_exception
      
      * Cleanup some formatting things caught by clang-format
      
      * Remove the templates from exception translators
      
      But I added a using declaration to alias the type.
      
      * Remove the extra local from local_internals variable names
      
      * Add an extra explanatory comment to local_internals
      
      * Fix a typo in the code
      d65edfb0
  2. 12 Jul, 2021 1 commit
    • Ralf W. Grosse-Kunstleve's avatar
      NOLINT reduction (#3096) · 2d468697
      Ralf W. Grosse-Kunstleve authored
      * Copying from prework_no_rst branch (PR #3087): test_numpy_array.cpp, test_stl.cpp
      
      * Manual changes reducing NOLINTs.
      
      * clang-format-diff.py
      
      * Minor adjustment to avoid MSVC warning C4702: unreachable code
      2d468697
  3. 22 Jun, 2021 1 commit
    • Aaron Gokaslan's avatar
      fix(clang-tidy): performance fixes applied in tests and CI (#3051) · dac74ebd
      Aaron Gokaslan authored
      * Initial fixes
      
      * Whoops
      
      * Finish clang-tidy manual fixes
      
      * Add two missing fixes
      
      * Revert
      
      * Update clang-tidy
      
      * Try to fix unreachable code error
      
      * Move nolint comment
      
      * Apply missing fix
      
      * Don't override clang-tidy config
      
      * Does this fix clang-tidy?
      
      * Make all clang-tidy errors visible
      
      * Add comments about NOLINTs and remove a few
      
      * Fix typo
      dac74ebd
  4. 27 May, 2021 1 commit
  5. 29 Oct, 2020 1 commit
    • Ralf W. Grosse-Kunstleve's avatar
      clang -Wnon-virtual-dtor compatibility (#2626) · 8290a5a0
      Ralf W. Grosse-Kunstleve authored
      * Adding missing virtual destructors, to silence clang -Wnon-virtual-dtor warnings.
      
      Tested with clang version 9.0.1-12 under an Ubuntu-like OS.
      Originally discovered in the Google-internal environment.
      
      * adding -Wnon-virtual-dtor for GNU|Intel|Clang
      8290a5a0
  6. 03 Oct, 2020 1 commit
  7. 15 Sep, 2020 1 commit
  8. 18 Aug, 2020 1 commit
  9. 16 Aug, 2020 1 commit
  10. 14 Nov, 2019 1 commit
  11. 12 May, 2019 1 commit
  12. 11 Jan, 2018 1 commit
  13. 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
  14. 27 Jun, 2017 1 commit
  15. 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
  16. 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
  17. 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
  18. 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
  19. 07 Sep, 2016 1 commit
  20. 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
  21. 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
  22. 18 Jul, 2016 1 commit
    • Jason Rhinelander's avatar
      Rename examples files, as per #288 · b3f3d79f
      Jason Rhinelander authored
      This renames example files from `exampleN` to `example-description`.
      
      Specifically, the following renaming is applied:
      
      example1 -> example-methods-and-attributes
      example2 -> example-python-types
      example3 -> example-operator-overloading
      example4 -> example-constants-and-functions
      example5 -> example-callbacks (*)
      example6 -> example-sequence-and-iterators
      example7 -> example-buffers
      example8 -> example-custom-ref-counting
      example9 -> example-modules
      example10 -> example-numpy-vectorize
      example11 -> example-arg-keywords-and-defaults
      example12 -> example-virtual-functions
      example13 -> example-keep-alive
      example14 -> example-opaque-types
      example15 -> example-pickling
      example16 -> example-inheritance
      example17 -> example-stl-binders
      example18 -> example-eval
      example19 -> example-custom-exceptions
      
      * the inheritance parts of example5 are moved into example-inheritance
      (previously example16), and the remainder is left as example-callbacks.
      
      This commit also renames the internal variables ("Example1",
      "Example2", "Example4", etc.) into non-numeric names ("ExampleMandA",
      "ExamplePythonTypes", "ExampleWithEnum", etc.) to correspond to the
      file renaming.
      
      The order of tests is preserved, but this can easily be changed if
      there is some more natural ordering by updating the list in
      examples/CMakeLists.txt.
      b3f3d79f
  23. 11 Jul, 2016 1 commit