Commit 5578a9ea authored by Stefan Behnel's avatar Stefan Behnel

Return Py_ssize_t instead of size_t for len(char*) and len(Py_UNICODE*).

Closes GH-2992.
parent 087070ba
......@@ -2557,12 +2557,20 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin,
Pyx_strlen_func_type = PyrexTypes.CFuncType(
PyrexTypes.c_size_t_type, [
PyrexTypes.CFuncTypeArg("bytes", PyrexTypes.c_const_char_ptr_type, None)
])
],
nogil=True)
Pyx_ssize_strlen_func_type = PyrexTypes.CFuncType(
PyrexTypes.c_py_ssize_t_type, [
PyrexTypes.CFuncTypeArg("bytes", PyrexTypes.c_const_char_ptr_type, None)
],
exception_value="-1")
Pyx_Py_UNICODE_strlen_func_type = PyrexTypes.CFuncType(
PyrexTypes.c_size_t_type, [
PyrexTypes.c_py_ssize_t_type, [
PyrexTypes.CFuncTypeArg("unicode", PyrexTypes.c_const_py_unicode_ptr_type, None)
])
],
exception_value="-1")
PyObject_Size_func_type = PyrexTypes.CFuncType(
PyrexTypes.c_py_ssize_t_type, [
......@@ -2596,15 +2604,16 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin,
arg = arg.arg
if arg.type.is_string:
new_node = ExprNodes.PythonCapiCallNode(
node.pos, "strlen", self.Pyx_strlen_func_type,
node.pos, "__Pyx_ssize_strlen", self.Pyx_ssize_strlen_func_type,
args = [arg],
is_temp = node.is_temp,
utility_code = UtilityCode.load_cached("IncludeStringH", "StringTools.c"))
utility_code = UtilityCode.load_cached("ssize_strlen", "StringTools.c"))
elif arg.type.is_pyunicode_ptr:
new_node = ExprNodes.PythonCapiCallNode(
node.pos, "__Pyx_Py_UNICODE_strlen", self.Pyx_Py_UNICODE_strlen_func_type,
node.pos, "__Pyx_Py_UNICODE_ssize_strlen", self.Pyx_Py_UNICODE_strlen_func_type,
args = [arg],
is_temp = node.is_temp)
is_temp = node.is_temp,
utility_code = UtilityCode.load_cached("ssize_pyunicode_strlen", "StringTools.c"))
elif arg.type.is_memoryviewslice:
func_type = PyrexTypes.CFuncType(
PyrexTypes.c_py_ssize_t_type, [
......
......@@ -7,6 +7,40 @@
#include <string>
//////////////////// ssize_strlen.proto ////////////////////
static CYTHON_INLINE Py_ssize_t __Pyx_ssize_strlen(const char *s);/*proto*/
//////////////////// ssize_strlen ////////////////////
//@requires: IncludeStringH
static CYTHON_INLINE Py_ssize_t __Pyx_ssize_strlen(const char *s) {
size_t len = strlen(s);
if (unlikely(len > PY_SSIZE_T_MAX)) {
PyErr_SetString(PyExc_OverflowError, "byte string is too long");
return -1;
}
return (Py_ssize_t) len;
}
//////////////////// ssize_pyunicode_strlen.proto ////////////////////
static CYTHON_INLINE Py_ssize_t __Pyx_Py_UNICODE_ssize_strlen(const Py_UNICODE *u);/*proto*/
//////////////////// ssize_pyunicode_strlen ////////////////////
static CYTHON_INLINE Py_ssize_t __Pyx_Py_UNICODE_ssize_strlen(const Py_UNICODE *u) {
size_t len = __Pyx_Py_UNICODE_strlen(u);
if (unlikely(len > PY_SSIZE_T_MAX)) {
PyErr_SetString(PyExc_OverflowError, "Py_UNICODE string is too long");
return -1;
}
return (Py_ssize_t) len;
}
//////////////////// InitStrings.proto ////////////////////
static int __Pyx_InitStrings(__Pyx_StringTabEntry *t); /*proto*/
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment