pybind11/tests
Jason Rhinelander 1682b67326 Simplify error_already_set
`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.
2017-07-28 20:40:35 -04:00
..
test_cmake_build Move test_cmake_build target code into its subdirectory 2017-06-27 10:38:41 +02:00
test_embed Fix embedded threads test on MSVC2015 / Python 2.7 2017-06-24 21:59:55 +02:00
CMakeLists.txt Move test_cmake_build target code into its subdirectory 2017-06-27 10:38:41 +02:00
conftest.py Minor pytest maintenance (#702) 2017-03-10 15:42:42 +01:00
constructor_stats.h Support multiple inheritance from python 2017-06-12 09:56:55 -03:00
object.h Improve custom holder support (#607) 2017-01-31 17:05:44 +01:00
pybind11_tests.cpp Split test_python_types.cpp into builtin_casters, stl and pytypes 2017-06-27 10:38:41 +02:00
pybind11_tests.h Use rvalue subcasting when casting an rvalue container 2017-07-05 12:27:14 -04:00
pytest.ini Don't let pytest discover tests from test_cmake_build and test_embed 2017-06-10 16:44:21 +02:00
test_buffers.cpp Convenience constructor templates for buffer_info (#860) 2017-05-29 03:13:55 +02:00
test_buffers.py Fix buffer protocol inheritance 2017-05-29 02:03:58 +02:00
test_builtin_casters.cpp Implicit conversions to bool + np.bool_ conversion (#925) 2017-07-23 11:02:43 -04:00
test_builtin_casters.py Implicit conversions to bool + np.bool_ conversion (#925) 2017-07-23 11:02:43 -04:00
test_call_policies.cpp Hold strong references to keep_alive patients 2017-06-24 12:59:46 -04:00
test_call_policies.py Make init_holder do registration, and rename to init_instance 2017-07-28 20:39:33 -04:00
test_callbacks.cpp Split test_python_types.cpp into builtin_casters, stl and pytypes 2017-06-27 10:38:41 +02:00
test_callbacks.py functional: support bound methods 2017-04-29 10:43:17 -04:00
test_chrono.cpp Fix for floating point durations 2017-03-11 23:04:16 -04:00
test_chrono.py Fix for floating point durations 2017-03-11 23:04:16 -04:00
test_class.cpp Add support custom sized operator deletes (#952) 2017-07-23 00:32:58 -04:00
test_class.py Add support custom sized operator deletes (#952) 2017-07-23 00:32:58 -04:00
test_constants_and_functions.cpp Support binding noexcept function/methods in C++17 2016-12-14 20:40:49 +01:00
test_constants_and_functions.py Support binding noexcept function/methods in C++17 2016-12-14 20:40:49 +01:00
test_copy_move.cpp Move tests from test_issues.cpp/py into appropriate files 2017-06-27 10:38:41 +02:00
test_copy_move.py Fix test suite under MSVC/Debug 2017-07-12 11:50:40 -04:00
test_docstring_options.cpp Fix extra docstring newlines under options.disable_function_signatures() 2017-03-08 12:32:42 -05:00
test_docstring_options.py Fix extra docstring newlines under options.disable_function_signatures() 2017-03-08 12:32:42 -05:00
test_eigen.cpp Fix indirect loading of Eigen::Ref 2017-06-29 11:31:54 +02:00
test_eigen.py Fix indirect loading of Eigen::Ref 2017-06-29 11:31:54 +02:00
test_enum.cpp enum_: fix implicit conversion on Python 2.7 2017-04-29 16:35:28 +02:00
test_enum.py pytest target: add USE_TERMINAL flag 2017-04-29 16:35:28 +02:00
test_eval_call.py Add checks to maintain a consistent Python code style and prevent bugs (#515) 2016-11-20 21:21:54 +01:00
test_eval.cpp Add py::exec() as a shortcut for py::eval<py::eval_statements>() 2017-05-08 20:46:16 +02:00
test_eval.py Rewrite eval tests to allow for simple asserts 2016-08-19 16:31:48 +02:00
test_exceptions.cpp Simplify error_already_set 2017-07-28 20:40:35 -04:00
test_exceptions.py Simplify error_already_set 2017-07-28 20:40:35 -04:00
test_kwargs_and_defaults.cpp Add support for positional args with args/kwargs 2017-01-31 17:24:41 +01:00
test_kwargs_and_defaults.py Minor pytest maintenance (#702) 2017-03-10 15:42:42 +01:00
test_methods_and_attributes.cpp Support take_ownership for custom type casters given a pointer 2017-07-16 11:04:43 -04:00
test_methods_and_attributes.py Support take_ownership for custom type casters given a pointer 2017-07-16 11:04:43 -04:00
test_modules.cpp Move tests from test_issues.cpp/py into appropriate files 2017-06-27 10:38:41 +02:00
test_modules.py Split test_python_types.cpp into builtin_casters, stl and pytypes 2017-06-27 10:38:41 +02:00
test_multiple_inheritance.cpp Make init_holder do registration, and rename to init_instance 2017-07-28 20:39:33 -04:00
test_multiple_inheritance.py Make init_holder do registration, and rename to init_instance 2017-07-28 20:39:33 -04:00
test_numpy_array.cpp Now shape, size, ndims and itemsize are also signed integers. 2017-05-08 01:50:21 +02:00
test_numpy_array.py Now shape, size, ndims and itemsize are also signed integers. 2017-05-08 01:50:21 +02:00
test_numpy_dtypes.cpp Allow std::complex field with PYBIND11_NUMPY_DTYPE (#831) 2017-05-10 11:36:24 +02:00
test_numpy_dtypes.py Allow std::complex field with PYBIND11_NUMPY_DTYPE (#831) 2017-05-10 11:36:24 +02:00
test_numpy_vectorize.cpp vectorize: pass-through of non-vectorizable args 2017-05-24 20:43:41 -04:00
test_numpy_vectorize.py vectorize: pass-through of non-vectorizable args 2017-05-24 20:43:41 -04:00
test_opaque_types.cpp Make test initialization self-registering 2016-09-03 17:34:41 -04:00
test_opaque_types.py Prefer non-converting argument overloads 2017-02-03 20:47:17 -05:00
test_operator_overloading.cpp Move tests from test_issues.cpp/py into appropriate files 2017-06-27 10:38:41 +02:00
test_operator_overloading.py Move tests from test_issues.cpp/py into appropriate files 2017-06-27 10:38:41 +02:00
test_pickling.cpp fix segfault in test suite due to typo (fixes #586) 2017-01-04 15:05:20 +01:00
test_pickling.py WIP: PyPy support (#527) 2016-12-16 15:00:46 +01:00
test_pytypes.cpp Split test_python_types.cpp into builtin_casters, stl and pytypes 2017-06-27 10:38:41 +02:00
test_pytypes.py Split test_python_types.cpp into builtin_casters, stl and pytypes 2017-06-27 10:38:41 +02:00
test_sequences_and_iterators.cpp Move tests from test_issues.cpp/py into appropriate files 2017-06-27 10:38:41 +02:00
test_sequences_and_iterators.py Split test_python_types.cpp into builtin_casters, stl and pytypes 2017-06-27 10:38:41 +02:00
test_smart_ptr.cpp Move tests from test_issues.cpp/py into appropriate files 2017-06-27 10:38:41 +02:00
test_smart_ptr.py Move tests from test_issues.cpp/py into appropriate files 2017-06-27 10:38:41 +02:00
test_stl_binders.cpp Use rvalue subcasting when casting an rvalue container 2017-07-05 12:27:14 -04:00
test_stl_binders.py Fix invalid memory access in vector insert method 2017-05-25 10:51:28 -04:00
test_stl.cpp Fix regression: container pointers not castable 2017-07-16 11:04:43 -04:00
test_stl.py Fix regression: container pointers not castable 2017-07-16 11:04:43 -04:00
test_virtual_functions.cpp Move tests from short translation units into their logical parents 2017-06-27 10:38:41 +02:00
test_virtual_functions.py Move tests from short translation units into their logical parents 2017-06-27 10:38:41 +02:00