Skip to content

Commit 8ca05b3

Browse files
dubeycopybara-github
authored andcommitted
Support using handle for absl::Cord saving a memcpy.
PiperOrigin-RevId: 730968780
1 parent 09cc01c commit 8ca05b3

File tree

1 file changed

+2
-33
lines changed

1 file changed

+2
-33
lines changed

pybind11_abseil/absl_casters.h

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
#ifndef PYBIND11_ABSEIL_ABSL_CASTERS_H_
3333
#define PYBIND11_ABSEIL_ABSL_CASTERS_H_
3434

35-
#include <Python.h>
3635
#include <pybind11/cast.h>
3736
#include <pybind11/pybind11.h>
3837
#include <pybind11/stl.h>
@@ -46,7 +45,6 @@
4645
#include <cstring>
4746
#include <tuple>
4847
#include <type_traits>
49-
#include <utility>
5048
#include <vector>
5149

5250
#include "absl/cleanup/cleanup.h"
@@ -620,39 +618,10 @@ struct type_caster<absl::Cord> {
620618

621619
// Conversion part 1 (Python->C++)
622620
bool load(handle src, bool convert) {
623-
// If the source is a bytes/string object, we can avoid a copy by using
624-
// absl::MakeCordFromExternal.
625-
absl::optional<absl::string_view> view;
626-
if (PyUnicode_Check(src.ptr())) {
627-
Py_ssize_t size = 0;
628-
const char* data = PyUnicode_AsUTF8AndSize(src.ptr(), &size);
629-
if (data) {
630-
view = absl::string_view(data, size);
631-
} else {
632-
PyErr_Clear();
633-
}
634-
} else if (PyBytes_Check(src.ptr())) {
635-
view = absl::string_view(PyBytes_AS_STRING(src.ptr()),
636-
PyBytes_GET_SIZE(src.ptr()));
637-
}
638-
if (view.has_value()) {
639-
if (!view->empty()) {
640-
// Bypass StringViewCaster life support as absl::Cord may outlive the
641-
// handle.
642-
src.inc_ref();
643-
value = absl::MakeCordFromExternal(*view, [src] {
644-
PyGILState_STATE gstate = PyGILState_Ensure();
645-
src.dec_ref();
646-
PyGILState_Release(gstate);
647-
});
648-
} else {
649-
value.Clear();
650-
}
651-
return true;
652-
}
653621
auto caster = StringViewCaster();
654622
if (caster.load(src, convert)) {
655-
value = absl::Cord(cast_op<absl::string_view>(std::move(caster)));
623+
absl::string_view view = cast_op<absl::string_view>(std::move(caster));
624+
value = view;
656625
return true;
657626
}
658627
return false;

0 commit comments

Comments
 (0)