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
0c34237f
Commit
0c34237f
authored
Apr 04, 2022
by
Xiaofei Wang
Committed by
Copybara-Service
Apr 04, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Internal change
PiperOrigin-RevId: 439435815
parent
5f2e3baf
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
40 additions
and
0 deletions
+40
-0
pybind11_abseil/absl_casters.h
+26
-0
pybind11_abseil/tests/absl_example.cc
+5
-0
pybind11_abseil/tests/absl_test.py
+9
-0
No files found.
pybind11_abseil/absl_casters.h
View file @
0c34237f
...
...
@@ -13,6 +13,7 @@
// - absl::Duration- converted to/from python datetime.timedelta
// - absl::CivilTime- converted to/from python datetime.datetime and from date.
// - absl::Time- converted to/from python datetime.datetime and from date.
// - absl::TimeZone- converted to/from python str and from int.
// - absl::Span- converted to python sequences and from python buffers,
// opaque std::vectors and/or sequences.
// - absl::string_view
...
...
@@ -79,6 +80,31 @@ inline absl::TimeZone GetTimeZone(handle src) {
return
absl
::
FixedTimeZone
(
offset_seconds
);
}
template
<>
struct
type_caster
<
absl
::
TimeZone
>
{
public
:
PYBIND11_TYPE_CASTER
(
absl
::
TimeZone
,
_
<
absl
::
TimeZone
>
());
// Conversion part 1 (Python->C++)
bool
load
(
handle
src
,
bool
convert
)
{
if
(
PyUnicode_Check
(
src
.
ptr
()))
{
if
(
LoadTimeZone
(
PyUnicode_AsUTF8
(
src
.
ptr
()),
&
value
))
{
return
true
;
}
}
else
if
(
PyLong_Check
(
src
.
ptr
()))
{
value
=
absl
::
FixedTimeZone
(
PyLong_AsLong
(
src
.
ptr
()));
return
true
;
}
return
false
;
}
// Conversion part 2 (C++ -> Python)
static
handle
cast
(
const
absl
::
TimeZone
&
src
,
return_value_policy
,
handle
)
{
// Converts to Python str
return
PyUnicode_FromStringAndSize
(
src
.
name
().
data
(),
src
.
name
().
size
());
}
};
// Convert between absl::Duration and python datetime.timedelta.
template
<>
struct
type_caster
<
absl
::
Duration
>
{
...
...
pybind11_abseil/tests/absl_example.cc
View file @
0c34237f
...
...
@@ -92,6 +92,8 @@ bool CheckCivilYear(absl::CivilYear datetime, double secs) {
return
datetime
==
MakeCivilYear
(
secs
);
}
absl
::
TimeZone
RoundtripTimeZone
(
absl
::
TimeZone
timezone
)
{
return
timezone
;
}
// Since a span does not own its elements, we must create a class to own them
// and persist beyond the function that constructs the span for testing.
class
VectorContainer
{
...
...
@@ -294,6 +296,9 @@ PYBIND11_MODULE(absl_example, m) {
m
.
def
(
"make_datetime"
,
&
MakeTime
,
arg
(
"secs"
));
m
.
def
(
"check_datetime"
,
&
CheckDatetime
,
arg
(
"datetime"
),
arg
(
"secs"
));
// absl::TimeZone bindings
m
.
def
(
"roundtrip_timezone"
,
&
RoundtripTimeZone
,
arg
(
"timezone"
));
// absl::CivilTime bindings
m
.
def
(
"make_civilsecond"
,
&
MakeCivilSecond
,
arg
(
"secs"
));
m
.
def
(
"check_civilsecond"
,
&
CheckCivilSecond
,
arg
(
"datetime"
),
arg
(
"secs"
));
...
...
pybind11_abseil/tests/absl_test.py
View file @
0c34237f
...
...
@@ -191,6 +191,15 @@ class AbslTimeTest(parameterized.TestCase):
self
.
assertTrue
(
absl_example
.
check_civilyear
(
self
.
TEST_DATETIME
,
truncated
.
timestamp
()))
def
test_timezone
(
self
):
expected_timezone
=
'Fixed/UTC+02:00:00'
timezone
=
absl_example
.
roundtrip_timezone
(
expected_timezone
)
self
.
assertEqual
(
expected_timezone
,
timezone
)
timezone
=
absl_example
.
roundtrip_timezone
(
2
*
60
*
60
)
self
.
assertEqual
(
expected_timezone
,
timezone
)
with
self
.
assertRaises
(
TypeError
):
absl_example
.
roundtrip_timezone
(
'Not a timezone'
)
def
make_read_only_numpy_array
():
values
=
np
.
zeros
(
5
,
dtype
=
np
.
int32
)
...
...
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