-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ralf W. Grosse-Kunstleve
committed
Jun 4, 2021
1 parent
eeafbcc
commit ad96e32
Showing
5 changed files
with
85 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,65 @@ | ||
// clang-format off | ||
/* | ||
tests/test_pickling.cpp -- pickle support | ||
Copyright (c) 2016 Wenzel Jakob <[email protected]> | ||
Copyright (c) 2021 The Pybind Development Team. | ||
All rights reserved. Use of this source code is governed by a | ||
BSD-style license that can be found in the LICENSE file. | ||
*/ | ||
|
||
#include "pybind11_tests.h" | ||
|
||
// clang-format on | ||
|
||
#include <memory> | ||
#include <stdexcept> | ||
#include <utility> | ||
|
||
namespace exercise_trampoline { | ||
|
||
struct SimpleBase { | ||
int num = 0; | ||
virtual ~SimpleBase() = default; | ||
|
||
// For compatibility with old clang versions: | ||
SimpleBase() = default; | ||
SimpleBase(const SimpleBase &) = default; | ||
}; | ||
|
||
struct SimpleBaseTrampoline : SimpleBase {}; | ||
|
||
struct SimpleCppDerived : SimpleBase {}; | ||
|
||
void wrap(py::module m) { | ||
py::class_<SimpleBase, SimpleBaseTrampoline>(m, "SimpleBase") | ||
.def(py::init<>()) | ||
.def_readwrite("num", &SimpleBase::num) | ||
.def(py::pickle( | ||
[](py::object self) { | ||
py::dict d; | ||
if (py::hasattr(self, "__dict__")) | ||
d = self.attr("__dict__"); | ||
return py::make_tuple(self.attr("num"), d); | ||
}, | ||
[](py::tuple t) { | ||
if (t.size() != 2) | ||
throw std::runtime_error("Invalid state!"); | ||
auto cpp_state = std::unique_ptr<SimpleBase>(new SimpleBaseTrampoline); | ||
cpp_state->num = t[0].cast<int>(); | ||
auto py_state = t[1].cast<py::dict>(); | ||
return std::make_pair(std::move(cpp_state), py_state); | ||
})); | ||
|
||
m.def("make_SimpleCppDerivedAsBase", | ||
[]() { return std::unique_ptr<SimpleBase>(new SimpleCppDerived); }); | ||
} | ||
|
||
} // namespace exercise_trampoline | ||
|
||
// clang-format off | ||
|
||
TEST_SUBMODULE(pickling, m) { | ||
// test_roundtrip | ||
class Pickleable { | ||
|
@@ -135,4 +186,6 @@ TEST_SUBMODULE(pickling, m) { | |
} | ||
)); | ||
#endif | ||
|
||
exercise_trampoline::wrap(m); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.