Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
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) {
...
@@ -1429,12 +1429,14 @@ void register_exception_translator(ExceptionTranslator&& translator) {
template
<
typename
type
>
template
<
typename
type
>
class
exception
:
public
object
{
class
exception
:
public
object
{
public:
public:
exception
(
module
&
m
,
const
std
::
string
&
name
,
PyObject
*
base
=
PyExc_Exception
)
{
exception
(
handle
scope
,
const
char
*
name
,
PyObject
*
base
=
PyExc_Exception
)
{
std
::
string
full_name
=
std
::
string
(
PyModule_GetName
(
m
.
ptr
()))
std
::
string
full_name
=
scope
.
attr
(
"__name__"
).
cast
<
std
::
string
>
()
+
+
std
::
string
(
"."
)
+
name
;
std
::
string
(
"."
)
+
name
;
char
*
exception_name
=
const_cast
<
char
*>
(
full_name
.
c_str
());
m_ptr
=
PyErr_NewException
((
char
*
)
full_name
.
c_str
(),
base
,
NULL
);
m_ptr
=
PyErr_NewException
(
exception_name
,
base
,
NULL
);
if
(
hasattr
(
scope
,
name
))
m
.
add_object
(
name
.
c_str
(),
*
this
);
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
// Sets the current python exception to this exception object with the given message
...
@@ -1448,14 +1450,16 @@ public:
...
@@ -1448,14 +1450,16 @@ public:
* This is intended for simple exception translations; for more complex translation, register the
* This is intended for simple exception translations; for more complex translation, register the
* exception object and translator directly.
* exception object and translator directly.
*/
*/
template
<
typename
CppException
>
exception
<
CppException
>&
register_exception
(
module
&
m
,
const
std
::
string
&
name
,
PyObject
*
base
=
PyExc_Exception
)
{
template
<
typename
CppException
>
static
exception
<
CppException
>
ex
(
m
,
name
,
base
);
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
)
{
register_exception_translator
([](
std
::
exception_ptr
p
)
{
if
(
!
p
)
return
;
if
(
!
p
)
return
;
try
{
try
{
std
::
rethrow_exception
(
p
);
std
::
rethrow_exception
(
p
);
}
}
catch
(
const
CppException
&
e
)
{
catch
(
const
CppException
&
e
)
{
ex
(
e
.
what
());
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