Use PyUnicode_GET_LENGTH when possible otherwise use PyUnicode_GET_SIZE for Python 3 (#1614)

* Use PyUnicode_GET_LENGTH for Python 3.3+ and PyUnicode_GET_SIZE before Python3.3 as GET_SIZE is deprecated in 3.3 and removed in 3.12 while GET_LENGTH is new in 3.3.

* Fixed correct python version for when PyUnicode_GET_LENGTH was new in a comment.
This commit is contained in:
Hong Chen 2023-11-16 10:27:50 -06:00 committed by GitHub
parent 25b692d055
commit 2c27b65e14
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -64,9 +64,15 @@ template<typename T > static int typemap_in_scalar( T & output , PyObject *input
output = (T)PyFloat_AsDouble(input) ;
} else if ( PyInt_Check(input) ) {
output = (T)PyInt_AsLong(input) ;
// PyUnicode_GET_SIZE: deprecated since Python 3.3 and removed in Python 3.12
// PyUnicode_GET_LENGTH: new in Python 3.3
#if PY_VERSION_HEX >= 0x03000000
} else if ( PyUnicode_Check(input) ) {
#if defined(PyUnicode_GET_LENGTH)
if ( PyUnicode_GET_LENGTH(input) == 1 ) {
#else
if ( PyUnicode_GET_SIZE(input) == 1 ) {
#endif
PyObject * temp = PyUnicode_AsEncodedString(input, "utf-8", "Error ~");
char * temp_str = PyBytes_AS_STRING(temp) ;
output = (T)temp_str[0] ;
@ -142,7 +148,11 @@ template<typename T > static T * typemap_in_1d( PyObject *input , unsigned int o
}
#if PY_VERSION_HEX >= 0x03000000
} else if ( PyUnicode_Check(input) ) {
#if defined(PyUnicode_GET_LENGTH)
unsigned int size = PyUnicode_GET_LENGTH(input) ;
#else
unsigned int size = PyUnicode_GET_SIZE(input) ;
#endif
PyObject * temp = PyUnicode_AsEncodedString(input, "utf-8", "Error ~");
char * temp_str = PyBytes_AS_STRING(temp) ;
#else
@ -330,7 +340,11 @@ template<typename T, typename baseT > static void * typemap_in_2d( PyObject *inp
}
#if PY_VERSION_HEX >= 0x03000000
} else if ( PyUnicode_Check(o) ) {
#if defined(PyUnicode_GET_LENGTH)
unsigned int size = PyUnicode_GET_LENGTH(o) ;
#else
unsigned int size = PyUnicode_GET_SIZE(o) ;
#endif
PyObject * temp = PyUnicode_AsEncodedString(o, "utf-8", "Error ~");
char * temp_str = PyBytes_AS_STRING(temp) ;
#else