Skip to content

Commit 61007e9

Browse files
committed
pickle setstate: setattr __dict__ only if not empty, to not force use of py::dynamic_attr() unnecessarily.
1 parent 5443043 commit 61007e9

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

include/pybind11/detail/init.h

+5
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,11 @@ template <typename Class, typename T, typename O,
293293
enable_if_t<std::is_convertible<O, handle>::value, int> = 0>
294294
void setstate(value_and_holder &v_h, std::pair<T, O> &&result, bool need_alias) {
295295
construct<Class>(v_h, std::move(result.first), need_alias);
296+
auto d = handle(result.second);
297+
if (PyDict_Check(d.ptr()) && PyDict_Size(d.ptr()) == 0) {
298+
// Skipping setattr below, to not force use of py::dynamic_attr() for Class unnecessarily.
299+
return;
300+
}
296301
setattr((PyObject *) v_h.inst, "__dict__", result.second);
297302
}
298303

0 commit comments

Comments
 (0)