Skip to content

Commit

Permalink
Fix an issue trying list.index indexing in FusedNode
Browse files Browse the repository at this point in the history
In some Python versions, generating the error message when .index
fails to find an index leads to a compiler crash.

Fix this by not relying on type being fully set up while generating
__str__.

Fixes cython#5894 and cython#5588
  • Loading branch information
da-woods committed Dec 5, 2023
1 parent 02ddde3 commit 773667a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Cython/Compiler/PyrexTypes.py
Expand Up @@ -3294,7 +3294,12 @@ def declaration_code(self, entity_code,
if self.is_overridable:
arg_decl_list.append("int %s" % Naming.skip_dispatch_cname)
if self.optional_arg_count:
arg_decl_list.append(self.op_arg_struct.declaration_code(Naming.optional_args_cname))
if self.op_arg_struct:
arg_decl_list.append(self.op_arg_struct.declaration_code(Naming.optional_args_cname))
else:
# op_arg_struct may not be initialized at this point if this class is being used
# to prepare a Python error message or similar. In this case, just omit the args.
assert for_display
if self.has_varargs:
arg_decl_list.append("...")
arg_decl_code = ", ".join(arg_decl_list)
Expand Down
4 changes: 4 additions & 0 deletions tests/run/fused_cpdef.pxd
@@ -0,0 +1,4 @@
cimport cython

cdef class C:
cpdef object has_default_struct(self, cython.floating x, a=?)
13 changes: 13 additions & 0 deletions tests/run/fused_cpdef.pyx
Expand Up @@ -205,3 +205,16 @@ def test_defaults():
>>> mutable_default(3,[])
[3]
"""

cdef class C:
cpdef object has_default_struct(self, cython.floating x, a=None):
return x, a

# https://github.com/cython/cython/issues/5588
# On some Python versions this was causing a compiler crash
def test_call_has_default_struct(C c, double x):
"""
>>> test_call_has_default_struct(C(), 5.)
(5.0, None)
"""
return c.has_default_struct(x)

0 comments on commit 773667a

Please sign in to comment.