From cd0383982e1ecef41515b1811a3ef052ad190b28 Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Tue, 12 May 2020 15:36:50 -0400 Subject: [PATCH] Use UTF-8 string caching introduced with Py3.2. According to the Python documentation, calling `PyUnicode_AsUTF8AndSize` will cache a UTF-8 version of the python string for future use. So let's do that instead of reencoding UTF-16 or UTF-32 to UTF-8 each call to `PyUnicode_AsSTString`. --- Sources/Plasma/FeatureLib/pfPython/pyGlueHelpers.cpp | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/Sources/Plasma/FeatureLib/pfPython/pyGlueHelpers.cpp b/Sources/Plasma/FeatureLib/pfPython/pyGlueHelpers.cpp index c5daa3120c..10db23c98e 100644 --- a/Sources/Plasma/FeatureLib/pfPython/pyGlueHelpers.cpp +++ b/Sources/Plasma/FeatureLib/pfPython/pyGlueHelpers.cpp @@ -50,13 +50,10 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com ST::string PyUnicode_AsSTString(PyObject* obj) { if (PyUnicode_Check(obj)) { -#if (Py_UNICODE_SIZE == 2) - return ST::string::from_utf16(reinterpret_cast(PyUnicode_AsUnicode(obj))); -#elif (Py_UNICODE_SIZE == 4) - return ST::string::from_utf32(reinterpret_cast(PyUnicode_AsUnicode(obj))); -#else -# error "Py_UNICODE is an unexpected size" -#endif + Py_ssize_t size; + const char* str = PyUnicode_AsUTF8AndSize(obj, &size); + if (str) + return ST::string::from_utf8(str, size, ST::assume_valid); } return ST::null;