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
720136bf
Commit
720136bf
authored
Sep 10, 2016
by
Wenzel Jakob
Browse files
RAII wrapper for error state
parent
1f2e417d
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
13 additions
and
7 deletions
+13
-7
include/pybind11/cast.h
include/pybind11/cast.h
+5
-7
include/pybind11/common.h
include/pybind11/common.h
+7
-0
include/pybind11/pybind11.h
include/pybind11/pybind11.h
+1
-0
No files found.
include/pybind11/cast.h
View file @
720136bf
...
@@ -111,18 +111,16 @@ PYBIND11_NOINLINE inline std::string error_string() {
...
@@ -111,18 +111,16 @@ PYBIND11_NOINLINE inline std::string error_string() {
return
"Unknown internal error occurred"
;
return
"Unknown internal error occurred"
;
}
}
PyObject
*
type
,
*
value
,
*
traceback
;
error_scope
scope
;
// Preserve error state
PyErr_Fetch
(
&
type
,
&
value
,
&
traceback
);
std
::
string
errorString
;
std
::
string
errorString
;
if
(
type
)
{
if
(
scope
.
type
)
{
errorString
+=
handle
(
type
).
attr
(
"__name__"
).
cast
<
std
::
string
>
();
errorString
+=
handle
(
scope
.
type
).
attr
(
"__name__"
).
cast
<
std
::
string
>
();
errorString
+=
": "
;
errorString
+=
": "
;
}
}
if
(
value
)
if
(
scope
.
value
)
errorString
+=
(
std
::
string
)
handle
(
value
).
str
();
errorString
+=
(
std
::
string
)
handle
(
scope
.
value
).
str
();
PyErr_Restore
(
type
,
value
,
traceback
);
return
errorString
;
return
errorString
;
}
}
...
...
include/pybind11/common.h
View file @
720136bf
...
@@ -420,6 +420,13 @@ template <typename T> struct format_descriptor<T, typename std::enable_if<std::i
...
@@ -420,6 +420,13 @@ template <typename T> struct format_descriptor<T, typename std::enable_if<std::i
template
<
typename
T
>
constexpr
const
char
format_descriptor
<
template
<
typename
T
>
constexpr
const
char
format_descriptor
<
T
,
typename
std
::
enable_if
<
std
::
is_integral
<
T
>::
value
>::
type
>::
value
[
2
];
T
,
typename
std
::
enable_if
<
std
::
is_integral
<
T
>::
value
>::
type
>::
value
[
2
];
/// RAII wrapper that temporarily clears any Python error state
struct
error_scope
{
PyObject
*
type
,
*
value
,
*
trace
;
error_scope
()
{
PyErr_Fetch
(
&
type
,
&
value
,
&
trace
);
}
~
error_scope
()
{
PyErr_Restore
(
type
,
value
,
trace
);
}
};
PYBIND11_DECL_FMT
(
float
,
"f"
);
PYBIND11_DECL_FMT
(
float
,
"f"
);
PYBIND11_DECL_FMT
(
double
,
"d"
);
PYBIND11_DECL_FMT
(
double
,
"d"
);
PYBIND11_DECL_FMT
(
bool
,
"?"
);
PYBIND11_DECL_FMT
(
bool
,
"?"
);
...
...
include/pybind11/pybind11.h
View file @
720136bf
...
@@ -1309,6 +1309,7 @@ NAMESPACE_END(detail)
...
@@ -1309,6 +1309,7 @@ NAMESPACE_END(detail)
template
<
return_value_policy
policy
=
return_value_policy
::
automatic_reference
,
typename
...
Args
>
template
<
return_value_policy
policy
=
return_value_policy
::
automatic_reference
,
typename
...
Args
>
void
print
(
Args
&&
...
args
)
{
void
print
(
Args
&&
...
args
)
{
error_scope
scope
;
// Preserve error state
auto
c
=
detail
::
collect_arguments
<
policy
>
(
std
::
forward
<
Args
>
(
args
)...);
auto
c
=
detail
::
collect_arguments
<
policy
>
(
std
::
forward
<
Args
>
(
args
)...);
detail
::
print
(
c
.
args
(),
c
.
kwargs
());
detail
::
print
(
c
.
args
(),
c
.
kwargs
());
}
}
...
...
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