File tree Expand file tree Collapse file tree 1 file changed +16
-0
lines changed Expand file tree Collapse file tree 1 file changed +16
-0
lines changed Original file line number Diff line number Diff 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 ; }
You can’t perform that action at this time.
0 commit comments