Is there a way to check for the PyType of Python AST objects from C code?

  abstract-syntax-tree, c++, python

I am attempting to write a Python C extension that performs work on a Python AST which is passed from Python code. However, I’m having some trouble checking the PyType of the passed object.

I have the following method, registered as METH_O, compiled into a Python module:

#include <Python.h>
// Python-ast.h defines PyAST_Check
#include <Python-ast.h>

static PyObject * python_is_ast(PyObject *Py_UNUSED(self), PyObject *obj) {
    if (PyAST_Check(obj)) Py_RETURN_TRUE;
    else Py_RETURN_FALSE;
}

If I try to import this code using Python 3.9.6, I get the error

ImportError: /home/user/path/build/lib.linux-x86_64-3.9/_module.cpython-39-x86_64-linux-gnu.so: undefined symbol: PyAST_Check

I would think that PyAST_Check would need to be available in the interpreter, but apparently it isn’t exported. Is there a way to check for the PyType of Python AST objects from C code?

Source: Python Questions

LEAVE A COMMENT