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
281ccc69
Commit
281ccc69
authored
Nov 16, 2016
by
Wenzel Jakob
Browse files
exception constructor consistency improvements (fixes #492)
parent
5027c4f9
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
14 additions
and
10 deletions
+14
-10
include/pybind11/pybind11.h
include/pybind11/pybind11.h
+14
-10
No files found.
include/pybind11/pybind11.h
View file @
281ccc69
...
...
@@ -1429,12 +1429,14 @@ void register_exception_translator(ExceptionTranslator&& translator) {
template
<
typename
type
>
class
exception
:
public
object
{
public:
exception
(
module
&
m
,
const
std
::
string
&
name
,
PyObject
*
base
=
PyExc_Exception
)
{
std
::
string
full_name
=
std
::
string
(
PyModule_GetName
(
m
.
ptr
()))
+
std
::
string
(
"."
)
+
name
;
char
*
exception_name
=
const_cast
<
char
*>
(
full_name
.
c_str
());
m_ptr
=
PyErr_NewException
(
exception_name
,
base
,
NULL
);
m
.
add_object
(
name
.
c_str
(),
*
this
);
exception
(
handle
scope
,
const
char
*
name
,
PyObject
*
base
=
PyExc_Exception
)
{
std
::
string
full_name
=
scope
.
attr
(
"__name__"
).
cast
<
std
::
string
>
()
+
std
::
string
(
"."
)
+
name
;
m_ptr
=
PyErr_NewException
((
char
*
)
full_name
.
c_str
(),
base
,
NULL
);
if
(
hasattr
(
scope
,
name
))
pybind11_fail
(
"Error during initialization: multiple incompatible "
"definitions with name
\"
"
+
std
::
string
(
name
)
+
"
\"
"
);
scope
.
attr
(
name
)
=
*
this
;
}
// Sets the current python exception to this exception object with the given message
...
...
@@ -1448,14 +1450,16 @@ public:
* This is intended for simple exception translations; for more complex translation, register the
* exception object and translator directly.
*/
template
<
typename
CppException
>
exception
<
CppException
>&
register_exception
(
module
&
m
,
const
std
::
string
&
name
,
PyObject
*
base
=
PyExc_Exception
)
{
static
exception
<
CppException
>
ex
(
m
,
name
,
base
);
template
<
typename
CppException
>
exception
<
CppException
>
&
register_exception
(
handle
scope
,
const
char
*
name
,
PyObject
*
base
=
PyExc_Exception
)
{
static
exception
<
CppException
>
ex
(
scope
,
name
,
base
);
register_exception_translator
([](
std
::
exception_ptr
p
)
{
if
(
!
p
)
return
;
try
{
std
::
rethrow_exception
(
p
);
}
catch
(
const
CppException
&
e
)
{
}
catch
(
const
CppException
&
e
)
{
ex
(
e
.
what
());
}
});
...
...
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