Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pybind11_abseil
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_abseil
Commits
35f5df96
Commit
35f5df96
authored
Feb 07, 2022
by
Ralf W. Grosse-Kunstleve
Committed by
Copybara-Service
Feb 07, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Using status_caster.h in register_status_bindings.cc (fixes ODR issue).
PiperOrigin-RevId: 426917066
parent
1bb411eb
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
24 deletions
+38
-24
pybind11_abseil/BUILD
+2
-0
pybind11_abseil/register_status_bindings.cc
+36
-24
No files found.
pybind11_abseil/BUILD
View file @
35f5df96
...
@@ -73,6 +73,8 @@ pybind_library(
...
@@ -73,6 +73,8 @@ pybind_library(
visibility = ["//visibility:private"],
visibility = ["//visibility:private"],
deps = [
deps = [
":absl_casters",
":absl_casters",
":no_throw_status",
":status_caster",
":status_not_ok_exception",
":status_not_ok_exception",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings",
...
...
pybind11_abseil/register_status_bindings.cc
View file @
35f5df96
...
@@ -3,12 +3,15 @@
...
@@ -3,12 +3,15 @@
#include <pybind11/pybind11.h>
#include <pybind11/pybind11.h>
#include <exception>
#include <exception>
#include <functional>
#include <string>
#include <string>
#include <utility>
#include <utility>
#include "absl/status/status.h"
#include "absl/status/status.h"
#include "absl/strings/string_view.h"
#include "absl/strings/string_view.h"
#include "pybind11_abseil/absl_casters.h"
#include "pybind11_abseil/absl_casters.h"
#include "pybind11_abseil/no_throw_status.h"
#include "pybind11_abseil/status_caster.h"
#include "pybind11_abseil/status_not_ok_exception.h"
#include "pybind11_abseil/status_not_ok_exception.h"
namespace
pybind11
{
namespace
pybind11
{
...
@@ -79,6 +82,17 @@ absl::Status WrapUnknownError(absl::string_view message) {
...
@@ -79,6 +82,17 @@ absl::Status WrapUnknownError(absl::string_view message) {
return
absl
::
UnknownError
(
message
);
return
absl
::
UnknownError
(
message
);
}
}
void
def_status_factory
(
module
&
m
,
const
char
*
name
,
absl
::
Status
(
*
absl_status_factory
)(
absl
::
string_view
message
))
{
m
.
def
(
name
,
[
absl_status_factory
](
absl
::
string_view
message
)
{
return
DoNotThrowStatus
(
absl_status_factory
(
message
));
},
arg
(
"message"
));
}
// Allows exception to raise an instance, with custom parameters and attributes.
// Allows exception to raise an instance, with custom parameters and attributes.
template
<
typename
type
>
template
<
typename
type
>
class
exception_with_attributes
:
public
exception
<
type
>
{
class
exception_with_attributes
:
public
exception
<
type
>
{
...
@@ -139,28 +153,24 @@ void RegisterStatusBindings(module m) {
...
@@ -139,28 +153,24 @@ void RegisterStatusBindings(module m) {
"used in this case because an ok status is never returned; instead, a "
"used in this case because an ok status is never returned; instead, a "
"non-status object is returned, which doesn't have a .ok() method."
);
"non-status object is returned, which doesn't have a .ok() method."
);
// status_casters.h has not been included, so the functions below will
// return a wrapped status, not raise an exception.
// Return canonical errors.
// Return canonical errors.
m
.
def
(
"aborted_error"
,
&
WrapAbortedError
,
arg
(
"message"
));
def_status_factory
(
m
,
"aborted_error"
,
WrapAbortedError
);
m
.
def
(
"already_exists_error"
,
&
WrapAlreadyExistsError
,
arg
(
"message"
));
def_status_factory
(
m
,
"already_exists_error"
,
WrapAlreadyExistsError
);
m
.
def
(
"cancelled_error"
,
&
WrapCancelledError
,
arg
(
"message"
));
def_status_factory
(
m
,
"cancelled_error"
,
WrapCancelledError
);
m
.
def
(
"data_loss_error"
,
&
WrapDataLossError
,
arg
(
"message"
));
def_status_factory
(
m
,
"data_loss_error"
,
WrapDataLossError
);
m
.
def
(
"deadline_exceeded_error"
,
&
WrapDeadlineExceededError
,
arg
(
"message"
));
def_status_factory
(
m
,
"deadline_exceeded_error"
,
WrapDeadlineExceededError
);
m
.
def
(
"failed_precondition_error"
,
&
WrapFailedPreconditionError
,
def_status_factory
(
m
,
"failed_precondition_error"
,
arg
(
"message"
));
WrapFailedPreconditionError
);
m
.
def
(
"internal_error"
,
&
WrapInternalError
,
arg
(
"message"
));
def_status_factory
(
m
,
"internal_error"
,
WrapInternalError
);
m
.
def
(
"invalid_argument_error"
,
&
WrapInvalidArgumentError
,
arg
(
"message"
));
def_status_factory
(
m
,
"invalid_argument_error"
,
WrapInvalidArgumentError
);
m
.
def
(
"not_found_error"
,
&
WrapNotFoundError
,
arg
(
"message"
));
def_status_factory
(
m
,
"not_found_error"
,
WrapNotFoundError
);
m
.
def
(
"out_of_range_error"
,
&
WrapOutOfRangeError
,
arg
(
"message"
));
def_status_factory
(
m
,
"out_of_range_error"
,
WrapOutOfRangeError
);
m
.
def
(
"permission_denied_error"
,
&
WrapPermissionDeniedError
,
arg
(
"message"
));
def_status_factory
(
m
,
"permission_denied_error"
,
WrapPermissionDeniedError
);
m
.
def
(
"resource_exhausted_error"
,
&
WrapResourceExhaustedError
,
def_status_factory
(
m
,
"resource_exhausted_error"
,
WrapResourceExhaustedError
);
arg
(
"message"
));
def_status_factory
(
m
,
"unauthenticated_error"
,
WrapUnauthenticatedError
);
m
.
def
(
"unauthenticated_error"
,
&
WrapUnauthenticatedError
,
arg
(
"message"
));
def_status_factory
(
m
,
"unavailable_error"
,
WrapUnavailableError
);
m
.
def
(
"unavailable_error"
,
&
WrapUnavailableError
,
arg
(
"message"
));
def_status_factory
(
m
,
"unimplemented_error"
,
WrapUnimplementedError
);
m
.
def
(
"unimplemented_error"
,
&
WrapUnimplementedError
,
arg
(
"message"
));
def_status_factory
(
m
,
"unknown_error"
,
WrapUnknownError
);
m
.
def
(
"unknown_error"
,
&
WrapUnknownError
,
arg
(
"message"
));
// Register the exception.
// Register the exception.
static
exception_with_attributes
<
StatusNotOk
>
status_not_ok
(
m
,
"StatusNotOk"
);
static
exception_with_attributes
<
StatusNotOk
>
status_not_ok
(
m
,
"StatusNotOk"
);
...
@@ -172,10 +182,12 @@ void RegisterStatusBindings(module m) {
...
@@ -172,10 +182,12 @@ void RegisterStatusBindings(module m) {
if
(
p
)
std
::
rethrow_exception
(
p
);
if
(
p
)
std
::
rethrow_exception
(
p
);
}
catch
(
StatusNotOk
&
e
)
{
}
catch
(
StatusNotOk
&
e
)
{
auto
rvalue_e
=
std
::
move
(
e
);
auto
rvalue_e
=
std
::
move
(
e
);
auto
py_status
=
cast
(
google
::
NoThrowStatus
<
const
absl
::
Status
&>
(
rvalue_e
.
status
()));
status_not_ok
(
status_not_ok
(
pybind11
::
make_tuple
(
rvalue_e
.
what
()),
// args
pybind11
::
make_tuple
(
rvalue_e
.
what
()),
// args
pybind11
::
dict
(),
// kwargs
pybind11
::
dict
(),
// kwargs
pybind11
::
dict
(
arg
(
"status"
)
=
rvalue_e
.
status
()
));
// attributes
pybind11
::
dict
(
arg
(
"status"
)
=
py_status
));
// attributes
}
}
});
});
}
}
...
...
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