Skip to content

Commit

Permalink
Avoid use of temporary bytes object in string_caster for UTF-8
Browse files Browse the repository at this point in the history
Fixes #3252
  • Loading branch information
jbms committed Sep 9, 2021
1 parent 6abf2ba commit 267c8f9
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions include/pybind11/cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,22 @@ template <typename StringType, bool IsView = false> struct string_caster {
#endif
}

#if PY_VERSION_HEX >= 0x03030000
// On Python >= 3.3, for UTF-8 we avoid the need for a temporary `bytes`
// object by using `PyUnicode_AsUTF8AndSize`.
if (PYBIND11_SILENCE_MSVC_C4127(UTF_N == 8)) {
Py_ssize_t size = -1;
const auto *buffer
= reinterpret_cast<const CharT *>(PyUnicode_AsUTF8AndSize(load_src.ptr(), &size));
if (!buffer) {
PyErr_Clear();
return false;
}
value = StringType(buffer, static_cast<size_t>(size));
return true;
}
#endif

auto utfNbytes = reinterpret_steal<object>(PyUnicode_AsEncodedString(
load_src.ptr(), UTF_N == 8 ? "utf-8" : UTF_N == 16 ? "utf-16" : "utf-32", nullptr));
if (!utfNbytes) { PyErr_Clear(); return false; }
Expand Down

0 comments on commit 267c8f9

Please sign in to comment.