MCPcopy Create free account
hub / github.com/pybind/pybind11 / decode_utfN

Method decode_utfN

include/pybind11/cast.h:574–590  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

572
573private:
574 static handle decode_utfN(const char *buffer, ssize_t nbytes) {
575#if !defined(PYPY_VERSION)
576 return UTF_N == 8 ? PyUnicode_DecodeUTF8(buffer, nbytes, nullptr)
577 : UTF_N == 16 ? PyUnicode_DecodeUTF16(buffer, nbytes, nullptr, nullptr)
578 : PyUnicode_DecodeUTF32(buffer, nbytes, nullptr, nullptr);
579#else
580 // PyPy segfaults when on PyUnicode_DecodeUTF16 (and possibly on PyUnicode_DecodeUTF32 as
581 // well), so bypass the whole thing by just passing the encoding as a string value, which
582 // works properly:
583 return PyUnicode_Decode(buffer,
584 nbytes,
585 UTF_N == 8 ? "utf-8"
586 : UTF_N == 16 ? "utf-16"
587 : "utf-32",
588 nullptr);
589#endif
590 }
591
592 // When loading into a std::string or char*, accept a bytes/bytearray object as-is (i.e.
593 // without any encoding/decoding attempt). For other C++ char sizes this is a no-op.

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected