python / python/cpython

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

Open
#133,161 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic-C-API type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the C API calls shown in the issue, especially PyType_GetModuleByDef and the array_Check example, to understand the current type-checking path. Review related CPython C API conventions and determine a concrete, broadly applicable way to check heap types; done means an agreed API direction with appropriate documentation and tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, python
Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.