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
9f13dde9
Commit
9f13dde9
authored
Nov 08, 2022
by
Ralf W. Grosse-Kunstleve
Committed by
Copybara-Service
Nov 08, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Internal change: Raise a derived `StatusNotOk` type if the Python import is available.
PiperOrigin-RevId: 487063854
parent
c4baef68
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
41 additions
and
9 deletions
+41
-9
pybind11_abseil/register_status_bindings.cc
+41
-9
No files found.
pybind11_abseil/register_status_bindings.cc
View file @
9f13dde9
...
@@ -113,11 +113,46 @@ std::size_t boost_hash_combine(std::size_t lhs, std::size_t rhs) {
...
@@ -113,11 +113,46 @@ std::size_t boost_hash_combine(std::size_t lhs, std::size_t rhs) {
return
lhs
;
return
lhs
;
}
}
handle
ThisModule
(
handle
m
=
nullptr
)
{
static
handle
this_module
=
nullptr
;
if
(
m
)
{
this_module
=
m
;
}
return
this_module
;
}
handle
PyStatusNotOkTypeInUse
()
{
static
handle
type_in_use
=
nullptr
;
if
(
type_in_use
)
{
return
type_in_use
;
}
object
module_in_use
;
// Import any module with a derived or alternative StatusNotOk type here
// and assign to module_in_use.
if
(
!
module_in_use
)
{
module_in_use
=
reinterpret_borrow
<
object
>
(
ThisModule
());
if
(
!
module_in_use
)
{
throw
std
::
runtime_error
(
"Internal error: ThisModule() undefined ("
__FILE__
":"
+
std
::
to_string
(
__LINE__
)
+
")"
);
}
}
// Intentionally leak this Python reference:
// https://google.github.io/styleguide/cppguide.html#Static_and_Global_Variables
type_in_use
=
object
(
module_in_use
.
attr
(
"StatusNotOk"
)).
release
();
return
type_in_use
;
}
}
// namespace
}
// namespace
namespace
internal
{
namespace
internal
{
void
RegisterStatusBindings
(
module
m
)
{
void
RegisterStatusBindings
(
module
m
)
{
ThisModule
(
m
);
enum_
<
InitFromTag
>
(
m
,
"InitFromTag"
)
enum_
<
InitFromTag
>
(
m
,
"InitFromTag"
)
.
value
(
"capsule"
,
InitFromTag
::
capsule
)
.
value
(
"capsule"
,
InitFromTag
::
capsule
)
.
value
(
"capsule_direct_only"
,
InitFromTag
::
capsule_direct_only
)
.
value
(
"capsule_direct_only"
,
InitFromTag
::
capsule_direct_only
)
...
@@ -389,25 +424,22 @@ void RegisterStatusBindings(module m) {
...
@@ -389,25 +424,22 @@ void RegisterStatusBindings(module m) {
)"
,
)"
,
m
.
attr
(
"__dict__"
),
m
.
attr
(
"__dict__"
));
m
.
attr
(
"__dict__"
),
m
.
attr
(
"__dict__"
));
// Intentionally leak this Python reference:
// https://google.github.io/styleguide/cppguide.html#Static_and_Global_Variables
static
handle
PyStatusNotOk
=
object
(
m
.
attr
(
"StatusNotOk"
)).
release
();
// Register a custom handler which converts a C++ StatusNotOk to a
// Register a custom handler which converts a C++ StatusNotOk to a
// PyStatusNotOk.
// PyStatusNotOk.
register_exception_translator
([](
std
::
exception_ptr
p
)
{
register_exception_translator
([](
std
::
exception_ptr
p
)
{
try
{
try
{
if
(
p
)
std
::
rethrow_exception
(
p
);
if
(
p
)
std
::
rethrow_exception
(
p
);
}
catch
(
const
StatusNotOk
&
e
)
{
}
catch
(
const
StatusNotOk
&
e
)
{
PyErr_SetObject
(
PyErr_SetObject
(
PyStatusNotOkTypeInUse
().
ptr
(),
PyStatusNotOk
.
ptr
(),
PyStatusNotOkTypeInUse
()(
PyStatusNotOk
(
google
::
NoThrowStatus
<
absl
::
Status
>
(
e
.
status
())).
ptr
());
google
::
NoThrowStatus
<
absl
::
Status
>
(
e
.
status
()))
.
ptr
());
}
}
});
});
m
.
def
(
"BuildStatusNotOk"
,
[](
absl
::
StatusCode
code
,
const
std
::
string
&
msg
)
{
m
.
def
(
"BuildStatusNotOk"
,
[](
absl
::
StatusCode
code
,
const
std
::
string
&
msg
)
{
return
PyStatusNotOk
(
google
::
NoThrowStatus
<
absl
::
Status
>
(
return
PyStatusNotOk
TypeInUse
()
(
absl
::
Status
(
code
,
msg
)));
google
::
NoThrowStatus
<
absl
::
Status
>
(
absl
::
Status
(
code
,
msg
)));
});
});
}
}
...
...
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