Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Changelog

## Unreleased
## v2.3.0

### Added

- At the end of a build, the python modules relying on external modules are imported and smoke tested in the built python: the build fails if one that the build compiled (or listed in the new `cpython-required-modules` setting) is absent, broken, or reports another library version than the one compiled. Modules relying on libraries found on the build host are reported with a warning. See [module checks](docs/concepts/module-checks.md)
- `inspect` smoke tests modules as well, and marks them in its report: ✅ passed, ❌ failed (module imports, but is broken), ⚠️ reports another library version than the one compiled. For a python built by portable-python, it fails if a module relying on an external module that was compiled (or a required module) is not working, as recorded in its `.manifest.yml`
- `.manifest.yml` records compiled sub-modules (such as `ncurses`, with `readline`) in `static`, and the new `required-modules`

### Changed

Expand Down
2 changes: 1 addition & 1 deletion docs/architecture/cpython.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ After `make install`, a portable build's interpreter still thinks it lives at th
- **Trim** — remove tests / idle / 2to3 before byte-compiling, then prune seldom-used pycaches after (driven by the `cpython-clean-*` config).
- **Byte-compile** the standard library.
- **Relativize** — rewrite `bin/` shebangs and the absolute paths baked into `sysconfig` so everything resolves relative to the install's own location.
- **Verify** — sanity-check that `venv` works in the finished build.
- **Verify** — sanity-check that `venv` works in the finished build, and [check the python modules](/concepts/module-checks.md) relying on external modules.
4 changes: 3 additions & 1 deletion docs/cli/inspect.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ type: CLI Command
title: inspect
description: Inspect a python installation for non-portable dynamic library usage, reporting any references that would break portability.
tags: [cli, inspect, portability, command]
timestamp: 2026-06-23T00:00:00Z
timestamp: 2026-12-03T00:00:00Z
---

# inspect
Expand All @@ -17,6 +17,8 @@ portable-python inspect [OPTIONS] PATH
Worth knowing (`--help` for the full flag list):

- It **exits non-zero** when it finds a portability problem, so it works as a CI gate.
- Modules are [smoke tested](/concepts/module-checks.md): ✅ passed, ❌ failed, ⚠️ another library version than the one compiled, no marker when there's no smoke test. For a python built by portable-python (with a build-info manifest), modules relying on what the build compiled must work, otherwise `inspect` exits non-zero.
- `--modules/-m` **replaces** the list of modules to inspect (`-m tkinter` shows only `tkinter`, and skips the `.so` check), a leading `+` adds to the default list instead (`-m +tkinter`).
- `PATH` can be the special value `invoker` to inspect the python running the CLI itself.
- `--prefix` says the build was pinned to a prefix (not portable), relaxing the check; `--verbose/-v` shows the full `.so` report.

Expand Down
1 change: 1 addition & 0 deletions docs/concepts/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The domain ideas behind portable-python. Read these to understand *why* the buil
* [Portability](/concepts/portability.md) - What "portable" means here, and how it is validated.
* [Static linking](/concepts/static-linking.md) - Why dependencies are compiled statically and paths relativized.
* [Telltale detection](/concepts/telltale-detection.md) - How the tool decides whether a library must be compiled or already exists.
* [Module checks](/concepts/module-checks.md) - How the python modules of a fresh build are checked: strictly for what the build provides, with a warning for the rest.
* [Folder masking](/concepts/folder-masking.md) - The macOS `/usr/local` RAM-disk mask that prevents accidental dynamic linking.
* [Build layout](/concepts/build-layout.md) - The `build/` and `dist/` folder structure shared by every component.
* [ppp-marker prefix](/concepts/ppp-marker.md) - The placeholder install prefix that fills in for `--prefix` and is relativized for portable builds.
39 changes: 39 additions & 0 deletions docs/concepts/module-checks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
type: Concept
title: Module Checks
description: After a build, the python modules relying on external modules are imported and smoke tested in the freshly built python, strictly for what the build was asked to provide, with a warning for the rest.
tags: [concept, validation, modules, finalize]
timestamp: 2026-12-03T00:00:00Z
---

# Module Checks

A C extension that fails to build or link doesn't fail the CPython build: `make` only mentions it in its output (such as "could not be imported"), and the module is silently missing from the result. To catch this, at the end of each build (during [`Cpython`](/architecture/cpython.md) finalization), the python modules relying on [external modules](/modules/index.md) are imported and smoke tested in the freshly built python.

## What is checked, and how strictly

Each external module declares which python modules rely on it, via its `m_provides` attribute (for example `openssl` provides `_ssl _hashlib`, `xz` provides `_lzma`). How strictly they're checked depends on how the build provided that external module (its outcome, as shown by [`build-report`](/cli/build-report.md)):

