Skip to content

Commit

Permalink
Fix an issue trying list.index indexing in FusedNode (#5896)
Browse files Browse the repository at this point in the history
* Fix an issue trying list.index indexing in FusedNode

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 #5894 and #5588

* Check "in" before indexing
  • Loading branch information
da-woods committed Dec 8, 2023
1 parent f64f067 commit 28b64ad
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
7 changes: 3 additions & 4 deletions Cython/Compiler/FusedNode.py
Expand Up @@ -196,12 +196,11 @@ def copy_cdef(self, env):
break

# replace old entry with new entries
try:
if self.node.entry in env.cfunc_entries:
cindex = env.cfunc_entries.index(self.node.entry)
except ValueError:
env.cfunc_entries.extend(new_cfunc_entries)
else:
env.cfunc_entries[cindex:cindex+1] = new_cfunc_entries
else:
env.cfunc_entries.extend(new_cfunc_entries)

if orig_py_func:
self.py_func = self.make_fused_cpdef(orig_py_func, env,
Expand Down
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 28b64ad

Please sign in to comment.