Skip to content

Commit

Permalink
Improve "@__setstate__" related code in pybind11.h for readability.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralf W. Grosse-Kunstleve committed Jan 26, 2024
1 parent 10b0796 commit 1d7ac2f
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,11 @@ class cpp_function : public function {
// it alive.
auto *rec = unique_rec.get();

// Note the "@__setstate__" manipulation below.
rec->is_constructor = rec->name != nullptr
&& (std::strcmp(rec->name, "__init__") == 0
|| std::strcmp(rec->name, "__setstate__") == 0);

// Keep track of strdup'ed strings, and clean them up as long as the function's capsule
// has not taken ownership yet (when `unique_rec.release()` is called).
// Note: This cannot easily be fixed by a `unique_ptr` with custom deleter, because the
Expand All @@ -403,12 +408,13 @@ class cpp_function : public function {
strdup_guard guarded_strdup;

/* Create copies of all referenced C-style strings */
bool setstate_is_ctor = true;
if (rec->name && std::strcmp(rec->name, "@__setstate__") == 0) {
if (rec->name == nullptr) {
rec->name = guarded_strdup("");
} else if (std::strcmp(rec->name, "@__setstate__") == 0) {
// See google/pywrapcc#30094 for background.
rec->name = guarded_strdup(rec->name + 1);
setstate_is_ctor = false;
} else {
rec->name = guarded_strdup(rec->name ? rec->name : "");
rec->name = guarded_strdup(rec->name);
}
if (rec->doc) {
rec->doc = guarded_strdup(rec->doc);
Expand All @@ -424,9 +430,6 @@ class cpp_function : public function {
}
}

rec->is_constructor = (std::strcmp(rec->name, "__init__") == 0)
|| (setstate_is_ctor && std::strcmp(rec->name, "__setstate__") == 0);

#if defined(PYBIND11_DETAILED_ERROR_MESSAGES) && !defined(PYBIND11_DISABLE_NEW_STYLE_INIT_WARNING)
if (rec->is_constructor && !rec->is_new_style_constructor) {
const auto class_name
Expand Down

0 comments on commit 1d7ac2f

Please sign in to comment.