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
38b42686
Commit
38b42686
authored
Feb 09, 2023
by
pybind11_abseil authors
Committed by
Copybara-Service
Feb 09, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
internal change.
PiperOrigin-RevId: 508543167
parent
87c41a4e
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
103 additions
and
3 deletions
+103
-3
pybind11_abseil/display_source_location_in_python.cc
+48
-0
pybind11_abseil/display_source_location_in_python.h
+49
-0
pybind11_abseil/register_status_bindings.cc
+4
-2
pybind11_abseil/tests/status_example_test.py
+2
-1
No files found.
pybind11_abseil/display_source_location_in_python.cc
0 → 100644
View file @
38b42686
#include "absl/status/status.h"
#include "absl/strings/string_view.h"
#include "util/task/status_builder.h"
namespace
pybind11
{
namespace
google
{
namespace
{
constexpr
absl
::
string_view
kDisplay
=
"1"
;
constexpr
absl
::
string_view
kDoNotDisplay
=
"0"
;
constexpr
absl
::
string_view
kDisplaySourceLocationInPython
=
"pybind11_abseil_display_source_location"
;
}
// namespace
bool
HasDisplaySourceLocationInPython
(
absl
::
Status
s
)
{
auto
optional_payload
=
s
.
GetPayload
(
kDisplaySourceLocationInPython
);
return
optional_payload
.
has_value
()
&&
optional_payload
.
value
()
==
kDisplay
;
}
bool
HasDoNotDisplaySourceLocationInPython
(
absl
::
Status
s
)
{
auto
optional_payload
=
s
.
GetPayload
(
kDisplaySourceLocationInPython
);
return
optional_payload
.
has_value
()
&&
optional_payload
.
value
()
==
kDoNotDisplay
;
}
absl
::
Status
DisplaySourceLocationInPython
(
absl
::
Status
s
)
{
s
.
SetPayload
(
kDisplaySourceLocationInPython
,
absl
::
Cord
(
kDisplay
));
return
s
;
}
absl
::
Status
DoNotDisplaySourceLocationInPython
(
absl
::
Status
s
)
{
s
.
SetPayload
(
kDisplaySourceLocationInPython
,
absl
::
Cord
(
kDoNotDisplay
));
return
s
;
}
util
::
StatusBuilder
DisplaySourceLocationInPython
(
util
::
StatusBuilder
sb
)
{
return
sb
.
SetPayload
(
kDisplaySourceLocationInPython
,
absl
::
Cord
(
kDisplay
));
}
util
::
StatusBuilder
DoNotDisplaySourceLocationInPython
(
util
::
StatusBuilder
sb
)
{
return
sb
.
SetPayload
(
kDisplaySourceLocationInPython
,
absl
::
Cord
(
kDoNotDisplay
));
}
}
// namespace google
}
// namespace pybind11
pybind11_abseil/display_source_location_in_python.h
0 → 100644
View file @
38b42686
#ifndef PYBIND11_ABSEIL_DISPLAY_SOURCE_LOCATION_IN_PYTHON_H_
#define PYBIND11_ABSEIL_DISPLAY_SOURCE_LOCATION_IN_PYTHON_H_
// This file exists to simplify adding C++ source location to python StatusNotOk
// traces. See: b/266066084 for details.
#include "absl/status/status.h"
#include "util/task/status_builder.h"
namespace
pybind11
{
namespace
google
{
// Return true if the status was set with DisplaySourceLocationInPython.
bool
HasDisplaySourceLocationInPython
(
absl
::
Status
s
);
bool
HasDoNotDisplaySourceLocationInPython
(
absl
::
Status
s
);
// Annotate the Status to display the c++ SourceLocation in python.
absl
::
Status
DisplaySourceLocationInPython
(
absl
::
Status
s
);
absl
::
Status
DoNotDisplaySourceLocationInPython
(
absl
::
Status
s
);
// Annotate the StatusBuilder to display the c++ SourceLocation in python.
util
::
StatusBuilder
DisplaySourceLocationInPython
(
util
::
StatusBuilder
sb
);
util
::
StatusBuilder
DoNotDisplaySourceLocationInPython
(
util
::
StatusBuilder
sb
);
// Annotate the StatusOr<T> to display the c++ SourceLocation in python.
template
<
typename
StatusOrT
>
StatusOrT
DisplaySourceLocationInPython
(
StatusOrT
&&
s_or_t
)
{
if
(
s_or_t
.
ok
())
{
return
s_or_t
;
}
absl
::
Status
status_with_payload
=
DisplaySourceLocationInPython
(
s_or_t
.
status
());
return
StatusOrT
{
status_with_payload
};
}
template
<
typename
StatusOrT
>
StatusOrT
DoNotDisplaySourceLocationInPython
(
StatusOrT
&&
s_or_t
)
{
if
(
s_or_t
.
ok
())
{
return
s_or_t
;
}
absl
::
Status
status_with_payload
=
DoNotDisplaySourceLocationInPython
(
s_or_t
.
status
());
return
StatusOrT
{
status_with_payload
};
}
}
// namespace google
}
// namespace pybind11
#endif // PYBIND11_ABSEIL_DISPLAY_SOURCE_LOCATION_IN_PYTHON_H_
pybind11_abseil/register_status_bindings.cc
View file @
38b42686
...
@@ -264,9 +264,11 @@ void RegisterStatusBindings(module m) {
...
@@ -264,9 +264,11 @@ void RegisterStatusBindings(module m) {
(
void
(
absl
::
Status
::*
)(
const
absl
::
Status
&
))
&
absl
::
Status
::
Update
,
(
void
(
absl
::
Status
::*
)(
const
absl
::
Status
&
))
&
absl
::
Status
::
Update
,
arg
(
"other"
))
arg
(
"other"
))
.
def
(
"to_string"
,
.
def
(
"to_string"
,
[](
const
absl
::
Status
&
s
)
{
[](
const
absl
::
Status
&
s
,
bool
with_source_location
)
{
if
(
with_source_location
)
{
}
return
decode_utf8_replace
(
s
.
ToString
());
return
decode_utf8_replace
(
s
.
ToString
());
})
}
,
kw_only
{},
arg
(
"with_source_location"
)
=
false
)
.
def
(
"status_not_ok_str"
,
.
def
(
"status_not_ok_str"
,
[](
const
absl
::
Status
&
s
)
{
[](
const
absl
::
Status
&
s
)
{
std
::
string
code_str
=
absl
::
StatusCodeToString
(
s
.
code
());
std
::
string
code_str
=
absl
::
StatusCodeToString
(
s
.
code
());
...
...
pybind11_abseil/tests/status_example_test.py
View file @
38b42686
...
@@ -302,7 +302,8 @@ class StatusOrTest(absltest.TestCase):
...
@@ -302,7 +302,8 @@ class StatusOrTest(absltest.TestCase):
with
self
.
assertRaises
(
ValueError
):
with
self
.
assertRaises
(
ValueError
):
int_getter
.
Get
(
100
)
int_getter
.
Get
(
100
)
self
.
assertEqual
(
self
.
assertEqual
(
status_example
.
call_get_redirect_to_python
(
int_getter
,
5
),
5
)
status_example
.
call_get_redirect_to_python
(
int_getter
,
5
),
5
)
with
self
.
assertRaises
(
ValueError
):
with
self
.
assertRaises
(
ValueError
):
status_example
.
call_get_redirect_to_python
(
int_getter
,
100
)
status_example
.
call_get_redirect_to_python
(
int_getter
,
100
)
...
...
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