From 1011ddeb7d9c8fee5b23c5ac590057740c04c902 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Thu, 22 Apr 2021 07:08:34 -0700 Subject: [PATCH] pickle setstate: setattr __dict__ only if not empty, to not force use of py::dynamic_attr() unnecessarily. --- include/pybind11/detail/init.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/include/pybind11/detail/init.h b/include/pybind11/detail/init.h index 3ef78c1179f..376f1142b10 100644 --- a/include/pybind11/detail/init.h +++ b/include/pybind11/detail/init.h @@ -293,6 +293,11 @@ template ::value, int> = 0> void setstate(value_and_holder &v_h, std::pair &&result, bool need_alias) { construct(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); }