C Extensions Architecture & The Python C-API
Native C extensions link directly against the CPython runtime header `<Python.h>`. Functions operate on raw `PyObject*` pointers, unpack parameters via `PyArg_ParseTuple`, and register dispatch tables via `PyMethodDef` ⚙️.
import sysconfig
# Python development headers path:
inc_dir = sysconfig.get_path("include")
print(f"Python C-API Headers: {inc_dir} ⚙️")
# Minimal C function signature:
# PyObject* my_func(PyObject* self, PyObject* args);
# 96. C Extensions Architecture & The Python C-API Native C extensions link directly against the CPython runtime header `<Python.h>`. Functions operate on raw `PyObject*` pointers, unpack parameters via `PyArg_ParseTuple`, and register dispatch tables via `PyMethodDef` ⚙️. ```python import sysconfig # Python development headers path: inc_dir = sysconfig.get_path("include") print(f"Python C-API Headers: {inc_dir} ⚙️") # Minimal C function signature: # PyObject* my_func(PyObject* self, PyObject* args); ``` When writing native extensions, developers must scrupulously manage reference counts (`Py_INCREF` and `Py_DECREF`). Leaking a reference leaks RAM; decref-ing too early triggers catastrophic segmentation faults 💥! --- ### Question What primary header file provides the CPython runtime object models, macros, and API definitions? 🧠 - [ ] a. `<Python.h>`, providing all PyObject definitions, reference counting macros, and memory allocators ⚙️ - [ ] b. `<unistd.h>`, providing POSIX process management hooks - [ ] c. `<cpython_core.h>`, providing bytecode evaluation routines - [ ] d. `<glibc_python.h>`, providing Linux standard library bridges --- ### Challenge Inspect `sysconfig.get_path('include')`. Print `True` if the path contains `'python'`. 🎯 <details> <summary>Solution</summary> ```python import sysconfig inc = sysconfig.get_path('include') print('python' in inc.lower()) ``` </details>