Encode the text to UTF-8 (PyUnicode_AsUTF8AndSize).
Escape it into raw buffer
Decode it (PyUnicode_FromStringAndSize).
While PEP 393 specific API can be faster, it is CPython only, and it will be broken if CPython changed string implementation again. So I recommend UTF-8 APIs.
It is deprecated by PEP 393.
My recommendation is:
While PEP 393 specific API can be faster, it is CPython only, and it will be broken if CPython changed string implementation again. So I recommend UTF-8 APIs.