Skip to main content

Python/bltinmodule.c

Source:

cpython 3.14 @ ab2d84fe1023/Python/bltinmodule.c

bltinmodule.c implements the builtins module. Every Python program has implicit access to these functions. Performance matters here: len, range, map, filter, enumerate, and sorted are called millions of times in typical code.

Map

LinesSymbolRole
1-200print, inputTerminal I/O
201-500len, hash, id, callable, repr, str, bytes, int, float, bool, complexType coercion and info
501-800range, map, filter, zip, enumerateLazy iteration
801-1100sorted, reversed, iter, next, sum, min, max, any, allAggregation
1101-1400openFile I/O entry point
1401-1700eval, exec, compile, globals, locals, varsCode execution
1701-2000isinstance, issubclass, typeType checking
2001-2500getattr, setattr, delattr, hasattr, dirAttribute access
2501-3500staticmethod, classmethod, property, superDescriptors

Reading

len

// CPython: Python/bltinmodule.c:1520 builtin_len
static PyObject *
builtin_len(PyObject *module, PyObject *v)
{
Py_ssize_t res = PyObject_Size(v);
if (res < 0 && PyErr_Occurred())
return NULL;
return PyLong_FromSsize_t(res);
}

PyObject_Size calls tp_as_sequence->sq_length or tp_as_mapping->mp_length.

sorted

// CPython: Python/bltinmodule.c:2140 builtin_sorted
static PyObject *
builtin_sorted(PyObject *self, PyObject *const *args, Py_ssize_t nargs,
PyObject *kwnames)
{
PyObject *newlist = PySequence_List(iterable);
if (PyList_Sort(newlist) < 0) {
Py_DECREF(newlist);
return NULL;
}
return newlist;
}

sorted converts the iterable to a list, then sorts in-place. The key and reverse keyword arguments are passed through to list.sort.

eval

// CPython: Python/bltinmodule.c:1020 builtin_eval_impl
static PyObject *
builtin_eval_impl(PyObject *module, PyObject *source, PyObject *globals,
PyObject *locals)
{
if (PyUnicode_Check(source)) {
PyObject *code = Py_CompileStringExFlags(source_str, "<string>",
Py_eval_input, ...);
return PyEval_EvalCode(code, globals, locals);
}
/* If source is already a code object, evaluate directly */
return PyEval_EvalCode(source, globals, locals);
}

eval(expr) compiles expr to a code object in Py_eval_input mode (single expression) and evaluates it.

isinstance with multiple types

// CPython: Python/bltinmodule.c:2350 abstract_issubclass
static int
abstract_issubclass(PyObject *derived, PyObject *cls)
{
/* Handle tuple of types: isinstance(x, (int, str)) */
if (PyTuple_Check(cls)) {
Py_ssize_t i, n = PyTuple_GET_SIZE(cls);
for (i = 0; i < n; i++) {
if (abstract_issubclass(derived, PyTuple_GET_ITEM(cls, i)))
return 1;
}
return 0;
}
return PyType_IsSubtype((PyTypeObject *)derived, (PyTypeObject *)cls);
}

getattr with default

// CPython: Python/bltinmodule.c:1390 builtin_getattr
/*
* getattr(obj, name) -> obj.name, raises AttributeError if missing
* getattr(obj, name, default) -> default if AttributeError
*/

gopy notes

All built-in functions are in module/builtins/. len, isinstance, getattr, setattr, and hasattr are performance-critical and are called from within the eval loop itself. eval and exec require the compiler to be accessible at runtime (compile/ package). sorted and min/max with key= require calling back into Python for each comparison.