| Outcome | Meaning | Check |
|---------|---------|-------|
| `static` | Compiled by this build (selected, or auto-selected) | **Strict**: the build fails if the module is absent, broken, or reports a different library version than the one compiled |
| `shared` | Found on the build host ([telltale](/concepts/telltale-detection.md) present) | **Warning** only: a header on the build host doesn't guarantee that CPython's `./configure` can use the library (for example, the macOS SDK has `tk.h`, but `_tkinter` doesn't get built with it) |
| `absent` | Neither | Not checked |

Sub-modules (such as `ncurses`, under `readline`) share the outcome of their parent.

Python modules listed in the `cpython-required-modules` [config](/configuration/portable-python-yml.md) setting are checked strictly as well, whatever their outcome. This lets a build state what it can't do without (for example `_uuid` or `readline`), even when provided by the build host.

## What a check does

For each checked module, inside the built python:

- **Import** the module.
- **Smoke test** it, beyond importing: for example a compression round-trip for `zlib` / `_lzma` / `_bz2` / `_zstd`, `ssl.create_default_context()`, an in-memory `sqlite3` query, a `ctypes` callback (exercising libffi), a `dbm` write and read, a Tcl expression for `_tkinter`. Modules without a smoke test (such as `readline`, `_curses`, which need a terminal) are checked via import only. The smoke tests are in `SMOKE_TESTS` in `external/_inspect.py`.
- **Version**, for statically compiled modules that report the version of their C library (such as `_ssl`'s `OPENSSL_VERSION`): it must match the compiled version. A mismatch means the module got linked against another copy of the library (for example one from the build host).

## With `inspect`

The [`inspect`](/cli/inspect.md) command runs the same smoke tests on any python installation, and marks each module in its report: ✅ smoke test passed, ❌ failed (module imports, but is broken), ⚠️ reports another library version than the one compiled (with the expected version), no marker when the module has no smoke test (or is absent).

When the inspected python was built by portable-python, its build-info [manifest](/configuration/portable-python-yml.md) (`.manifest.yml` by default) records what the build compiled (`static`: external modules with their versions, sub-modules included) and what it required (`required-modules`). `inspect` uses it to check the same modules strictly as the build did, and fails if one of them is not working. Other modules are only reported. Manifests written prior to v2.3.0 don't record sub-modules and required modules: modules relying on sub-modules are then checked without version comparison.
2 changes: 1 addition & 1 deletion docs/concepts/portability.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ See the [`build`](/cli/build.md) command for how to choose.

## Validation

Portability is not assumed — it is **verified**. The [`PythonInspector`](/architecture/python-inspector.md) walks every executable and `.so` file, parses their dynamic dependencies (`ldd` on Linux, `otool` on macOS), and classifies each reference as a system library, a relative reference, or a problem. The [`inspect`](/cli/inspect.md) command surfaces this report and fails if a non-portable reference is found.
Portability is not assumed — it is **verified**. The [`PythonInspector`](/architecture/python-inspector.md) walks every executable and `.so` file, parses their dynamic dependencies (`ldd` on Linux, `otool` on macOS), and classifies each reference as a system library, a relative reference, or a problem. The [`inspect`](/cli/inspect.md) command surfaces this report and fails if a non-portable reference is found. A build also [checks its python modules](/concepts/module-checks.md).
3 changes: 2 additions & 1 deletion docs/configuration/portable-python-yml.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ portable-python is configured via a YAML file — by default `portable-python.ym
| `cpython-clean` | list | Extra globs removed in the second cleanup pass, along with `cpython-clean-2nd-pass`. |
| `cpython-symlink` | list | Files to ensure exist in the install (default: `bin/python`), symlinked to the matching main file (eg: `bin/python3.14`). |
| `cpython-validate-script` | path | Python script run with the freshly built python before packaging, the build fails if it fails. Relative to the config file that sets it. |
| `cpython-required-modules` | CSV/list | Python modules that must import and pass their smoke test, whatever their outcome (see [module checks](/concepts/module-checks.md)). |
| `cpython-check-venvs` | string | Check that the built python can create a venv: `venv` (default), `copies` (`--copies` venv), `all` (both), or `""` to skip. |
| `cpython-pep668-externally-managed` | map | Mark the install as [externally managed](https://peps.python.org/pep-0668/): its keys are written to `EXTERNALLY-MANAGED` (an `Error` key is required), and `bin/pip*` are removed. |
| `allowed-system-libs` | regex | Opt-in: fail a portable build (no `--prefix`, no `--enable-shared`) if its `.so` files link a system library whose path doesn't match. Also used by `inspect` (unless `--prefix`). |
Expand Down Expand Up @@ -75,7 +76,7 @@ Files written in the install folder (relative paths), set a key to `""` to not w

| Key | Default | Content |
|-----|---------|---------|
| `build-info` | `.manifest.yml` | How the build was done: cpython version and source, selected modules, `./configure` args, compilation info (date, host, `compiled-by`), and `additional-info`. |
| `build-info` | `.manifest.yml` | How the build was done: cpython version and source, compiled external modules (`static`, with their versions, sub-modules included), `required-modules`, `./configure` args, compilation info (date, host, `compiled-by`), and `additional-info`. Used by [`inspect`](/cli/inspect.md) to check modules strictly. |
| `inspection-report` | `.inspection-report.yml` | Report of the built python's modules and `.so` files (same as the `inspect` command). |
| `additional-info` | | Map of extra info to record in `build-info`, a value of the form `$NAME` is expanded from env var `NAME`. |

Expand Down
2 changes: 1 addition & 1 deletion docs/guides/add-an-external-module.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ To statically link a new C library into CPython, add a [`ModuleBuilder`](/archit
self.run_make("install")
```

2. **Set the declarative attributes**: `m_telltale` (see [telltale detection](/concepts/telltale-detection.md)), `m_debian` with the right sigil, and `m_include`/`m_build_cwd` if needed.
2. **Set the declarative attributes**: `m_telltale` (see [telltale detection](/concepts/telltale-detection.md)), `m_debian` with the right sigil, `m_provides` (the python modules relying on it, see [module checks](/concepts/module-checks.md)), and `m_include`/`m_build_cwd` if needed. If those python modules can be exercised beyond importing them, add a smoke test to `SMOKE_TESTS` in `external/_inspect.py`.

3. **Implement source resolution**: `url` and `version` (using `cfg_url`/`cfg_version` so config overrides work).

Expand Down
3 changes: 3 additions & 0 deletions docs/log.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## 2026-12-03

* **Update**: [Module checks](/concepts/module-checks.md) and [configuration](/configuration/portable-python-yml.md): the build-info manifest records compiled sub-modules and `required-modules`, used by `inspect`.
* **Update**: [Module checks](/concepts/module-checks.md) and [inspect](/cli/inspect.md): smoke test markers in the `inspect` report, strict checks for pythons built by portable-python (via their manifest), `-m` replaces the default module list.
* **Creation**: Added [module checks](/concepts/module-checks.md): python modules of a fresh build are smoke tested, strictly for what the build provides (`m_provides`, `cpython-required-modules`), with a warning for the rest.
* **Update**: [Folder masking](/concepts/folder-masking.md): `isolate-usr-local` defaults to `auto` on macOS, which masks `/usr/local` only when Homebrew is installed there, and `none` turns isolation off.
* **Update**: [Configuration](/configuration/portable-python-yml.md): documented the remaining config keys (`cpython-clean`, `cpython-symlink`, `cpython-validate-script`, `cpython-check-venvs`, `cpython-pep668-externally-managed`, `allowed-system-libs`, `isolate-usr-local`, `env`, `manifest`, `compiled-by`) and the `-{platform}` suffix of the cleanup globs.
* **Update**: [Folder masking](/concepts/folder-masking.md): masking is opt-in via `isolate-usr-local`, and covers `/usr/local/{etc,include,lib,opt}`.
Expand Down
1 change: 1 addition & 0 deletions src/portable_python/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ class ModuleBuilder:
m_build_cwd: str | None = None # Optional: relative (to unpacked source) folder where to run configure/make from
m_debian: str | None = None # Optional: debian package, prefixed with "!": required, "+": required if selected, "-": prevents static
m_include: str | None = None # Optional: subfolder to automatically list in CPATH when this module is active
m_provides: str = "" # Optional: python modules relying on this external module (space separated), checked post-build
m_telltale: str = "" # Optional: file(s) that, if present, indicate this module is installed (space separated)

setup: BuildSetup
Expand Down
13 changes: 13 additions & 0 deletions src/portable_python/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from runez.render import PrettyTable

from portable_python import BuildSetup, PPG, required_python_spec
from portable_python.cpython import Cpython, ModuleExpectation
from portable_python.inspector import LibAutoCorrect, PythonInspector

LOG = logging.getLogger(__name__)
Expand Down Expand Up @@ -78,12 +79,24 @@ def inspect(modules, verbose, prefix, skip_so, path):

inspector = PythonInspector(path, modules=modules)
runez.abort_if(inspector.python.problem, "%s: %s" % (runez.red(path), inspector.python.problem))
# For a python built by portable-python: modules relying on external modules it compiled must work (and required ones)
expectations = Cpython.manifest_expectations(inspector.install_folder)
additional = sorted(x for x in expectations if x not in PythonInspector.default.split(","))
if additional and not modules:
inspector = PythonInspector(path, modules="+%s" % ",".join(additional))

ModuleExpectation.annotate(expectations, inspector.module_info)
print(runez.blue(inspector.python))
print(inspector.represented(verbose=verbose))
if not skip_so and (not modules or modules == "all"):
problem = inspector.full_so_report.get_problem(portable=not prefix)
runez.abort_if(problem)

if inspector.module_info:
expectations = {k: v for k, v in expectations.items() if k in inspector.module_info}
failed = ModuleExpectation.failures(expectations, inspector.module_info)
runez.abort_if(failed, "Modules not working as expected:\n %s" % "\n ".join(failed))


@main.command(name="list")
@click.option("--json", is_flag=True, help="Json output")
Expand Down
Loading