Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pybind11
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open
pybind11
Commits
587aa328
Commit
587aa328
authored
Sep 08, 2016
by
Wenzel Jakob
Committed by
GitHub
Sep 08, 2016
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #395 from aldanor/feature/error-already-set-message
error_already_set improvements
parents
39577e8c
67b54894
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
37 additions
and
2 deletions
+37
-2
include/pybind11/cast.h
+6
-1
tests/test_exceptions.cpp
+19
-1
tests/test_exceptions.py
+12
-0
No files found.
include/pybind11/cast.h
View file @
587aa328
...
@@ -106,12 +106,17 @@ PYBIND11_NOINLINE inline handle get_type_handle(const std::type_info &tp) {
...
@@ -106,12 +106,17 @@ PYBIND11_NOINLINE inline handle get_type_handle(const std::type_info &tp) {
}
}
PYBIND11_NOINLINE
inline
std
::
string
error_string
()
{
PYBIND11_NOINLINE
inline
std
::
string
error_string
()
{
if
(
!
PyErr_Occurred
())
{
PyErr_SetString
(
PyExc_RuntimeError
,
"Unknown internal error occurred"
);
return
"Unknown internal error occurred"
;
}
PyObject
*
type
,
*
value
,
*
traceback
;
PyObject
*
type
,
*
value
,
*
traceback
;
PyErr_Fetch
(
&
type
,
&
value
,
&
traceback
);
PyErr_Fetch
(
&
type
,
&
value
,
&
traceback
);
std
::
string
errorString
;
std
::
string
errorString
;
if
(
type
)
{
if
(
type
)
{
errorString
+=
(
std
::
string
)
handle
(
type
).
str
();
errorString
+=
handle
(
type
).
attr
(
"__name__"
).
cast
<
std
::
string
>
();
errorString
+=
": "
;
errorString
+=
": "
;
}
}
if
(
value
)
if
(
value
)
...
...
tests/test_exceptions.cpp
View file @
587aa328
...
@@ -104,5 +104,23 @@ test_initializer custom_exceptions([](py::module &m) {
...
@@ -104,5 +104,23 @@ test_initializer custom_exceptions([](py::module &m) {
m
.
def
(
"throws3"
,
&
throws3
);
m
.
def
(
"throws3"
,
&
throws3
);
m
.
def
(
"throws4"
,
&
throws4
);
m
.
def
(
"throws4"
,
&
throws4
);
m
.
def
(
"throws_logic_error"
,
&
throws_logic_error
);
m
.
def
(
"throws_logic_error"
,
&
throws_logic_error
);
});
m
.
def
(
"throw_already_set"
,
[](
bool
err
)
{
if
(
err
)
PyErr_SetString
(
PyExc_ValueError
,
"foo"
);
try
{
throw
py
::
error_already_set
();
}
catch
(
const
std
::
runtime_error
&
e
)
{
if
((
err
&&
e
.
what
()
!=
std
::
string
(
"ValueError: foo"
))
||
(
!
err
&&
e
.
what
()
!=
std
::
string
(
"Unknown internal error occurred"
)))
{
PyErr_Clear
();
throw
std
::
runtime_error
(
"error message mismatch"
);
}
}
PyErr_Clear
();
if
(
err
)
PyErr_SetString
(
PyExc_ValueError
,
"foo"
);
throw
py
::
error_already_set
();
});
});
tests/test_exceptions.py
View file @
587aa328
import
pytest
import
pytest
def
test_error_already_set
(
msg
):
from
pybind11_tests
import
throw_already_set
with
pytest
.
raises
(
RuntimeError
)
as
excinfo
:
throw_already_set
(
False
)
assert
msg
(
excinfo
.
value
)
==
"Unknown internal error occurred"
with
pytest
.
raises
(
ValueError
)
as
excinfo
:
throw_already_set
(
True
)
assert
msg
(
excinfo
.
value
)
==
"foo"
def
test_custom
(
msg
):
def
test_custom
(
msg
):
from
pybind11_tests
import
(
MyException
,
throws1
,
throws2
,
throws3
,
throws4
,
from
pybind11_tests
import
(
MyException
,
throws1
,
throws2
,
throws3
,
throws4
,
throws_logic_error
)
throws_logic_error
)
...
...
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