Skip to content

ADR 0056: An extension module exports its public functions

Status: Final, landed 2026-09-30 Type: Feature Created: 2026-09-30 Related-ADRs: 0006 (the callback wrapper this reuses), 0027 (the list layout, and why a list cannot cross), 0053 (the kernels this makes callable)

Context

--ext-module registered each public ufunc with NumPy and nothing else. Any other compiled function could be called through the pp_* C ABI or handed to CPython as a callback. An ordinary Python project could not import it. The roadmap's 0.2 release ("the kernel flagship") names that as an open item: compiled functions importable from ordinary Python code (frozen plan P4.4).

Most of the mechanism existed. A compiled function that the program hands to CPython (sorted(xs, key=f), a Flask route) already gets a PyCFunction wrapper (pyexport.py): arguments converted by declared type with CPython's argument rules, keywords accepted, a POST exception raised as the Python one, one cached function object per function.

Decision

Every public function of the entry translation unit is a module attribute, with the namespace rules ufunc registration already had: imports under their local names, module-level aliases under theirs, _-prefixed names private, a declared __all__ narrowing the set. A ufunc stays a numpy.ufunc; everything else is marked as a Python export of the module defining it. The build then emits and links the callback wrapper, and PyInit adds the cached object. module.f is therefore the same object compiled code hands CPython for f. A module with no ufunc includes and imports no NumPy.

A function whose signature cannot cross fails the build, naming it. Leaving it out of the module would turn a build decision into an AttributeError at import time. The message says how to keep it internal (_, or __all__).

list[T] parameters are refused. A CPython list and a compiled list are different objects (0027), so crossing would be a copy. A copy hides from the caller each change the function makes: square(x, out) would return with out untouched, where the interpreted function fills it. Sharing the caller's list would need the runtime to adopt CPython's list layout, which is not a 0.2 change.

Array[T] parameters cross as the caller's buffer, through the buffer protocol: a NumPy array (or an array.array, or anything exporting a buffer) is borrowed for the call, so the function's writes are the caller's. The compiled function trusts its declaration, so the wrapper checks what the buffer protocol does not. A mismatch is a TypeError at the call:

  • the element kind and size (int64 into Array[Float64] is refused);
  • the rank: the declared shape's, or one for an unshaped Array[T], which is indexed a[i]; a two-dimensional parameter says so with Shape[None, None];
  • the static dimensions a shape declares;
  • the layout: a C-order parameter (the default) computes its strides from its shape, so it asks for a C-contiguous buffer and a strided view like x[::2] is refused; FOrder asks for F-contiguous; only an explicit Strides layout takes any strides;
  • writability: a function that stores into the array, or passes it to a call, asks for a writable buffer, so a read-only array is refused up front.

Consequences

  • A kernel written for the C ABI is importable with no glue: ppc build kernels.py --ext-module, then kernels.total(np_array).
  • Callbacks gain array parameters too, since the wrapper is shared.
  • Refusals that remain: list, dict and set parameters, default parameter values (the wrapper does not hold the constant yet), complex arrays, output parameters, and pygo programs (extension modules link no pygo runtime yet).
  • The rank of an unshaped Array[T] is fixed at one. A function that indexes one with two subscripts needs its shape declared to be callable from Python; without one, it is refused.
  • tests/b_integration/test_ext_callables.py holds all of this from CPython's side.