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
73992b54
Commit
73992b54
authored
Jun 07, 2022
by
Xiaofei Wang
Committed by
Copybara-Service
Jun 07, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Internal change
PiperOrigin-RevId: 453518316
parent
4b5def2c
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
33 additions
and
7 deletions
+33
-7
pybind11_abseil/absl_casters.h
+20
-7
pybind11_abseil/tests/absl_test.py
+13
-0
No files found.
pybind11_abseil/absl_casters.h
View file @
73992b54
...
@@ -115,15 +115,28 @@ struct type_caster<absl::Duration> {
...
@@ -115,15 +115,28 @@ struct type_caster<absl::Duration> {
// Conversion part 1 (Python->C++)
// Conversion part 1 (Python->C++)
bool
load
(
handle
src
,
bool
convert
)
{
bool
load
(
handle
src
,
bool
convert
)
{
// Ensure that absl::Duration is converted from a Python datetime.timedelta.
if
(
!
convert
)
{
if
(
!
convert
||
!
hasattr
(
src
,
"days"
)
||
!
hasattr
(
src
,
"seconds"
)
||
!
hasattr
(
src
,
"microseconds"
))
{
return
false
;
return
false
;
}
}
value
=
absl
::
Hours
(
24
*
GetInt64Attr
(
src
,
"days"
))
+
if
(
PyFloat_Check
(
src
.
ptr
()))
{
absl
::
Seconds
(
GetInt64Attr
(
src
,
"seconds"
))
+
value
=
absl
::
Seconds
(
src
.
cast
<
double
>
());
absl
::
Microseconds
(
GetInt64Attr
(
src
,
"microseconds"
));
return
true
;
return
true
;
}
else
if
(
PyLong_Check
(
src
.
ptr
()))
{
value
=
absl
::
Seconds
(
src
.
cast
<
int64_t
>
());
return
true
;
}
else
{
// Ensure that absl::Duration is converted from a Python
// datetime.timedelta.
if
(
!
hasattr
(
src
,
"days"
)
||
!
hasattr
(
src
,
"seconds"
)
||
!
hasattr
(
src
,
"microseconds"
))
{
return
false
;
}
value
=
absl
::
Hours
(
24
*
GetInt64Attr
(
src
,
"days"
))
+
absl
::
Seconds
(
GetInt64Attr
(
src
,
"seconds"
))
+
absl
::
Microseconds
(
GetInt64Attr
(
src
,
"microseconds"
));
return
true
;
}
return
false
;
}
}
// Conversion part 2 (C++ -> Python)
// Conversion part 2 (C++ -> Python)
...
...
pybind11_abseil/tests/absl_test.py
View file @
73992b54
...
@@ -46,6 +46,19 @@ class AbslTimeTest(parameterized.TestCase):
...
@@ -46,6 +46,19 @@ class AbslTimeTest(parameterized.TestCase):
duration
=
datetime
.
timedelta
(
seconds
=
self
.
NEGATIVE_SECS
)
duration
=
datetime
.
timedelta
(
seconds
=
self
.
NEGATIVE_SECS
)
self
.
assertTrue
(
absl_example
.
check_duration
(
duration
,
self
.
NEGATIVE_SECS
))
self
.
assertTrue
(
absl_example
.
check_duration
(
duration
,
self
.
NEGATIVE_SECS
))
def
test_pass_float_duration
(
self
):
self
.
assertTrue
(
absl_example
.
check_duration
(
self
.
POSITIVE_SECS
,
self
.
POSITIVE_SECS
))
def
test_pass_integer_duration
(
self
):
self
.
assertTrue
(
absl_example
.
check_duration
(
self
.
SECONDS_IN_DAY
,
self
.
SECONDS_IN_DAY
))
def
test_duration_integer_overflow
(
self
):
duration
=
2
**
129
with
self
.
assertRaises
(
RuntimeError
):
absl_example
.
check_duration
(
duration
,
duration
)
def
test_return_datetime
(
self
):
def
test_return_datetime
(
self
):
secs
=
self
.
TEST_DATETIME
.
timestamp
()
secs
=
self
.
TEST_DATETIME
.
timestamp
()
local_tz
=
tz
.
gettz
()
local_tz
=
tz
.
gettz
()
...
...
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