@@ -939,6 +939,62 @@ PyThreadState_GetUnchecked(void)
939939}
940940#endif
941941
942+ // gh-110289 added PyUnicode_EqualToUTF8() to Python 3.13.0a1
943+ #if PY_VERSION_HEX < 0x030D00A1
944+ static inline int
945+ PyUnicode_EqualToUTF8 (PyObject *unicode, const char *str)
946+ {
947+ Py_ssize_t len;
948+ const void *utf8;
949+ // Python 3.3.0a1 added PyUnicode_AsUTF8AndSize()
950+ #if PY_VERSION_HEX >= 0x030300A1
951+ if (PyUnicode_IS_ASCII (unicode)) {
952+ utf8 = PyUnicode_DATA (unicode);
953+ len = PyUnicode_GET_LENGTH (unicode);
954+ }
955+ else {
956+ utf8 = PyUnicode_AsUTF8AndSize (unicode, &len);
957+ if (utf8 == NULL ) {
958+ // Memory allocation failure. The API cannot report error,
959+ // so clear the exception and return 0.
960+ PyErr_Clear ();
961+ return 0 ;
962+ }
963+ }
964+
965+ if ((size_t )len != strlen (str)) {
966+ return 0 ;
967+ }
968+ return (memcmp (utf8, str, (size_t )len) == 0 );
969+ #else
970+ int res;
971+ PyObject *bytes = PyUnicode_AsUTF8String (unicode);
972+ if (bytes == NULL ) {
973+ // Memory allocation failure. The API cannot report error,
974+ // so clear the exception and return 0.
975+ PyErr_Clear ();
976+ return 0 ;
977+ }
978+
979+ #if PY_VERSION_HEX >= 0x03000000
980+ len = PyBytes_GET_SIZE (bytes);
981+ utf8 = PyBytes_AS_STRING (bytes);
982+ #else
983+ len = PyString_GET_SIZE (bytes);
984+ utf8 = PyString_AS_STRING (bytes);
985+ #endif
986+ if ((size_t )len != strlen (str)) {
987+ Py_DECREF (bytes);
988+ return 0 ;
989+ }
990+
991+ res = (memcmp (utf8, str, (size_t )len) == 0 );
992+ Py_DECREF (bytes);
993+ return res;
994+ #endif
995+ }
996+ #endif
997+
942998
943999#ifdef __cplusplus
9441000}
0 commit comments