summaryrefslogtreecommitdiff
path: root/Lib
diff options
context:
space:
mode:
authorClinton Stimpson <cjstimp@sandia.gov>2022-10-29 11:19:18 -0600
committerOlly Betts <ojwbetts@gmail.com>2022-11-01 07:54:58 +1300
commit1f40d38e56c79d5ba7b2442033297c8095e0cbe8 (patch)
treefedbcf958d724579d9fbae2d7006d08bfbb254f5 /Lib
parentdf716b8f00fd4c6c980b91569ab7612015509c01 (diff)
downloadswig-1f40d38e56c79d5ba7b2442033297c8095e0cbe8.tar.gz
Use more portable PyUnicode_AsUTF8* methods.
This supports the use of Py_LIMITED_API and also uses PyUnicode_AsUTF8AndSize if Py_LIMITED_API is set to 3.10 or newer.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/python/pyhead.swg9
1 files changed, 8 insertions, 1 deletions
diff --git a/Lib/python/pyhead.swg b/Lib/python/pyhead.swg
index 6f37160bb..a5f804a7b 100644
--- a/Lib/python/pyhead.swg
+++ b/Lib/python/pyhead.swg
@@ -34,7 +34,14 @@ SWIGINTERN char*
SWIG_Python_str_AsChar(PyObject *str)
{
#if PY_VERSION_HEX >= 0x03030000
- return (char *)PyUnicode_AsUTF8(str);
+# if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030A0000
+ return (char*)PyUnicode_AsUTF8AndSize(str, NULL);
+# else
+ PyObject* bytes = PyUnicode_AsUTF8String(str);
+ char* chars = PyBytes_AsString(bytes);
+ Py_DECREF(bytes);
+ return chars;
+# endif
#else
return PyString_AsString(str);
#endif