Difference in PyList_Insert() and list_insert_impl()

hi, everyone.
In Cpython3.7.12+ source code, I notice that they have two insert() func, int PyList_Insert(PyObject *op, Py_ssize_t where, PyObject *newitem) and static PyObject * list_insert_impl(PyListObject *self, Py_ssize_t index, PyObject *object). By test, I know that when i use lis.insert(1, 1), python will call list_insert_impl(), so i want to know when Python calls PyList_Insert(), and difference in PyList_insert() and list_insert_impl()

PyList_Insert() is a function in Python’s C API for list objects, which is also part of the stable API. Note the use of PyAPI_FUNC in its declaration in “Include/listobject.h”. PyAPI_FUNC is defined in “Include/pyport.h” and depends on Py_EXPORTED_SYMBOL, which is defined in “Include/exports.h”.

list_insert_impl() is internal to the implementation of the list type. Note its static linkage declared in “Objects/clinic/listobject.c.h”.

1 Like

Just as a quick terminological clarification, PyList_Insert() is part of the Limited API, which is exposed in the Stable ABI. And just to be clear, in general its a good idea to always strive to conform to the Limited API where possible, as it ensures your extensions will be usable with the widest number of different Python versions, and minimize the changes you will need to make to support compiling with new versions.

1 Like