-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include <nanobind/nanobind.h> | ||
|
||
namespace nb = nanobind; | ||
|
||
NB_MODULE(test_exception_ext, m) { | ||
m.def("raise_generic", [] { throw std::exception(); }); | ||
m.def("raise_bad_alloc", [] { throw std::bad_alloc(); }); | ||
m.def("raise_runtime_error", [] { throw std::runtime_error("a runtime error"); }); | ||
m.def("raise_domain_error", [] { throw std::domain_error("a domain error"); }); | ||
m.def("raise_invalid_argument", [] { throw std::invalid_argument("an invalid argument error"); }); | ||
m.def("raise_length_error", [] { throw std::length_error("a length error"); }); | ||
m.def("raise_out_of_range", [] { throw std::out_of_range("an out of range error"); }); | ||
m.def("raise_range_error", [] { throw std::range_error("a range error"); }); | ||
m.def("raise_overflow_error", [] { throw std::overflow_error("an overflow error"); }); | ||
m.def("raise_index_error", [] { throw nb::index_error("an index error"); }); | ||
m.def("raise_key_error", [] { throw nb::key_error("a key error"); }); | ||
m.def("raise_value_error", [] { throw nb::value_error("a value error"); }); | ||
m.def("raise_type_error", [] { throw nb::type_error("a type error"); }); | ||
m.def("raise_import_error", [] { throw nb::import_error("an import error"); }); | ||
m.def("raise_attribute_error", [] { throw nb::attribute_error("an attribute error"); }); | ||
m.def("raise_stop_iteration", [] { throw nb::stop_iteration("a stop iteration error"); }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import test_exception_ext as t | ||
import pytest | ||
|
||
def test01_base(): | ||
with pytest.raises(RuntimeError): | ||
assert t.raise_generic() | ||
|
||
def test02_bad_alloc(): | ||
with pytest.raises(MemoryError): | ||
assert t.raise_bad_alloc() | ||
|
||
def test03_runtime_error(): | ||
with pytest.raises(RuntimeError) as excinfo: | ||
assert t.raise_runtime_error() | ||
assert str(excinfo.value) == 'a runtime error' | ||
|
||
def test04_domain_error(): | ||
with pytest.raises(ValueError) as excinfo: | ||
assert t.raise_domain_error() | ||
assert str(excinfo.value) == 'a domain error' | ||
|
||
def test05_invalid_argument(): | ||
with pytest.raises(ValueError) as excinfo: | ||
assert t.raise_invalid_argument() | ||
assert str(excinfo.value) == 'an invalid argument error' | ||
|
||
def test06_length(): | ||
with pytest.raises(ValueError) as excinfo: | ||
assert t.raise_length_error() | ||
assert str(excinfo.value) == 'a length error' | ||
|
||
def test07_out_of_range(): | ||
with pytest.raises(IndexError) as excinfo: | ||
assert t.raise_out_of_range() | ||
assert str(excinfo.value) == 'an out of range error' | ||
|
||
def test08_range_error(): | ||
with pytest.raises(ValueError) as excinfo: | ||
assert t.raise_range_error() | ||
assert str(excinfo.value) == 'a range error' | ||
|
||
def test09_overflow_error(): | ||
with pytest.raises(OverflowError) as excinfo: | ||
assert t.raise_overflow_error() | ||
assert str(excinfo.value) == 'an overflow error' | ||
|
||
def test10_index_error(): | ||
with pytest.raises(IndexError) as excinfo: | ||
assert t.raise_index_error() | ||
assert str(excinfo.value) == 'an index error' | ||
|
||
def test11_key_error(): | ||
with pytest.raises(KeyError) as excinfo: | ||
assert t.raise_key_error() | ||
assert str(excinfo.value) == "'a key error'" | ||
|
||
def test12_value_error(): | ||
with pytest.raises(ValueError) as excinfo: | ||
assert t.raise_value_error() | ||
assert str(excinfo.value) == 'a value error' | ||
|
||
def test13_type_error(): | ||
with pytest.raises(TypeError) as excinfo: | ||
assert t.raise_type_error() | ||
assert str(excinfo.value) == 'a type error' | ||
|
||
def test14_import_error(): | ||
with pytest.raises(ImportError) as excinfo: | ||
assert t.raise_import_error() | ||
assert str(excinfo.value) == 'an import error' | ||
|
||
def test15_attribute_error(): | ||
with pytest.raises(AttributeError) as excinfo: | ||
assert t.raise_attribute_error() | ||
assert str(excinfo.value) == 'an attribute error' | ||
|
||
def test16_stop_iteration(): | ||
with pytest.raises(StopIteration) as excinfo: | ||
assert t.raise_stop_iteration() | ||
assert str(excinfo.value) == 'a stop iteration error' |