Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
pybind11
Commits
deb3cb23
Commit
deb3cb23
authored
Nov 14, 2019
by
Francesco Biscani
Committed by
Wenzel Jakob
Nov 14, 2019
Browse files
Add exception translation for std::overflow_error. (#1977)
parent
55ff4642
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
8 additions
and
0 deletions
+8
-0
docs/advanced/exceptions.rst
docs/advanced/exceptions.rst
+2
-0
include/pybind11/detail/internals.h
include/pybind11/detail/internals.h
+1
-0
tests/test_exceptions.cpp
tests/test_exceptions.cpp
+1
-0
tests/test_exceptions.py
tests/test_exceptions.py
+4
-0
No files found.
docs/advanced/exceptions.rst
View file @
deb3cb23
...
...
@@ -28,6 +28,8 @@ exceptions:
+--------------------------------------+--------------------------------------+
| :class:`std::range_error` | ``ValueError`` |
+--------------------------------------+--------------------------------------+
| :class:`std::overflow_error` | ``OverflowError`` |
+--------------------------------------+--------------------------------------+
| :class:`pybind11::stop_iteration` | ``StopIteration`` (used to implement |
| | custom iterators) |
+--------------------------------------+--------------------------------------+
...
...
include/pybind11/detail/internals.h
View file @
deb3cb23
...
...
@@ -211,6 +211,7 @@ inline void translate_exception(std::exception_ptr p) {
}
catch
(
const
std
::
length_error
&
e
)
{
PyErr_SetString
(
PyExc_ValueError
,
e
.
what
());
return
;
}
catch
(
const
std
::
out_of_range
&
e
)
{
PyErr_SetString
(
PyExc_IndexError
,
e
.
what
());
return
;
}
catch
(
const
std
::
range_error
&
e
)
{
PyErr_SetString
(
PyExc_ValueError
,
e
.
what
());
return
;
}
catch
(
const
std
::
overflow_error
&
e
)
{
PyErr_SetString
(
PyExc_OverflowError
,
e
.
what
());
return
;
}
catch
(
const
std
::
exception
&
e
)
{
PyErr_SetString
(
PyExc_RuntimeError
,
e
.
what
());
return
;
}
catch
(...)
{
PyErr_SetString
(
PyExc_RuntimeError
,
"Caught an unknown exception!"
);
...
...
tests/test_exceptions.cpp
View file @
deb3cb23
...
...
@@ -116,6 +116,7 @@ TEST_SUBMODULE(exceptions, m) {
m
.
def
(
"throws5"
,
[]()
{
throw
MyException5
(
"this is a helper-defined translated exception"
);
});
m
.
def
(
"throws5_1"
,
[]()
{
throw
MyException5_1
(
"MyException5 subclass"
);
});
m
.
def
(
"throws_logic_error"
,
[]()
{
throw
std
::
logic_error
(
"this error should fall through to the standard handler"
);
});
m
.
def
(
"throws_overflow_error"
,
[]()
{
throw
std
::
overflow_error
(
""
);
});
m
.
def
(
"exception_matches"
,
[]()
{
py
::
dict
foo
;
try
{
...
...
tests/test_exceptions.py
View file @
deb3cb23
...
...
@@ -79,6 +79,10 @@ def test_custom(msg):
m
.
throws_logic_error
()
assert
msg
(
excinfo
.
value
)
==
"this error should fall through to the standard handler"
# OverFlow error translation.
with
pytest
.
raises
(
OverflowError
)
as
excinfo
:
m
.
throws_overflow_error
()
# Can we handle a helper-declared exception?
with
pytest
.
raises
(
m
.
MyException5
)
as
excinfo
:
m
.
throws5
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment