Commit f30c3a6b authored by Stefan Behnel's avatar Stefan Behnel

prevent Py_UNICODE from becoming a plain integer during arithmetic operations

parent 42572ca9
...@@ -5015,6 +5015,11 @@ class NumBinopNode(BinopNode): ...@@ -5015,6 +5015,11 @@ class NumBinopNode(BinopNode):
self.operand1.result(), self.operand1.result(),
self.operand2.result()) self.operand2.result())
def is_py_operation_types(self, type1, type2):
return (type1 is PyrexTypes.c_py_unicode_type or
type2 is PyrexTypes.c_py_unicode_type or
BinopNode.is_py_operation_types(self, type1, type2))
def py_operation_function(self): def py_operation_function(self):
return self.py_functions[self.operator] return self.py_functions[self.operator]
......
...@@ -2443,6 +2443,9 @@ def spanning_type(type1, type2): ...@@ -2443,6 +2443,9 @@ def spanning_type(type1, type2):
return type1 return type1
elif type1 is py_object_type or type2 is py_object_type: elif type1 is py_object_type or type2 is py_object_type:
return py_object_type return py_object_type
elif type1 is c_py_unicode_type or type2 is c_py_unicode_type:
# Py_UNICODE behaves more like a string than an int
return py_object_type
span_type = _spanning_type(type1, type2) span_type = _spanning_type(type1, type2)
if span_type is None: if span_type is None:
return py_object_type return py_object_type
......
...@@ -126,3 +126,31 @@ def index_compare_string(unicode ustring, Py_ssize_t i, unicode other): ...@@ -126,3 +126,31 @@ def index_compare_string(unicode ustring, Py_ssize_t i, unicode other):
IndexError: string index out of range IndexError: string index out of range
""" """
return ustring[i] == other return ustring[i] == other
@cython.test_assert_path_exists("//CoerceToPyTypeNode",
"//IndexNode",
"//MulNode",
"//MulNode/CoerceToPyTypeNode")
@cython.test_fail_if_path_exists("//IndexNode//CoerceToPyTypeNode")
def index_multiply(unicode ustring, Py_ssize_t i, int mul):
"""
>>> ustring[0] * 5
u'aaaaa'
>>> index_multiply(ustring, 0, 5)
u'aaaaa'
"""
return ustring[i] * mul
@cython.test_assert_path_exists("//CoerceToPyTypeNode",
"//IndexNode",
"//AddNode",
"//AddNode/CoerceToPyTypeNode")
@cython.test_fail_if_path_exists("//IndexNode//CoerceToPyTypeNode")
def index_add(unicode ustring, Py_ssize_t i, Py_ssize_t j):
"""
>>> ustring[0] + ustring[-1]
u'a6'
>>> index_add(ustring, 0, -1)
u'a6'
"""
return ustring[i] + ustring[j]
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