PyO3 crates

The wrapper crate RustCall builds around a PyO3 extension module so that its #[pyfunction]s and #[pymethods] can be called from Julia, the link plan that finds the Python runtime, the skip reasons a scan reports, and the Python host that binds a crate as the extension it already is. See PyO3 Crates for the user-facing guide.

PyO3 wrapper crates (src/pyo3/pyo3.jl)

RustCall.CRATES_IO_SOURCES — Constant
CRATES_IO_SOURCES

The source spellings Cargo uses for crates.io, the one registry a generated dependency can name with a bare version requirement.

Cargo reports the registry protocol it used, and the sparse protocol has been the default since 1.70, so both spellings occur.

Compared for equality, never as a prefix: a custom registry whose URL merely begins with one of these — ...crates.io-index-mirror — would otherwise be taken for crates.io and aliased by bare version, which is the second-instance failure the refusal exists to prevent (#392 review).

source
RustCall.PYO3_DISPATCHER_MINIMUM — Constant
PYO3_DISPATCHER_MINIMUM

The oldest PyO3 a dispatcher-using wrapper can be generated against (#370).

A wrapper that reaches Python — a defaulted callable, or a class made Python-owned by inheritance — is emitted with Python::initialize and Python::attach. Those names arrived in PyO3 0.26; 0.25 and earlier spell the same operations pyo3::prepare_freethreaded_python and Python::with_gil, so the generated source does not compile against them. Determined by reading the marker.rs of 0.24, 0.25 and 0.26 rather than from the changelog.

Everything else RustCall generates for a PyO3 crate calls the crate's own functions and is unaffected, which is why the floor is checked only where the dispatcher is actually used.

source
RustCall.PYO3_ERROR_CODE — Constant
PYO3_ERROR_CODE

The i32 a lowered PyResult carries in the Err slot (rustcall_julia_core::wrap::PYERR_CODE). It is the only value the wrapper produces, so the Julia side never decodes it — it reports PYO3_OPAQUE_ERROR instead — but the number is part of the ABI and is written down here.

source
RustCall.PYO3_OPAQUE_ERROR — Constant
PYO3_OPAQUE_ERROR

The error a lowered PyResult reports. It is fixed, and deliberately so.

Creating and dropping a PyErr without a Python interpreter is safe; rendering one is not — Display/Debug on a PyErr asserts inside pyo3 that the interpreter is initialized, and the resulting panic crossing extern "C" aborts the process. Reading only the exception type would still need a Python token, which a wrapper crate by definition does not have. So the generated wrapper drops the PyErr and reports the fixed code rustcall_julia_core::wrap::PYERR_CODE, and this is the sentence Julia raises for it.

Must equal rustcall_julia_core::wrap::PYERR_MESSAGE; test/test_pyo3_wrapper.jl checks that it does.

source
RustCall.PYO3_SKIP_REASONS — Constant
PYO3_SKIP_REASONS

Human-readable text for each skip_reason the extractor records (rustcall_julia_core::manifest::skip_reason). A reason may carry a detail after a colon (pyo3_type:Python<'_>); pyo3_skip_explanation splits it off.

source
RustCall.WRAPPER_RESERVED_CRATE_NAMES — Constant
WRAPPER_RESERVED_CRATE_NAMES

The crate names a generated wrapper spends on itself: rustcall_pyo3 is the alias for the PyO3 the target crate resolved, and rustcall_julia_macros is the runtime the quiet-panic guard comes from. Both are written into the wrapper's Cargo.toml and named literally by the generated Rust.

Nothing reserves a crate name — the crate being wrapped is the user's — so a target crate may legitimately be called either of these. See wrapper_target_identifier.

source
RustCall.PyO3LinkPlan — Type
PyO3LinkPlan

One candidate build of a wrapper crate against a PyO3 crate: which Cargo features it uses, whether the resulting cdylib can be linked and loaded, and the configuration to scan the crate under (#275 Phase 1.5).

Fields

  • mode::Symbol: one of

    modemeaning
    :python_freepyo3 is not in the resolved dependency graph of this build, so nothing links libpython
    :link_libpythonpyo3 is resolved: the wrapper cdylib hard-links libpython and only loads if the interpreter's library directory is on the runtime search path
    :unlinkablepyo3's resolved features include extension-module: the wrapper cdylib cannot be loaded (it does not link on macOS, and fails dlopen on Linux)
  • feature_flags::Vector{String}: the Cargo flags that select this build ([], ["--no-default-features"], ["--all-features"]).

  • crate_features::Vector{String}: the root package's features as Cargo resolved them under those flags.

  • pyo3_features::Vector{String}: pyo3's resolved features, empty when pyo3 is not in the graph. extension-module here is what makes a build :unlinkable.

  • dependency_default_features::Bool: what the wrapper's [dependencies.<crate>] entry must set. false is how a target crate's default features are switched off — the cargo build --no-default-features flag applies to the package being built, i.e. the wrapper, and does not reach a dependency's defaults.

  • cfg_text::String: rustc --print cfg for this build, or "" when Cargo could not answer. scan_report hands it to the extractor so #[cfg] and #[cfg_attr] are evaluated by the real evaluator and the manifest contains exactly the items this build has.

  • interpreter::String: the interpreter a :link_libpython build pins PYO3_PYTHON to, decided together with rpath by python_link_source so the two never name different Pythons; "" when none could be identified (pyo3 then picks for itself). Part of the wrapper's artifact identity.

  • interpreter_config::String: what that interpreter is — implementation, version, ABI tag, the library it links, its LIBDIR, pointer width — as one line the interpreter itself reported (_python_interpreter_fingerprint). Also part of the identity, so a Python upgraded in place behind the same path, or a retargeted symlink, is a different wrapper rather than a stale cache hit (#307 review; the analogue of artifact_compiler_identity for rustc).

  • runtime_libraries::Vector{String}: the libraries the wrapper imports by name that the loader would not find on its own, opened before it when the generated module loads — on Windows, which has no rpath, the interpreter's own python3xy.dll (_python_runtime_libraries); empty elsewhere.

  • resolved::Bool: whether Cargo answered. false means the plan is the conservative reading of Cargo.toml described below.

  • reason::String: why this mode was chosen, in words.

  • build_inputs::Vector{String}: target-package paths declared by its build script with rerun-if-changed, resolved against the package root.

Cargo resolves the features, RustCall does not

Feature activation is transitive, target-dependent, and renameable (python = { package = "pyo3" }), and a #[cfg] predicate has Boolean structure (all, any, not). Reimplementing any of that in Julia gets it wrong in a new way for every crate shape, so the plan asks Cargo instead: cargo tree -e features gives the resolved feature set of the root package and of pyo3 itself, and cargo rustc -- --print cfg gives the configuration the crate scan then runs under, in strict mode.

When Cargo cannot answer — it is not installed, or the crate does not resolve — the plan falls back to a deliberately conservative read of Cargo.toml that does no resolution at all: any pyo3 declaration listing extension-module is :unlinkable, any other pyo3 declaration is :link_libpython, and only a crate with no pyo3 declaration at all is :python_free. resolved is false and reason says so.

Why :link_libpython exists

Disabling extension-module is not enough to get a Python-free build: verified in the #275 MWE, any build whose graph contains pyo3 links libpython, and the resulting cdylib fails to dlopen unless the loader can find it. The only genuinely Python-free build is one in which pyo3 is not resolved at all.

Why "optional pyo3" is rare

#[cfg_attr(feature = "python", ...)] works for the item attributes (pyfunction, pyclass, pymethods) but not for the inner ones (new, staticmethod, getter, setter, pyo3(get, set)): the outer macro runs before cfg_attr expands, so rustc reports cannot find attribute 'new' in this scope. Real crates therefore make pyo3 mandatory, which makes :link_libpython the common case rather than the exception.

source
RustCall.PyO3Wrapper — Type
PyO3Wrapper

A built wrapper crate for a PyO3 crate that carries no RustCall attribute (#275 Phase 2).

  • info: a CrateInfo whose julia_functions / julia_structs are the items the wrapper exports, so every existing emitter binds them exactly as it binds #[julia] ones;
  • plan: the PyO3LinkPlan the wrapper was built under;
  • source: the generated lib.rs and the manifest that describes it;
  • lib_path: the compiled cdylib;
  • lib_name: the RUST_LIBRARIES key, which includes the feature set, so two builds of one crate under different features do not clobber each other;
  • skipped: the PyO3 items that were not wrapped, each with its reason.
source
RustCall._build_pyo3_wrapper_project — Method
_build_pyo3_wrapper_project(info, plan, source, rustflags, release, key, cache_enabled) -> String

Write the generated wrapper crate to a directory of its own under crate_target_directory(crate, :pyo3_wrapper) — RustCall's cache, never the crate's own target/ (#486) — build it from the crate's directory, and return a path to the result that outlives that directory.

The wrapper is a Cargo root of its own, and Cargo gives a root three things it does not give a dependency: .cargo/config.toml discovery (from the working directory upwards), the lockfile, and the [patch] table. A wrapper written to an unrelated temporary directory therefore resolved and compiled the crate differently from the crate's own build — no config, a fresh resolution, no overrides. _wrapper_shaped_project seeds it with the crate's Cargo.lock and [patch], and Cargo runs in the crate's directory with --manifest-path naming the wrapper, so the three apply as they do to the crate itself (#307 review, #486).

A generated build.rs carries the plan's link options (_pyo3_wrapper_build_script); PYO3_PYTHON pins the interpreter pyo3's build script probes to the one whose library directory that script put on the search path, so the cdylib cannot end up linked against one Python and pointed at another.

The build directory is temporary and removed as soon as the build finishes, so the library is copied out before the cleanup — into the artifact cache under key when caching is on, and otherwise into a directory of its own. Returning Cargo's own output path here left load_artifact! opening a file that had already been deleted.

source
RustCall._cargo_resolved_features — Method
_cargo_resolved_features(crate_path; features, default_features) -> Union{Tuple, Nothing}

(crate_features, pyo3_features, pyo3_active) as Cargo resolves them for a build of crate_path as a dependency of the generated wrapper, from

cargo tree -e features,normal --prefix none --format "{p}|{f}" --no-dedupe -p <target>

which prints one line per package with its resolved feature list — the transitive closure, with target-specific tables and renamed dependencies already applied.

The command runs in a wrapper-shaped edition-2021 root with the requested features on its target dependency, and the target workspace's lockfile and root overrides. Running in the target root instead can use resolver 1 and unify build-dependency features that the actual wrapper keeps separate.

The edge filter is features,normal: a wrapper crate depends on the target crate's library, so its dev- and build-dependencies are not in the graph it builds. Without the filter a crate whose dev-dependency on pyo3 enables extension-module would be reported :unlinkable for a build that never activates it.

nothing when the command fails (no cargo, an unresolvable crate, no network for a fresh registry).

source
RustCall._declaration_manifest — Method
_declaration_manifest(crate_path) -> Dict

The crate's parsed Cargo.toml with { workspace = true } dependency inheritance expanded from the workspace root's [workspace.dependencies] table.

Cargo expands that inheritance before it resolves a build, and the declaration-only plan must see the same table: with the unexpanded member manifest, a dependency inherited under an alias (the root writes [workspace.dependencies.python] package = "pyo3" and the member python = { workspace = true }) has no local package field, looks like an unknown crate, and the plan wrongly says :python_free (#425 review). Pure manifest reads: no Cargo.

source
RustCall._git_dependency_keys — Method
_git_dependency_keys(source) -> Vector{String}

The git = ... dependency keys that reproduce Cargo's git source string exactly, selector included.

A git source is git+<url>[?<selector>]#<commit>, and the selector is part of the package's identity: git+URL?branch=main and git+URL?rev=SHA are two different sources to Cargo even when they resolve to the same commit. So the selector is copied as it stands and the commit is not turned into a rev — pinning that way would create the second pyo3 instance this is here to avoid (#392 review). The commit does not need repeating: the wrapper is seeded with the target crate's own Cargo.lock (_wrapper_shaped_project), which pins it.

source
RustCall._linkable_lib_target — Method
_linkable_lib_target(cargo_toml) -> Bool

Whether a dependent crate can link this one as a Rust library: its [lib] crate-type is absent (Cargo's default is lib, i.e. an rlib) or lists lib, rlib or dylib. A ["cdylib"], ["staticlib"] or ["proc-macro"]-only library has no target a path dependency can use, and Cargo builds the dependent with a "provides no linkable target" warning followed by an unresolved-crate error.

source
RustCall._new_shaped_project_dir — Method
_new_shaped_project_dir(parent) -> String

A unique, not-yet-created project_<pid>_<random> path under parent. The directory is made only after its lease is published (_wrapper_shaped_project), so no sweep can see an unclaimed project.

source
RustCall._publish_shaped_project_lease — Method
_publish_shaped_project_lease(parent, dir) -> Union{IOStream, Nothing}

Take the claim on dir before it exists: create a staging lease under parent, lock it, then rename it to generation_lease_path(dir). The rename is atomic and the file is already locked, so <dir>.lease is never visible unlocked — the sweep asks the lock, never a timestamp (#437).

Returns the open, locked stream, or nothing where there is no lock to publish (Windows, whose machine-wide process table decides instead, a file system without advisory locking, or a locking error); the sweep then falls back to the pid in the name. Only a lease this call actually holds is published: a stream left unlocked — a transient lock failure, not a file system without locking — would let a later sweep take a still-active project for abandoned (#437 review).

source
RustCall._pyo3_alias_toml — Method
_pyo3_alias_toml(dependency) -> Vector{String}

The [dependencies.rustcall_pyo3] table naming the same pyo3 package the target crate resolved (_resolved_pyo3_dependency).

A crates.io package is pinned by exact version and a path dependency by its directory. A git dependency repeats Cargo's source exactly, selector included (_git_dependency_keys), and not the resolved commit: git+URL?branch=main pinned as rev = <sha> would be a second source to Cargo, and the commit is pinned by the target crate's Cargo.lock the wrapper is seeded with. A package from any other registry raises RustError, because a bare version would select crates.io's pyo3 instead (#370, #392 review).

source
RustCall._pyo3_conservative_plan — Method
_pyo3_conservative_plan(cargo_toml) -> PyO3LinkPlan

The plan when Cargo could not resolve the crate. It performs no feature resolution — that is exactly what got this wrong repeatedly — and reads only whether a pyo3 dependency is declared at all:

  • no pyo3 declaration anywhere (including [target.'cfg(...)'.dependencies] and renamed entries) -> :python_free;
  • any declaration listing extension-module -> :unlinkable;
  • otherwise -> :link_libpython, because without Cargo nothing here can show that the optional dependency is off in the build the wrapper would make.

The feature selection the caller asked for (flags, features, default_features) travels with the plan even though its resolved closure is unknown: the wrapper's dependency entry is written from the plan, and a plan that dropped them would build the crate's default configuration — not the one requested, and possibly one that re-enables a default the caller switched off (#307 review). crate_features is the requested list here, not Cargo's transitive closure.

source
RustCall._pyo3_dependencies — Method
_pyo3_dependencies(cargo) -> Vector{_PyO3Dependency}

Every declaration of pyo3 in the crate's manifest: the plain [dependencies] entry and each [target.'cfg(...)'.dependencies] one, matched on the package field so a renamed dependency (python = { package = "pyo3" }) is found too. The shorthand pyo3 = "0.29" is normalized to Dict("version" => "0.29").

source
RustCall._pyo3_feature_flags — Method
_pyo3_feature_flags(features, default_features) -> Vector{String}

The Cargo flags naming a feature set: --no-default-features and --features a,b, in the spelling cargo build takes.

source
RustCall._pyo3_resolved_plan — Method
_pyo3_resolved_plan(crate_path, flags; release = true) -> Union{PyO3LinkPlan, Nothing}

Ask Cargo what a build of crate_path under flags resolves to, and turn the answer into a plan. nothing when Cargo is unavailable or the crate does not resolve — the caller then falls back to _pyo3_conservative_plan. The cfg probe runs the crate as a wrapper's dependency, under the profile the wrapper will be built with (_wrapper_probe_cfg_text).

source
RustCall._pyo3_unresolved_cfg_plan — Method
_pyo3_unresolved_cfg_plan(crate_path, flags, crate_features, pyo3_features, pyo3_active)

The plan for a build whose feature graph Cargo resolved but whose configuration it would not print (cargo rustc -- --print cfg failed: an offline registry, a build.rs that does not run here, a target without a toolchain).

The mode is decided from the feature graph exactly as a fully resolved plan decides it — that part is known — but cfg_text is empty and resolved is false, so scan_report scans leniently and says so, and the Phase-2 wrapper refuses any #[cfg]-carrying item (cfg_undecided) instead of generating a call it cannot justify.

source
RustCall._pyo3_wrapper_build_env — Method
_pyo3_wrapper_build_env(plan, rustflags) -> Vector{Pair{String, String}}

The build-environment half of a wrapper's ArtifactId. The environment the build inherits is part of the artifact, not just the flags RustCall adds: RUSTFLAGS and the rest of the #282 allowlist reach cargo through _build_pyo3_wrapper_project, so two builds under different ambient flags are different binaries. So are the plan's link flags — and, for :link_libpython, the interpreter the build pins PYO3_PYTHON to: it is decided by the plan, before the key, so a wrapper configured for one Python never answers a lookup made for another that happens to share its library directory (#307 review; #278's rule that identity is exhaustive). A :python_free build pins nothing and records nothing. pyo3's other build inputs (PYO3_CONFIG_FILE, PYO3_CROSS_*, …) reach the key through the PYO3_* prefix of the allowlist, and the contents of PYO3_CONFIG_FILE are hashed here on top.

source
RustCall._pyo3_wrapper_build_script — Method
_pyo3_wrapper_build_script(plan) -> String

The build.rs of a wrapper crate: the link options pyo3_link_rustflags describes, spoken the way a build script speaks them — cargo:rustc-link-search= native=<dir> for the interpreter's library directory and, where the platform has an rpath, cargo:rustc-link-arg=-Wl,-rpath,<dir>; nothing for a :python_free plan, which links no libpython. A build script reaches exactly this crate's link step, which an environment RUSTFLAGS did not reliably do (#307 review).

source
RustCall._pyo3_wrapper_items — Method
_pyo3_wrapper_items(manifest) -> (functions, structs, skipped, pyo3_exports)

Split the wrapper crate's manifest into what Julia binds and what it reports.

Both origins are bound. A crate may carry #[julia] items and PyO3-only ones; the wrapper crate exports the second kind and links the first, whose rustcall_* symbols the dependency's own object code already provides (the generated lib.rs keeps the glob import that pulls them into the cdylib). Returning only the PyO3 entries silently dropped every #[julia] function and struct of such a crate from the module @rust_crate handed back.

Only entries the generator actually emitted are bound: a skip_reason means no symbol exists, and generating a Julia definition for one would produce a dlsym failure at the first call instead of a message at scan time. Methods are filtered inside their class for the same reason, and the class keeps its fields untouched because the generator already cleared the accessors it did not emit.

pyo3_exports counts the PyO3-origin items that were emitted, which is what decides whether a wrapper crate is worth building at all.

source
RustCall._python_runtime_libraries — Method
_python_runtime_libraries(interpreter) -> Vector{String}

The shared libraries a :link_libpython wrapper needs at load time that the platform's loader would not find on its own — on Windows, the interpreter's own python3xy.dll.

The wrapper links the import library in LIBDIR (…\libs), but the DLL it then imports by name lives beside the interpreter (sys.base_prefix), and Windows has no rpath: the loader searches the application directory, the system directories and PATH, so a wrapper built against a virtual-environment or Conda PYO3_PYTHON that is not on the Julia process's PATH linked fine and failed to dlopen (#307 review). The Windows loader does, however, satisfy an import by name from a module the process has already loaded, whatever directory it came from — so the generated module opens this library, by full path, before the wrapper (load_artifact!'s preload), through the one load path of the package (#277).

The path is the interpreter's own answer — GetModuleFileNameW(sys.dllhandle), the DLL the running interpreter is executing — not a guess from its version and prefix, so a venv (whose Scripts\python.exe is a launcher beside no DLL) and a Conda environment report the right file. Empty on every other platform, where the rpath does this, when interpreter is "" (a PYO3_CONFIG_FILE or PYO3_CROSS_LIB_DIR configuration names no interpreter — put the DLL directory on PATH yourself), or when the interpreter cannot answer.

source
RustCall._remove_shaped_project — Function
_remove_shaped_project(path, lease = nothing)

Remove a wrapper-shaped project and its lease file. The directory goes first, while lease is still held, so a sweep — and an owner resuming from a pause — cannot claim it in between; then the lock is released and the lease file removed. Windows refuses to delete a file an open handle still holds, so the lease file goes after the close.

source
RustCall._require_dispatcher_pyo3_version — Method
_require_dispatcher_pyo3_version(version, crate_name)

Refuse a dispatcher-using wrapper whose PyO3 predates PYO3_DISPATCHER_MINIMUM, naming the version, the floor and the way out.

Before this, generation succeeded and the wrapper build failed with rustc errors about Python::attach — an error about generated code the user never wrote, for a crate that is otherwise wrappable (#370).

source
RustCall._resolved_pyo3_dependency — Method
_resolved_pyo3_dependency(crate_path, plan) -> NamedTuple

The pyo3 package the target crate actually resolves, as (; version, source, dir).

source is Cargo's own spelling — registry+..., git+..., or empty for a path dependency — and dir is the directory of the resolved package's manifest. Both are needed, not just the version: the wrapper's rustcall_pyo3 alias has to name the same package, and a version-only alias silently resolves a second copy from crates.io whenever the crate gets pyo3 from a path or a git checkout. Two pyo3 instances in one build is not a duplicate-work problem: the target's macro metadata carries types from its instance while the generated dispatcher supplies Python and the traits from the other, so the defaults and inherited classes do not build (#370).

All-empty when Cargo could not be asked, or when the crate resolves more than one pyo3, in which case nothing is claimed.

source
RustCall._sweep_abandoned_projects — Method
_sweep_abandoned_projects(parent) -> Int

Remove the generated projects under parent whose owner is gone.

A project is named project_<owner pid>_<random> and, while its owner works on it, is claimed by a held <project>.lease. The claim is published atomically: the lease is locked under a staging name and renamed into place (_publish_shaped_project_lease) before the directory exists, so a visible lease is always already locked (#437). A held lease keeps a project whatever the name's pid says; a free lease means the owner is gone — the finally that removes it did not run when the process was interrupted, and the cfg probe's project can be hundreds of MB (measured on a crate with large path dependencies, the probe wrote 538 MB before it was killed: #425) — and the project is removed. There is no window in which an owner is still claiming a visible project, so no age or grace window is consulted.

Where there is no lease — Windows, whose machine-wide process table decides instead of a lease, a file system without advisory locking, or a pre-#437 directory — the pid in the name is consulted, and only a provably dead owner is removed. _process_alive reads only the caller's pid namespace, but that is exact on Windows and the only signal left on a lockless volume. Returns the count.

source
RustCall._with_shaped_project — Method
_with_shaped_project(f, crate_path, subdir)

Claim a wrapper-shaped project (_wrapper_shaped_project), hold its lease for the duration of f(dir), and remove it afterwards.

The lease is <dir>.lease, held with the same advisory lock the generation copies use (_try_lock_lease). It is what makes the sweep safe across pid namespaces: a project whose lease another process still holds is live even when that process's pid means nothing here, and a project whose owner died — this finally never ran — is left with a free lease and is swept (#425 review). The claim is atomic (#437), so no grace window and no re-check are needed: the directory cannot appear before the lock, and no sweep can take a held lease. Where the file system offers no locking, the lease records nothing and the sweep falls back to the pid in the name.

source
RustCall._wrapper_probe_cfg_text — Method
_wrapper_probe_cfg_text(crate_path, env; features, default_features, release) -> String

rustc --print cfg for crate_path as the dependency of a wrapper crate, which is how a Phase-2 wrapper compiles it.

Probing the crate as its own Cargo root (_crate_build_cfg_text) applied the crate's own [profile.*] — its debug-assertions, its panic, its overflow checks — which Cargo ignores for a dependency and which the wrapper's root profile replaces (RustCall pins panic = "unwind" there, #244). A #[cfg(debug_assertions)] item was therefore scanned in and compiled out, or the reverse, and a generated call to it failed to resolve (#307 review).

The probe project has the wrapper's shape: a cdylib root depending on the crate by path, with the requested feature set in its [dependencies] entry — the only place a dependency's features can be named; cargo rustc --features is refused for a package outside the workspace — and the profile lines the generated wrapper carries. It lives under RustCall's cache (_wrapper_shaped_project, never the crate's own target/, #486) with the crate's lockfile and [patch] table, and Cargo runs from the crate, naming the probe by --manifest-path, so the crate's .cargo/config.toml, pins and overrides apply to the probe as to the wrapper; and it runs under the wrapper policy's environment (_cargo_panic_env), so an inherited CARGO_PROFILE_*_PANIC cannot make it describe a build the wrapper never makes. Then cargo rustc -p <crate> --lib -- --print cfg. The build cache is the wrapper's, crate_target_directory(crate, :pyo3_wrapper), so Cargo still reuses compiled dependencies, but the cfg answer itself is not memoized: build scripts can observe inputs outside a stable digest (#291). "" means Cargo did not answer.

interpreter, when given, is pinned in PYO3_PYTHON for the probe exactly as the wrapper build pins it (_wrapper_probe_env), so a crate whose build script derives Py_3_x cfgs through pyo3-build-config is probed for the Python the wrapper is built against, not the one pyo3 would find on its own.

env is the environment of the build the probe describes — a snapshot's (snapshot_env) or a record's (_record_build_subprocess_env) — and every Cargo command of the probe runs under it, with the wrapper policy's pins on top (#481). The forms without it take a snapshot of ENV now.

source
RustCall._wrapper_shaped_project — Method
_wrapper_shaped_project(crate_path, subdir) -> (dir, lease)

A fresh directory for a Cargo project that stands in for the wrapper crate — the generated wrapper itself, or the cfg probe — placed under crate_target_directory(crate, :pyo3_wrapper)/<subdir>/, in RustCall's cache and never in the crate, so a read-only crate can be wrapped (#486), and seeded with the root-only inputs of the crate's own build, so Cargo resolves the project the way it resolves the crate (#307 review):

  • .cargo/config.toml is discovered from the working directory upwards, so every Cargo command on the project runs in the crate's directory and names the project with --manifest-path: it finds the crate's config exactly as a build in the crate does — a project run from elsewhere found nothing, and an item that exists only under the config's rustflags was a compile error in generated code;
  • Cargo.lock is the root's, so the lockfile of the crate's Cargo root — the crate itself, or the workspace it is a member of (_workspace_root_dir) — is copied in; Cargo adds the project's own entry to the copy and pins everything else as the crate pinned it;
  • [patch] is honoured only in the root manifest, so that root's table is carried over by _root_patch_toml, relative paths made absolute.

Both generated manifests declare an empty [workspace]: a project below a workspace (a cache directory inside one, or the crate's own target/ before #486) would otherwise make Cargo climb to it and reject a crate it does not list ("believes it's in a workspace when it's not").

The claim comes before the directory: _publish_shaped_project_lease names the project, locks its lease under a staging name and renames it into place, and only then is the directory made (#437). A sweep therefore never sees a project without its lock, so it needs no grace window. The returned lease is the held stream (or nothing where there is no lock); pass both to _remove_shaped_project when done.

source
RustCall.build_pyo3_wrapper — Method
build_pyo3_wrapper(info; features, default_features, release, cache_enabled) -> PyO3Wrapper

Generate, build and load-prepare the wrapper crate for info (#275 Phase 2).

The steps, in the order their failures matter:

  1. Decide whether a wrapper can be linked at all. pyo3_link_plan asks Cargo to resolve the feature set, and pyo3_link_rustflags raises for an :unlinkable build or a :link_libpython one with no interpreter library directory. Nothing is generated or compiled before that is settled.
  2. Generate. wrap_crate re-runs the scan scan_crate reported and turns it into a lib.rs plus the manifest of what that file exports.
  3. Build. A temporary Cargo project with crate-type = ["cdylib"], the target crate as its only dependency — which the target must be able to be: a crate-type = ["cdylib"]-only crate has no rlib for a dependent to link and is refused with the one-line fix (_require_linkable_lib_target) — (with the plan's features, and default-features switched off there when the plan says so — nothing on the cargo build command line can do that for a dependency), panic = "unwind" pinned as every RustCall build is, and the plan's link flags.
  4. Cache. Under an ArtifactId of kind pyo3-wrapper whose codegen fields carry the feature set and the link flags, so a build under other features is a different artifact rather than a silent cache hit.

The scan is lenient, and that is deliberate

A PyO3 marker can be conditional (#[cfg_attr(feature = "python", pyfunction)]) while the item it marks is not. The wrapper calls the item, so it can be built even for a feature set in which nothing is exposed to Python — which is how a crate with an optional pyo3 dependency gets a wrapper that links no libpython at all. Scanning strictly under such a build would report no PyO3 item and wrap nothing.

The price is that an item whose own #[cfg] predicate is undecided cannot be called safely, so the generator refuses it with cfg_undecided rather than guessing. When Cargo resolved the build's configuration, the plan carries it, the scan is strict, and no item is refused for that reason.

source
RustCall.crate_needs_pyo3_wrapper — Method
crate_needs_pyo3_wrapper(info::CrateInfo) -> Bool

Whether @rust_crate should generate a wrapper crate for the PyO3 items of this crate rather than bind only its #[julia] ones (#275 Phase 2).

The initial scan may find an exportable PyO3 item, or a Cargo build script may generate one that cannot be seen until the build is probed. Build-script crates therefore need the resolved scan even when their committed sources expose no PyO3 items. Other crates with no wrappable PyO3 items keep the pre-#275 path.

This is the cheap pre-check, on the lenient scan scan_crate already did. build_pyo3_wrapper has the final say: under the build's own configuration a marker behind a feature that is off is simply not there, and it returns nothing so the caller falls back to the same pre-#275 path.

source
RustCall.crate_rust_identifier — Method
crate_rust_identifier(package_name, cargo_toml) -> String

The name Rust code refers to this crate by, which is not always its package name: [lib] name = "..." renames the library target, and a dependent crate then writes that_name::item. Cargo also maps - to _ in an identifier.

Generated wrapper code names the dependency by path, so getting this wrong is an unresolved-crate compile error in code the user never wrote.

source
RustCall.extension_module_is_linkable — Method
extension_module_is_linkable() -> Bool

Whether a wrapper cdylib that resolves pyo3 with extension-module can still be linked and loaded on this target.

On Unix it cannot: extension-module tells pyo3 to leave libpython's symbols undefined for the importing interpreter to supply, which the #275 MWE confirmed is fatal for a cdylib Julia loads — macOS does not even link it, and Linux links it but fails dlopen under both RTLD_NOW and RTLD_LAZY (undefined symbol: _Py_Dealloc).

Windows has no such mode. A DLL there must resolve every import at link time, so pyo3 links python3X.lib regardless of extension-module, and the resulting wrapper loads exactly like any other :link_libpython build as long as the interpreter's DLL directory is on PATH. Classifying such a build :unlinkable on Windows refused a crate that works.

source
RustCall.generate_pyo3_wrapper_cargo_toml — Method
generate_pyo3_wrapper_cargo_toml(info::CrateInfo, plan::PyO3LinkPlan) -> String

The Cargo.toml of a #275 Phase-2 wrapper crate.

The dependency entry comes from pyo3_dependency_toml, which is the only place a dependency's default features can be switched off — cargo build --no-default-features applies to the package being built, i.e. the wrapper. Most wrappers need only the target crate and std. A wrapper that calls PyO3's dispatcher also gets an exact-version, macros-only rustcall_pyo3 alias; the alias keeps generated paths stable without unifying against a different PyO3 release.

source
RustCall.partition_first — Method
partition_first(text, delimiter) -> (before, found, after)

Split text at the first delimiter. found is false when there is none, in which case before is all of text.

source
RustCall.pyo3_dependency_toml — Method
pyo3_dependency_toml(plan, name, path) -> String

The [dependencies.<name>] entry a Phase-2 wrapper crate must write for plan to hold. This is where dependency_default_features takes effect: nothing on the cargo build command line can turn off a dependency's default features.

source
RustCall.pyo3_feature_candidates — Method
pyo3_feature_candidates(crate_path) -> Vector{NamedTuple}

Which of the crate's own features activate pyo3, and which of those also pull extension-module — answered by Cargo, one resolution per feature:

[(feature = "python",    activates_pyo3 = true,  extension_module = false),
 (feature = "extension", activates_pyo3 = true,  extension_module = true)]

A feature that does not reach pyo3 at all is not listed. Empty when the crate has no [features] table, or when Cargo cannot resolve it — which is not the same as "no feature activates pyo3", so check pyo3_link_plan(...).resolved before reading anything into an empty list.

source
RustCall.pyo3_link_plan — Method
pyo3_link_plan(crate_path; features = String[], default_features = true,
               release = true) -> PyO3LinkPlan

The plan for building a wrapper crate against crate_path under a given feature set, which is named the way Cargo and @rust_crate name one: features are extra crate features to enable, default_features = false is --no-default-features. The default is the crate's own default features.

release is the profile the wrapper will be built with, and the cfg probe follows it: debug_assertions is set in a debug build and not in a release one, so a #[cfg(debug_assertions)] item scanned under the other profile would be decided the wrong way round (#307 review).

The plan is for that set only — it does not go looking for a better one. Use pyo3_feature_candidates to see which features activate pyo3 and which pull extension-module, then ask for the set you want.

Worked example

[features]
default = ["extension"]
python = ["dep:pyo3"]
extension = ["python", "pyo3/extension-module"]

The default build is :unlinkable (extension pulls extension-module), and so is --all-features. default_features = false alone is :python_free but compiles none of the crate's #[cfg(feature = "python")] API. The build that works is the one a person picks from the candidates:

RustCall.pyo3_feature_candidates(crate)
# ("python",    activates_pyo3 = true,  extension_module = false)
# ("extension", activates_pyo3 = true,  extension_module = true)

plan = RustCall.pyo3_link_plan(crate; features = ["python"], default_features = false)
plan.mode   # :link_libpython
source
RustCall.pyo3_link_rustflags — Method
pyo3_link_rustflags(plan::PyO3LinkPlan) -> Vector{String}

The link options a wrapper build carries for plan, in rustc's spelling: nothing for :python_free, and -L <dir> plus an rpath link argument for :link_libpython. They are part of the wrapper's artifact identity and what scan_report shows; the build itself emits the same options from a generated build.rs (_pyo3_wrapper_build_script) rather than through RUSTFLAGS.

Raises RustError when the plan cannot be built: :unlinkable, or :link_libpython with no interpreter library directory. This is the point where Phase 2 fails loudly rather than producing a cdylib nothing can load; pyo3_link_plan itself never raises, so a caller can inspect and report a crate without committing to building it.

source
RustCall.pyo3_skip_explanation — Method
pyo3_skip_explanation(reason::AbstractString) -> String

Turn a manifest skip_reason into a sentence. Unknown reasons are passed through unchanged so a newer extractor never produces an empty explanation.

source
RustCall.pyo3_wrapper_build_record — Method
pyo3_wrapper_build_record(crate_path, plan, snapshot; release, features,
                          default_features) -> CrateBuildRecord

The CrateBuildRecord of a PyO3 wrapper build made under snapshot from plan, unnamed (_record_named names it once the key is known). A :link_libpython plan's interpreter, fingerprint and link directory are recorded as the plan has them — the values _pyo3_wrapper_build_env puts in the artifact key — not asked of the interpreter again: an interpreter replaced in place after the build was verified would otherwise be recorded, and the load-time check would accept a wrapper built for the Python it replaced (#485 review). Both crate emitters are handed this record on the wrapper path.

source
RustCall.python_library_dir — Method
python_library_dir() -> String

The directory holding the Python interpreter's shared library, or "" when it cannot be found. Consulted in order:

  1. ENV["RUSTCALL_PYTHON_LIBDIR"] — an explicit override always wins;
  2. CondaPkg's environment, when CondaPkg is already loaded (PythonCall users);
  3. macOS only: sysconfig.get_config_var("PYTHONFRAMEWORKPREFIX"), the directory containing Python3.framework;
  4. python3-config --ldflags, taking its -L directory;
  5. sysconfig.get_config_var("LIBDIR") of python3.

Only an existing directory is returned.

This is the directory half of python_link_source, which decides the directory and the interpreter (PYO3_PYTHON) together; an explicit PYO3_PYTHON changes which interpreter steps 3–5 ask.

Why the framework prefix comes first on macOS

A framework build of Python is linked as @rpath/Python3.framework/Versions/3.x/Python3, so the rpath the wrapper needs is the directory holding the .framework, not LIBDIR (which is …/Versions/3.x/lib, one level inside it). Handing the linker LIBDIR produces a cdylib whose @rpath reference cannot be resolved and which then fails to load — with a message about a missing Python3.framework, not about the directory that was wrong.

source
RustCall.python_link_source — Method
python_link_source([snapshot::BuildEnvSnapshot]) -> (libdir::String, interpreter::String, config::String)

The library directory a :link_libpython wrapper links against and the interpreter it pins PYO3_PYTHON to, decided together so the two cannot name different Pythons: pyo3's build script configures itself for PYO3_PYTHON while the linker searches libdir, and a pair taken from two sources is a cdylib configured for one ABI and linked against another (#307 review). The third value is what that interpreter reports about itself (_python_interpreter_fingerprint), the identity the artifact key records alongside the path. Any of the three is "" when it cannot be identified.

In order:

  1. RUSTCALL_PYTHON_LIBDIR, when set, is the directory whatever else says — the RustCall-level override.
  2. pyo3's own configuration, when it names the directory: a cross-compilation PYO3_CROSS_LIB_DIR, or the lib_dir of a PYO3_CONFIG_FILE. pyo3 consults no interpreter in either case, so the plan does not invent one: the interpreter is PYO3_PYTHON if set and "" otherwise, and the fingerprint is empty (the configuration itself is in the artifact key). A directory derived from PYO3_PYTHON or PATH here would be one pyo3 never links, and the wrapper would carry an rpath to a library it is not linked against (#307 review).
  3. PYO3_PYTHON, when set, is the interpreter — an explicit choice, a virtual environment or a Conda interpreter, is never replaced by the first python3 on PATH — and the directory is what that interpreter reports (its framework prefix on macOS, its sysconfig LIBDIR otherwise).
  4. RUSTCALL_PYTHON_LIBDIR alone leaves the interpreter to PATH, the python3 / python that python3-config describes.
  5. A loaded CondaPkg names both: <envdir>/lib and the environment's own python.
  6. Otherwise the python3 / python on PATH: its sys.executable, and its directory from the framework prefix (macOS), python3-config --ldflags, or sysconfig LIBDIR, in that order.

Every variable is read from snapshot, and every interpreter and python3-config is run under it (#481); without one, a snapshot of ENV is taken now.

source
RustCall.scan_report — Method
scan_report(crate_path; features = String[], default_features = true, io = stdout,
            generate = true, resolve = true) -> NamedTuple

Report what a crate offers to RustCall, including the items it marks only for PyO3 (#275 Phase 1).

By default the crate is scanned under the build the plan describes: pyo3_link_plan asks Cargo to resolve features / default_features, and the scan then runs in strict mode under that build's configuration, so #[cfg] and #[cfg_attr] on functions, structs, impls, methods and fields are evaluated by the extractor's own evaluator and the items listed are exactly the items that build has.

Pass resolve = false for a probe-free Phase-1 mode: no Cargo runs at all. The plan is the declaration-only conservative reading of Cargo.toml (plan.resolved == false) and the scan is lenient, so a #[cfg]-carrying item is reported rather than decided — the same distinction an unresolved plan makes. Use it for large crates (the default mode's cfg probe compiles the crate's whole dependency graph as a wrapper dependency, which can be minutes and gigabytes), offline machines, and item-inventory checks that need no link plan. A workspace member's inherited version / edition are read from the workspace manifest, so even that does not reach cargo metadata. candidates is empty in this mode. generate = false is orthogonal: it skips the Phase-2 generator, but the default plan resolution still runs.

Returns (; julia, wrappable, wrapped, skipped, plan, info, candidates):

  • julia — items carrying a RustCall attribute (#[julia]), which @rust_crate wraps today;
  • wrappable — PyO3 items the scan found no reason to reject: a wrapper crate can name each of them;
  • wrapped — the items the Phase-2 generator actually emitted an entry point for, with symbol naming it (#275 Phase 2). It is a subset of wrappable: the generator refuses a signature it cannot lower — a Vec argument, a PyResult<String>, an item behind an undecided #[cfg] — and those appear in skipped with the reason it gave. nothing when the wrapper could not be generated at all (generate = false, or an :unlinkable plan), in which case skipped holds only the scan's own reasons;
  • skipped — PyO3 items that cannot be wrapped, each with its reason;
  • plan — the PyO3LinkPlan for the requested feature set;
  • info — the CrateInfo scanned under it;
  • candidates — pyo3_feature_candidates(crate_path), the features that activate pyo3, so a different set can be chosen deliberately.
RustCall.scan_report("/path/to/some_pyo3_crate")
RustCall.scan_report(crate; features = ["python"], default_features = false)
RustCall.scan_report(crate; resolve = false)   # no Cargo, lenient scan
source
RustCall.wrapper_target_identifier — Method
wrapper_target_identifier(package_name, cargo_toml) -> (identifier, renamed)

The name the generated wrapper depends on the target crate under, which is also the identifier its generated Rust names the crate by.

Normally that is crate_rust_identifier. When it — or the package name Cargo would key the dependency table on — is one of WRAPPER_RESERVED_CRATE_NAMES, the dependency is renamed: [dependencies.rustcall_pyo3] would otherwise be written twice into one Cargo.toml (invalid TOML), and use rustcall_pyo3::* in the generated lib.rs would resolve to the wrapped crate rather than to PyO3 (#392 review).

A rename is a plain Cargo feature: the table keeps package = "<real name>" and the crate is available in Rust under the key. renamed says whether the caller has to emit that key.

source

The Python host path (src/pyo3/pyo3_host.jl)

RustCall.PyO3Extension — Type
PyO3Extension

The artifact of building a PyO3 crate as the Python extension it already is (#424). Returned by build_pyo3_extension.

Fields

  • module_name::String: the name CPython imports — the #[pymodule] initializer's name, or its name = "..." option.
  • lib_path::String: the importable file, <module_name><ext_suffix>, inside dir.
  • dir::String: the directory to put on sys.path before importing.
  • ext_suffix::String: the interpreter's EXT_SUFFIX, which the file name must carry.
  • interpreter::String: the Python the crate was built against (PYO3_PYTHON).
  • fingerprint::String: what that interpreter reports about itself; part of the cache key.
  • key::String: the artifact cache key.
source
RustCall.PyO3HostProperty — Type
PyO3HostProperty

One property the PyO3 host binds on a class (#524): a #[pyo3(get)] / #[pyo3(set)] field, or the #[getter] / #[setter] methods of one Python attribute, merged by that attribute. python is the attribute PyO3 exposes (the manifest's python_name: #[getter(end)], #[pyo3(get, name = "x")], a get_ / set_ prefix dropped, a raw r#for unrawed); julia is the name Julia reads it under, julia_binding_name of that attribute (for → for_). read_shape is the hint of the value a read returns (nothing when nothing reads it); a read is converted as a method's return is (_pyo3_host_value_expr), a write as a method's argument is (_pyo3_to_python), by the value (PR #525 review). what names the Rust item in a message.

source
RustCall._build_pyo3_extension_library — Method
_build_pyo3_extension_library([snapshot,] crate_path, cargo_toml, module_name; kwargs...) -> String

Run cargo rustc --crate-type cdylib in the crate's own directory, with the output under crate_target_directory(crate_path, :pyo3_host) (#486), and return the built extension file, which is a temporary Cargo output — copy it out before the next build replaces it. It runs under snapshot (the build's, from build_pyo3_extension); the form without one takes a snapshot of ENV now.

source
RustCall._pyo3_extension_artifact — Method
_pyo3_extension_artifact(cache_dir, info, module_name, python, ext_suffix, fingerprint;
                         features, default_features, release) -> PyO3Extension

Where the extension module of info built for python lives under cache_dir: the cache key and the paths it decides, computed without touching the interpreter, Cargo or the cache directory. build_pyo3_extension is this plus the interpreter probe and the build; the precompile workload of #449 runs this on its own, because a Python interpreter may not be started while precompiling and the key must not depend on one.

source
RustCall._pyo3_host_bound_properties — Method
_pyo3_host_bound_properties(s::RustStructInfo) -> Vector{PyO3HostProperty}

The properties the host binds on s, the one list its property emitter (getproperty, setproperty!, propertynames) and its definitions (_pyo3_host_definitions) are read from (#524): every field PyO3 exposes, then every #[getter] / #[setter] method, merged by the Python attribute they expose — a getter and a setter of one attribute are one property, readable and writable. async accessors are refused by the extractor and bound nowhere.

source
RustCall._pyo3_host_build_env — Method
_pyo3_host_build_env(python, fingerprint, module_name) -> Vector{Pair{String, String}}

The build environment the host-path extension is keyed by: artifact_build_env() — RUSTFLAGS, a build script's CC and the rest of the #282 allowlist, which decide what cargo rustc produces exactly as they do for the plain crate path and the PyO3 wrapper — plus the contents of PYO3_CONFIG_FILE when it is set, then the interpreter the build is configured for and the module name. Without the allowlist a changed RUSTFLAGS found the previous extension in the cache (#461).

The ambient PYO3_PYTHON is left out of the allowlist half: the build sets it to python itself, which the key records under the same name. Reading only the environment and a file, this starts no process, so the precompile workload can compute it (#449).

source
RustCall._pyo3_host_cargo_cmd — Method
_pyo3_host_cargo_cmd(snapshot, crate_path, cargo_toml; python, features,
                     default_features, release, rustc_args) -> (Cmd, target_dir, package)

The one Cargo invocation of the PyO3 host path: cargo rustc --crate-type cdylib of the crate as its own root, under the requested profile and features, in RustCall's target directory for it, under snapshot with PYO3_PYTHON set to the interpreter the module is built for. rustc_args follow --. The build (_build_pyo3_extension_library, with the link arguments) and the cfg probe the bindings are scanned under (_pyo3_host_cfg_text, with --print cfg) are both this command, so the scan sees the #[cfg]s the build compiles — the profile's debug_assertions, the interpreter's Python-version cfgs — and the two cannot drift (#514 review). An empty python leaves PYO3_PYTHON as the snapshot has it (a scan outside the host extension).

source
RustCall._pyo3_host_cfg_text — Method
_pyo3_host_cfg_text(snapshot, crate_path, cargo_toml; python, features,
                    default_features, release) -> String

rustc --print cfg of the host build (_pyo3_host_cargo_cmd), for the scan the host bindings are generated from; "" when Cargo does not answer, and the lenient scan is used instead.

source
RustCall._pyo3_host_check_names — Method
_pyo3_host_check_names(info::CrateInfo)

_check_julia_definitions over _pyo3_host_definitions: the check every emitter runs, over what this one defines.

source
RustCall._pyo3_host_defined_names — Method
_pyo3_host_defined_names(exprs) -> Vector{String}

The module-level names these expressions define — import M, const X = ..., function f(...), function f end, abstract type T end — read off the expressions themselves, so the reserved set is whatever the prelude emits and never a list kept beside it.

source
RustCall._pyo3_host_definitions — Method
_pyo3_host_definitions(info::CrateInfo) -> Vector{JuliaDefinition}

What generate_pyo3_host_bindings defines at the top level of its module, item by item as its emitters define it (#514): a #[pyfunction] and a static or class method are free functions of untyped arguments (the host passes no type), a class is a type whose #[new] is its constructor, an instance method dispatches on its class, and each of _pyo3_host_bound_properties — an exposed field or the #[getter] / #[setter] methods of one attribute — is a property under its Julia name (#524). async items define nothing.

source
RustCall._pyo3_host_item_exprs — Method
_pyo3_host_item_exprs(info) -> Vector{Any}

The definitions of the host's functions and classes, every parameter named against the definitions it lands in (_rename_parameters, #526).

source
RustCall._pyo3_host_prelude_exprs — Method
_pyo3_host_prelude_exprs(path, features, default_features, release) -> Vector{Any}

What generate_pyo3_host_bindings defines at the top of its module before any binding: the imports, the build constants, the lazy import _pyo3_module, and the value conversions every binding goes through (PR #525 review):

  • _pyo3_to_python(x) — a class handle (_PyO3Object) is the Python object it holds; a Julia array of numbers is a numpy.ndarray when numpy imports (pyo3-numpy extracts from nothing else, #424, and PyO3 reads a Vec from one as from any sequence); another array or a tuple is converted element by element; anything else is passed as it is (nothing is None).
  • _pyo3_from_python(v[, T]) — None is nothing; an object is the Julia handle of the most derived of the module's classes (_pyo3_class_pairs, emitted after them) in its type's MRO — its own class first, so a #[pyclass(extends = ...)] object stays itself and an instance of a Python-defined subclass is its nearest bound base. The walk is not cached: a cache keyed by the type object would keep every type it saw alive, and the walk is a few identity comparisons; anything else is pyconverted to the hint T when it fits and stays the Python object when it does not. A list read as Vector{T} is read element by element, so a list of class objects is a vector of handles.
  • _pyo3_ok(v, T) — the Ok of a PyResult, typed T when the value is one.

The names it defines are reserved (_pyo3_host_defined_names, read by _pyo3_host_definitions), so a crate item bound under one is refused rather than redefining it.

source
RustCall._pyo3_host_read_type — Method
_pyo3_host_read_type(shape) -> Union{Symbol, Expr}

The Julia type the hint says a value is read back as, :Any for "whatever it is": a scalar's Julia type, String, a Vector of the element's hint, a numpy array by its element and rank. An Option is its payload's hint: None is nothing whatever the hint (_pyo3_from_python).

source
RustCall._pyo3_host_required_shape — Method
_pyo3_host_required_shape(shape, what) -> PyO3Shape

The hint of a PyO3 position, or a RustError when the manifest carries none: every position of a scanned PyO3 item has one, and without it the host cannot tell an argument the interpreter supplies from one the caller passes, so a missing one means the extractor predates it.

source
RustCall._pyo3_host_value_expr — Method
_pyo3_host_value_expr(call, shape) -> Expr

The call's result read back — the one decision for every value the host reads: a function's or a method's return and a property's read (getproperty). _pyo3_from_python decides by the value (None, an object of a bound class); the hint only types what is left.

source
RustCall.build_pyo3_extension — Method
build_pyo3_extension(crate_path; python, features, default_features, release, cache_enabled) -> PyO3Extension

Build the PyO3 crate at crate_path as a Python extension module, without modifying the crate, and return where the result lives (PyO3Extension).

python names the interpreter the module is built for; it is passed to pyo3's build configuration as PYO3_PYTHON, and its sysconfig EXT_SUFFIX names the output file. Pass the interpreter of the Python implementation that will import the result — for PythonCall, PythonCall.python_executable_path().

The crate must declare #[pymodule] (there is nothing to import otherwise); a [lib] crate-type that does not include "cdylib" is not a problem, because the build selects a cdylib explicitly. Unlike the wrapper path's rlib requirement, an rlib-only crate is fine here.

The build runs cargo rustc --crate-type cdylib in the crate's own directory, so the crate's .cargo/config.toml, lockfile and [patch] tables apply as they do to the crate itself. Its output — dependencies included — goes under crate_target_directory(crate_path, :pyo3_host) in RustCall's cache, never the crate's own target/ (#486), so a crate in a read-only tree builds; successive host builds of the crate reuse those dependency outputs, and the user's own cargo build in the crate neither shares them nor is disturbed by them. The platform's extension-module link flags (macOS -undefined dynamic_lookup, which pyo3's build script cannot deliver to the final cdylib) travel as trailing rustc arguments, not through RUSTFLAGS.

The result is cached under RustCall.get_cache_dir()/pyo3-host/, keyed by the crate path, the feature set, the profile, the module name and the interpreter's fingerprint; cache_enabled = false builds every time.

This is the interpreter-free half of pyo3_host_import (#449): python runs only as a subprocess, for the module's EXT_SUFFIX and the interpreter's fingerprint, so it may run before any Python is loaded into the process — but not while a package precompiles, because the interpreter it is keyed by is not known then.

source
RustCall.generate_pyo3_host_bindings — Method
generate_pyo3_host_bindings(crate_path; module_name, features, default_features, release) -> Expr

The module expression @rust_crate ... pyo3_host=true evaluates: a typed Julia surface over the crate's imported Python module. Core and interpreter-free to build — the import itself is lazy and lives in pyo3_host_import.

source
RustCall.pyo3_host_import — Function
pyo3_host_import(crate_path; features, default_features, release, cache_enabled) -> Py

Build crate_path as a Python extension and import it, returning the Python module object (as a PythonCall.Py).

This is a hook: RustCall does not depend on a Python implementation, so RustCall defines no method here. Load PythonCall and the RustCallPyO3HostExt package extension defines it, using PythonCall.python_executable_path() as the interpreter and PythonCall's own importer. RustCall.pyo3_host_available() says whether it is defined.

The crate is built unmodified. A #[pyfunction] fn f(...) that is not pub, a signature using Python<'_> or pyo3::Bound, numpy arrays and Python callables all become reachable, because the call goes through CPython and the crate's own #[pymodule] registration rather than through a Rust path.

pyo3_host_import(artifact::PyO3Extension) -> Py

The import alone: the module build_pyo3_extension already built (#449). The one-argument form is build_pyo3_extension(crate; python = PythonCall.python_executable_path(), ...) followed by this, and the build needs no interpreter, so a package can run it in its __init__, keep the artifact, and leave only the import to the first call. (A deps/build.jl is another process: its artifact cannot be kept, but the cache it fills spares the runtime's pyo3_host_import(crate) the build.)

source
RustCall.pyo3_host_python — Function
pyo3_host_python() -> String

The interpreter the PyO3 host path builds a crate for and imports it into — PythonCall.python_executable_path(), supplied by RustCallPyO3HostExt. The build (pyo3_host_import) and the scan the bindings are generated from (generate_pyo3_host_bindings) both ask this, so both configure the crate for the same Python (#514 review). Without the extension it has no method, and _pyo3_host_default_python answers "".

source