Skip to content

Commit

Permalink
fix: make sure PyUnicode_READY() is called before unicode iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
scoder committed Apr 15, 2012
1 parent 04ff4ec commit ee471d5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
30 changes: 24 additions & 6 deletions Cython/Compiler/Optimize.py
Expand Up @@ -283,14 +283,20 @@ def _transform_bytes_iteration(self, node, slice_node, reversed=False):
),
reversed = reversed))

PyUnicode_READY_func_type = PyrexTypes.CFuncType(
PyrexTypes.c_int_type, [
PyrexTypes.CFuncTypeArg("s", PyrexTypes.py_object_type, None)
],
exception_value='-1')

PyUnicode_GET_LENGTH_func_type = PyrexTypes.CFuncType(
PyrexTypes.c_py_ssize_t_type, [
PyrexTypes.CFuncTypeArg("s", Builtin.unicode_type, None)
PyrexTypes.CFuncTypeArg("s", PyrexTypes.py_object_type, None)
])

PyUnicode_KIND_func_type = PyrexTypes.CFuncType(
PyrexTypes.c_int_type, [
PyrexTypes.CFuncTypeArg("s", Builtin.unicode_type, None)
PyrexTypes.CFuncTypeArg("s", PyrexTypes.py_object_type, None)
])

PyUnicode_READ_func_type = PyrexTypes.CFuncType(
Expand All @@ -302,7 +308,7 @@ def _transform_bytes_iteration(self, node, slice_node, reversed=False):

PyUnicode_DATA_func_type = PyrexTypes.CFuncType(
PyrexTypes.c_void_ptr_type, [
PyrexTypes.CFuncTypeArg("s", Builtin.unicode_type, None)
PyrexTypes.CFuncTypeArg("s", PyrexTypes.py_object_type, None)
])

def _transform_unicode_iteration(self, node, slice_node, reversed=False):
Expand Down Expand Up @@ -349,7 +355,7 @@ def _transform_unicode_iteration(self, node, slice_node, reversed=False):
slice_node.pos, "__Pyx_PyUnicode_READ",
self.PyUnicode_READ_func_type,
args = [kind_temp, data_temp, counter_temp],
is_temp = 0,
is_temp = False,
))
body = Nodes.StatListNode(
node.pos,
Expand All @@ -366,9 +372,21 @@ def _transform_unicode_iteration(self, node, slice_node, reversed=False):

loop_node = UtilNodes.TempsBlockNode(
node.pos, temps=[counter], body=loop_node)
for temp in (kind_temp, data_temp, unpack_temp_node): # last is outermost temp
for temp in (kind_temp, data_temp):
loop_node = UtilNodes.LetNode(temp, loop_node)
return loop_node

setup_node = Nodes.ExprStatNode(
node.pos,
expr = ExprNodes.PythonCapiCallNode(
slice_node.pos, "__Pyx_PyUnicode_READY",
self.PyUnicode_READY_func_type,
args = [unpack_temp_node],
is_temp = True,
result_is_used = False,
))
return UtilNodes.LetNode(
unpack_temp_node,
Nodes.StatListNode(node.pos, stats=[setup_node, loop_node]))

def _transform_carray_iteration(self, node, slice_node, reversed=False):
neg_step = False
Expand Down
2 changes: 2 additions & 0 deletions Cython/Utility/ModuleSetupCode.c
Expand Up @@ -119,13 +119,15 @@
/* new Py3.3 unicode type (PEP 393) */
#if PY_VERSION_HEX > 0x03030000 && defined(PyUnicode_KIND)
#define CYTHON_PEP393_ENABLED 1
#define __Pyx_PyUnicode_READY(u) PyUnicode_READY(u)
#define __Pyx_PyUnicode_KIND(u) PyUnicode_KIND(u)
#define __Pyx_PyUnicode_DATA(u) PyUnicode_DATA(u)
#define __Pyx_PyUnicode_GET_LENGTH(u) PyUnicode_GET_LENGTH(u)
#define __Pyx_PyUnicode_READ_CHAR(u, i) PyUnicode_READ_CHAR(u, i)
#define __Pyx_PyUnicode_READ(k, d, i) PyUnicode_READ(k, d, i)
#else
#define CYTHON_PEP393_ENABLED 0
#define __Pyx_PyUnicode_READY(u) (0)
#define __Pyx_PyUnicode_KIND(u) (0) /* PyUnicode_WCHAR_KIND */
#define __Pyx_PyUnicode_DATA(u) PyUnicode_AS_UNICODE(u)
#define __Pyx_PyUnicode_GET_LENGTH(u) PyUnicode_GET_SIZE(u)
Expand Down

0 comments on commit ee471d5

Please sign in to comment.