Skip to content

Create a binding generator trait#2872

Merged
messense merged 4 commits intoPyO3:mainfrom
e-nomem:binding-generator
Nov 28, 2025
Merged

Create a binding generator trait#2872
messense merged 4 commits intoPyO3:mainfrom
e-nomem:binding-generator

Conversation

@e-nomem
Copy link
Copy Markdown
Contributor

@e-nomem e-nomem commented Nov 24, 2025

The goal is to deduplicate the code building the archive and adding type stubs, etc. This PR only adds the base trait and implements it for the PyO3 bindings. Once this PR looks good, I'll migrate the other bindings one at a time in separate PRs.

@e-nomem e-nomem force-pushed the binding-generator branch 6 times, most recently from afad3fa to 28ccf73 Compare November 25, 2025 19:32
@e-nomem e-nomem marked this pull request as ready for review November 25, 2025 19:57
@messense messense requested review from Copilot and messense November 26, 2025 03:20
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a BindingGenerator trait to deduplicate code for building archives and managing type stubs across different binding types. Currently, only PyO3 bindings are migrated to use this new trait, with other binding types (CFFI, UniFfi, WASM) to follow in separate PRs.

Key changes:

  • Introduced BindingGenerator trait and GeneratorOutput struct to standardize binding generation interface
  • Refactored PyO3 binding generation from a standalone function into a trait-based implementation (Pyo3BindingGenerator)
  • Centralized common logic (Python part installation, artifact copying, type stub handling) into generate_binding() function

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 6 comments.

File Description
src/binding_generator/mod.rs Added BindingGenerator trait, GeneratorOutput struct, and central generate_binding() function that handles the common workflow of installing Python files, artifacts, and type stubs
src/binding_generator/pyo3_binding.rs Refactored from function-based to trait-based implementation; Pyo3BindingGenerator now implements BindingGenerator trait and focuses on PyO3-specific logic (filename generation, AIX archive handling, __init__.py generation)
src/build_context.rs Updated to use new Pyo3BindingGenerator and generate_binding() instead of deprecated write_bindings_module() function
src/module_writer/path_writer.rs Added #[cfg(target_family = "unix")] guard to default_permission import to match its Unix-only usage

Comment thread src/binding_generator/pyo3_binding.rs
Comment on lines +107 to +112
let temp_dir = tempdir()?;
let GeneratorOutput {
artifact_target,
artifact_source_override,
additional_files,
} = generator.generate_bindings(context, interpreter, artifact, &module, &temp_dir)?;
Copy link

Copilot AI Nov 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] A temporary directory is now created for every build, even when not needed (only AIX builds require it for unpacking big archives). Consider creating it lazily or making it optional to avoid unnecessary filesystem operations for most builds.

Suggested change
let temp_dir = tempdir()?;
let GeneratorOutput {
artifact_target,
artifact_source_override,
additional_files,
} = generator.generate_bindings(context, interpreter, artifact, &module, &temp_dir)?;
// Only create a temporary directory for AIX builds, as required.
#[allow(unused_mut)]
let mut temp_dir = None;
let temp_dir_path = if cfg!(target_os = "aix") {
temp_dir = Some(tempdir()?);
temp_dir.as_ref().unwrap().path()
} else {
Path::new("")
};
let GeneratorOutput {
artifact_target,
artifact_source_override,
additional_files,
} = generator.generate_bindings(context, interpreter, artifact, &module, temp_dir_path)?;

Copilot uses AI. Check for mistakes.
Comment thread src/binding_generator/mod.rs
Comment thread src/binding_generator/mod.rs
Comment thread src/binding_generator/mod.rs
Comment thread src/binding_generator/mod.rs Outdated
Comment on lines +31 to +40
pub trait BindingGenerator {
fn generate_bindings(
&self,
context: &BuildContext,
interpreter: Option<&PythonInterpreter>,
artifact: &BuildArtifact,
module: &Path,
temp_dir: &TempDir,
) -> Result<GeneratorOutput>;
}
Copy link

Copilot AI Nov 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The BindingGenerator trait lacks documentation. Consider adding a doc comment that explains its purpose, when it should be implemented, and what each method parameter represents.

Copilot uses AI. Check for mistakes.
@messense messense merged commit ee4f164 into PyO3:main Nov 28, 2025
45 checks passed
@e-nomem e-nomem deleted the binding-generator branch November 28, 2025 04:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants