Skip to content

Commit 9ca9fc9

Browse files
committed
Avoid use of temporary bytes object in string_caster for UTF-8
Fixes #3252
1 parent 6abf2ba commit 9ca9fc9

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

include/pybind11/cast.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,22 @@ template <typename StringType, bool IsView = false> struct string_caster {
377377
#endif
378378
}
379379

380+
#if PY_VERSION_HEX >= 0x03030000
381+
// On Python >= 3.3, for UTF-8 we avoid the need for a temporary `bytes`
382+
// object by using `PyUnicode_AsUTF8AndSize`.
383+
if (PYBIND11_SILENCE_MSVC_C4127(UTF_N == 8)) {
384+
Py_ssize_t size; // NOLINT(cppcoreguidelines-init-variables)
385+
const auto *buffer
386+
= reinterpret_cast<const CharT *>(PyUnicode_AsUTF8AndSize(load_src.ptr(), &size));
387+
if (!buffer) {
388+
PyErr_Clear();
389+
return false;
390+
}
391+
value = StringType(buffer, static_cast<size_t>(size));
392+
return true;
393+
}
394+
#endif
395+
380396
auto utfNbytes = reinterpret_steal<object>(PyUnicode_AsEncodedString(
381397
load_src.ptr(), UTF_N == 8 ? "utf-8" : UTF_N == 16 ? "utf-16" : "utf-32", nullptr));
382398
if (!utfNbytes) { PyErr_Clear(); return false; }

0 commit comments

Comments
 (0)