Commit f8d45815 authored by Stefan Behnel's avatar Stefan Behnel

keep Python string object references alive across temp cleanup until disposal...

keep Python string object references alive across temp cleanup until disposal (e.g. during function calls that take them as arguments)
parent 279b7b9d
......@@ -10875,6 +10875,32 @@ class CoerceFromPyTypeNode(CoercionNode):
# The arg is always already analysed
return self
def generate_evaluation_code(self, code):
if self.type.is_string:
# when coercing Python strings to C, we may have to keep the Python
# object alive a little longer, e.g. during a function call, so we do
# not dispose of subexpression temps here and do it later during cleanup
self.generate_subexpr_evaluation_code(code)
code.mark_pos(self.pos)
if self.is_temp:
self.allocate_temp_result(code)
self.generate_result_code(code)
else:
super(CoerceFromPyTypeNode, self).generate_evaluation_code(code)
def generate_disposal_code(self, code):
if self.type.is_string and self.is_temp:
# postponed from self.generate_evaluation_code()
self.generate_subexpr_disposal_code(code)
self.free_subexpr_temps(code)
super(CoerceFromPyTypeNode, self).generate_disposal_code(code)
def generate_post_assignment_code(self, code):
if self.type.is_string and self.is_temp:
self.generate_subexpr_disposal_code(code)
self.free_subexpr_temps(code)
super(CoerceFromPyTypeNode, self).generate_post_assignment_code(code)
def generate_result_code(self, code):
function = self.type.from_py_function
operand = self.arg.py_result()
......
from cpython.version cimport PY_MAJOR_VERSION
cdef cfunc(char* s):
if PY_MAJOR_VERSION == 2:
return s
else:
return s.decode('ASCII')
def test_one_arg_indexing(s):
"""
>>> test_one_arg_indexing(b'xyz')
'y'
"""
cfunc(s[0])
z = cfunc(s[2])
return cfunc(s[1])
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