Skip to content

[BUG] Infinite loop/crash on __add__ #4172

@da-woods

Description

@da-woods

Describe the bug
It's possible to get an infinite loop for an add (and presumably other operators) operator that returns NotImplemented when used with a derived class.

To Reproduce

cdef class Base:
    def __add__(self, other):
        return NotImplemented

class Derived(Base):
    pass

and to test:

>>> import hmmm
>>> x = hmmm.Base()
>>> y = hmmm.Derived()
>>> y+x   # works OK as expected
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'Derived' and 'hmmm.Base'
>>> x+y

For x+y I get an infinite loop but Pandas reports segmentation fault - probably just to do with tail-call recursion optimization or not.

Expected behavior

Should just be NotImplementedError

Environment (please complete the following information):

  • OS: Linux
  • Python version 3.8.9
  • Cython version 0e80efb (close to current master)

Additional context

I think this is to do with the changes to support __radd__ etc.. I think it's somewhere in:

static PyObject *{{func_name}}(PyObject *left, PyObject *right {{extra_arg_decl}}) {

However, I haven't traced the error through in great detail so not 100% sure exactly what's gone wrong.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions