Commit d3982ce7 authored by Stefan Behnel's avatar Stefan Behnel

safety fix

parent 067af347
...@@ -1205,10 +1205,6 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform): ...@@ -1205,10 +1205,6 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform):
]) ])
def _handle_simple_function_len(self, node, pos_args): def _handle_simple_function_len(self, node, pos_args):
# note: this only works because we already replaced len() by
# PyObject_Length() which returns a Py_ssize_t instead of a
# Python object, so we can return a plain size_t instead
# without caring about Python object conversion etc.
if len(pos_args) != 1: if len(pos_args) != 1:
self._error_wrong_arg_count('len', node, pos_args, 1) self._error_wrong_arg_count('len', node, pos_args, 1)
return node return node
...@@ -1217,6 +1213,13 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform): ...@@ -1217,6 +1213,13 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform):
arg = arg.arg arg = arg.arg
if not arg.type.is_string: if not arg.type.is_string:
return node return node
if not node.type.is_numeric:
# this optimisation only works when we already replaced
# len() by PyObject_Length() which returns a Py_ssize_t
# instead of a Python object, so we can return a plain
# size_t instead without caring about Python object
# conversion etc.
return node
node = ExprNodes.PythonCapiCallNode( node = ExprNodes.PythonCapiCallNode(
node.pos, "strlen", self.Pyx_strlen_func_type, node.pos, "strlen", self.Pyx_strlen_func_type,
args = [arg], args = [arg],
......
...@@ -3,6 +3,10 @@ __doc__ = """ ...@@ -3,6 +3,10 @@ __doc__ = """
7 7
>>> lentest_char_c() >>> lentest_char_c()
7 7
>>> lentest_char_c_short()
7
>>> lentest_char_c_float()
7.0
>>> lentest_uchar() >>> lentest_uchar()
7 7
...@@ -36,6 +40,20 @@ def lentest_char_c(): ...@@ -36,6 +40,20 @@ def lentest_char_c():
cdef Py_ssize_t l = len(s) cdef Py_ssize_t l = len(s)
return l return l
@cython.test_assert_path_exists(
"//PythonCapiCallNode",
)
def lentest_char_c_short():
cdef short l = len(s)
return l
@cython.test_assert_path_exists(
"//PythonCapiCallNode",
)
def lentest_char_c_float():
cdef float l = len(s)
return l
@cython.test_assert_path_exists( @cython.test_assert_path_exists(
"//PythonCapiCallNode", "//PythonCapiCallNode",
......
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