Skip to content

It is awkward to check whether an obj is of a particular heap type (defined in an extension module) #133161

Open
@iritkatriel

Description

@iritkatriel

I wanted to check whether an object is of type array and ended up doing this:

    PyObject *exc = PyErr_GetRaisedException();
    PyObject *module = PyType_GetModuleByDef(Py_TYPE(lhs), &arraymodule);
    if (module == NULL) {
        if (!PyErr_Occurred() || PyErr_ExceptionMatches(PyExc_TypeError)) {
            /* lhs is not an array instance - ignore the TypeError (if any) */
            PyErr_SetRaisedException(exc);
            return 0;
        }
        else {
            _PyErr_ChainExceptions1(exc);
            return -1;
        } 
    }   
    PyErr_SetRaisedException(exc);
    return array_Check(lhs, get_array_state(module));

CC @ericsnowcurrently @encukou

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions