Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
Kirill Smelkov
cython
Commits
5578a9ea
Commit
5578a9ea
authored
5 years ago
by
Stefan Behnel
Browse files
Options
Download
Email Patches
Plain Diff
Return Py_ssize_t instead of size_t for len(char*) and len(Py_UNICODE*).
Closes GH-2992.
parent
087070ba
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
50 additions
and
7 deletions
+50
-7
Cython/Compiler/Optimize.py
Cython/Compiler/Optimize.py
+16
-7
Cython/Utility/StringTools.c
Cython/Utility/StringTools.c
+34
-0
No files found.
Cython/Compiler/Optimize.py
View file @
5578a9ea
...
...
@@ -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_s
size_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, [
...
...
This diff is collapsed.
Click to expand it.
Cython/Utility/StringTools.c
View file @
5578a9ea
...
...
@@ -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*/
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment