diff --git a/pybind11_abseil/absl_casters.h b/pybind11_abseil/absl_casters.h index 0e57884..a465f4a 100644 --- a/pybind11_abseil/absl_casters.h +++ b/pybind11_abseil/absl_casters.h @@ -42,6 +42,7 @@ #include #include #include +#include #include #include #include @@ -634,7 +635,15 @@ struct type_caster { return str(std::string(src)).release(); } #endif - return bytes(std::string(src)).release(); + bytes data(nullptr, src.size()); + // Cord::CopyToArray is not always available so we need to copy + // the cord manually. + char* ptr = PyBytes_AS_STRING(data.ptr()); + for (absl::string_view chunk : src.Chunks()) { + std::memcpy(ptr, chunk.data(), chunk.size()); + ptr += chunk.size(); + } + return data.release(); } };