Richcompare cause the SystemError: Objects/object.c:834: bad argument to internal function

Hi guys,

I am trying to implement the partial_richcompare to functools.partial. I achieve to compare same(takes the same function and the same argument) partial objects as True.

But when I run the CPython tests I get the following error; What could be the cause of this?
SystemError: Objects/object.c:834: bad argument to internal function

Example

>>> import functools
>>> f = lambda x:x
>>> a = functools.partial(f, 1)
>>> b = functools.partial(f, 1)
>>> a == b
True
>>>

PyObject_RichCompare

static PyObject *
partial_richcompare(PyObject *self, PyObject *other, int op)
{
    partialobject *a, *b;
    PyObject *res;
    int eq;

    if (op != Py_EQ && op != Py_NE) {
        Py_RETURN_NOTIMPLEMENTED;
    }

    a = (partialobject *) self;
    b = (partialobject *) other;

    eq = PyObject_RichCompareBool(a->fn, b->fn, Py_EQ);
    if (eq == 1) {
        eq = PyObject_RichCompareBool(a->args, b->args, Py_EQ);
        if (eq == 1) {
            eq = PyObject_RichCompareBool(a->kw, b->kw, Py_EQ);
        }
    }

    if (eq < 0) {
        return NULL;
    }

    if (op == Py_EQ) {
        res = eq ? Py_True : Py_False;
    }
    else {
        res = eq ? Py_False : Py_True;
    }

    Py_INCREF(res);
    return res;
}

I think it might be that partial doesn’t require keyword arguments, so kw can be NULL, i.e. a->kw or b->kw might be NULL.

1 Like

I had to check comparisons with other types. Thanks for your help.