mirror of
https://github.com/pybind/pybind11.git
synced 2026-07-14 19:17:12 +00:00
fix: add life support to handles cast to string_view (#6092)
* Fix handling of string_view to prevent GC'ing strings before they are used. * style: pre-commit fixes * Update cast.h --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
@@ -526,6 +526,9 @@ struct string_caster {
|
||||
return false;
|
||||
}
|
||||
value = StringType(buffer, static_cast<size_t>(size));
|
||||
if (IsView) {
|
||||
loader_life_support::add_patient(src);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -603,6 +606,9 @@ private:
|
||||
pybind11_fail("Unexpected PYBIND11_BYTES_AS_STRING() failure.");
|
||||
}
|
||||
value = StringType(bytes, (size_t) PYBIND11_BYTES_SIZE(src.ptr()));
|
||||
if (IsView) {
|
||||
loader_life_support::add_patient(src);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if (PyByteArray_Check(src.ptr())) {
|
||||
@@ -613,6 +619,9 @@ private:
|
||||
pybind11_fail("Unexpected PyByteArray_AsString() failure.");
|
||||
}
|
||||
value = StringType(bytearray, (size_t) PyByteArray_Size(src.ptr()));
|
||||
if (IsView) {
|
||||
loader_life_support::add_patient(src);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user