Skip to content

Commit

Permalink
pickle setstate: setattr __dict__ only if not empty, to not force use…
Browse files Browse the repository at this point in the history
… of py::dynamic_attr() unnecessarily.
  • Loading branch information
Ralf W. Grosse-Kunstleve committed Apr 22, 2021
1 parent 5443043 commit 61007e9
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions include/pybind11/detail/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,11 @@ template <typename Class, typename T, typename O,
enable_if_t<std::is_convertible<O, handle>::value, int> = 0>
void setstate(value_and_holder &v_h, std::pair<T, O> &&result, bool need_alias) {
construct<Class>(v_h, std::move(result.first), need_alias);
auto d = handle(result.second);
if (PyDict_Check(d.ptr()) && PyDict_Size(d.ptr()) == 0) {
// Skipping setattr below, to not force use of py::dynamic_attr() for Class unnecessarily.
return;
}
setattr((PyObject *) v_h.inst, "__dict__", result.second);
}

Expand Down

0 comments on commit 61007e9

Please sign in to comment.