Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pybind11
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
Commits
c14170a7
Unverified
Commit
c14170a7
authored
Feb 15, 2022
by
Ralf W. Grosse-Kunstleve
Committed by
GitHub
Feb 15, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Removing `// clang-format off` - `on` directives from test_pickling.cpp (#3738)
parent
0986af61
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
12 additions
and
15 deletions
+12
-15
tests/test_pickling.cpp
+12
-15
No files found.
tests/test_pickling.cpp
View file @
c14170a7
// clang-format off
/*
tests/test_pickling.cpp -- pickle support
...
...
@@ -11,8 +10,6 @@
#include "pybind11_tests.h"
// clang-format on
#include <memory>
#include <stdexcept>
#include <utility>
...
...
@@ -63,19 +60,18 @@ void wrap(py::module m) {
}
// namespace exercise_trampoline
// clang-format off
TEST_SUBMODULE
(
pickling
,
m
)
{
// test_roundtrip
class
Pickleable
{
public
:
explicit
Pickleable
(
const
std
::
string
&
value
)
:
m_value
(
value
)
{
}
explicit
Pickleable
(
const
std
::
string
&
value
)
:
m_value
(
value
)
{}
const
std
::
string
&
value
()
const
{
return
m_value
;
}
void
setExtra1
(
int
extra1
)
{
m_extra1
=
extra1
;
}
void
setExtra2
(
int
extra2
)
{
m_extra2
=
extra2
;
}
int
extra1
()
const
{
return
m_extra1
;
}
int
extra2
()
const
{
return
m_extra2
;
}
private
:
std
::
string
m_value
;
int
m_extra1
=
0
;
...
...
@@ -88,8 +84,7 @@ TEST_SUBMODULE(pickling, m) {
};
py
::
class_
<
Pickleable
>
pyPickleable
(
m
,
"Pickleable"
);
pyPickleable
.
def
(
py
::
init
<
std
::
string
>
())
pyPickleable
.
def
(
py
::
init
<
std
::
string
>
())
.
def
(
"value"
,
&
Pickleable
::
value
)
.
def
(
"extra1"
,
&
Pickleable
::
extra1
)
.
def
(
"extra2"
,
&
Pickleable
::
extra2
)
...
...
@@ -105,7 +100,7 @@ TEST_SUBMODULE(pickling, m) {
pyPickleable
.
def
(
"__setstate__"
,
[](
Pickleable
&
p
,
const
py
::
tuple
&
t
)
{
if
(
t
.
size
()
!=
3
)
{
throw
std
::
runtime_error
(
"Invalid state!"
);
}
}
/* Invoke the constructor (need to use in-place version) */
new
(
&
p
)
Pickleable
(
t
[
0
].
cast
<
std
::
string
>
());
...
...
@@ -124,7 +119,7 @@ TEST_SUBMODULE(pickling, m) {
[](
const
py
::
tuple
&
t
)
{
if
(
t
.
size
()
!=
3
)
{
throw
std
::
runtime_error
(
"Invalid state!"
);
}
}
auto
p
=
PickleableNew
(
t
[
0
].
cast
<
std
::
string
>
());
p
.
setExtra1
(
t
[
1
].
cast
<
int
>
());
...
...
@@ -136,7 +131,7 @@ TEST_SUBMODULE(pickling, m) {
// test_roundtrip_with_dict
class
PickleableWithDict
{
public
:
explicit
PickleableWithDict
(
const
std
::
string
&
value
)
:
value
(
value
)
{
}
explicit
PickleableWithDict
(
const
std
::
string
&
value
)
:
value
(
value
)
{}
std
::
string
value
;
int
extra
;
...
...
@@ -147,7 +142,8 @@ TEST_SUBMODULE(pickling, m) {
using
PickleableWithDict
::
PickleableWithDict
;
};
py
::
class_
<
PickleableWithDict
>
pyPickleableWithDict
(
m
,
"PickleableWithDict"
,
py
::
dynamic_attr
());
py
::
class_
<
PickleableWithDict
>
pyPickleableWithDict
(
m
,
"PickleableWithDict"
,
py
::
dynamic_attr
());
pyPickleableWithDict
.
def
(
py
::
init
<
std
::
string
>
())
.
def_readwrite
(
"value"
,
&
PickleableWithDict
::
value
)
.
def_readwrite
(
"extra"
,
&
PickleableWithDict
::
extra
)
...
...
@@ -159,7 +155,7 @@ TEST_SUBMODULE(pickling, m) {
pyPickleableWithDict
.
def
(
"__setstate__"
,
[](
const
py
::
object
&
self
,
const
py
::
tuple
&
t
)
{
if
(
t
.
size
()
!=
3
)
{
throw
std
::
runtime_error
(
"Invalid state!"
);
}
}
/* Cast and construct */
auto
&
p
=
self
.
cast
<
PickleableWithDict
&>
();
new
(
&
p
)
PickleableWithDict
(
t
[
0
].
cast
<
std
::
string
>
());
...
...
@@ -176,12 +172,13 @@ TEST_SUBMODULE(pickling, m) {
.
def
(
py
::
init
<
std
::
string
>
())
.
def
(
py
::
pickle
(
[](
const
py
::
object
&
self
)
{
return
py
::
make_tuple
(
self
.
attr
(
"value"
),
self
.
attr
(
"extra"
),
self
.
attr
(
"__dict__"
));
return
py
::
make_tuple
(
self
.
attr
(
"value"
),
self
.
attr
(
"extra"
),
self
.
attr
(
"__dict__"
));
},
[](
const
py
::
tuple
&
t
)
{
if
(
t
.
size
()
!=
3
)
{
throw
std
::
runtime_error
(
"Invalid state!"
);
}
}
auto
cpp_state
=
PickleableWithDictNew
(
t
[
0
].
cast
<
std
::
string
>
());
cpp_state
.
extra
=
t
[
1
].
cast
<
int
>
();
...
...
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