Skip to content

Commit ae91986

Browse files
authored
Add auto-generated ctypes bindings to C API (#16067)
* Add auto-generated `ctypes` bindings to C API This is similar to how Python exposes its own C API through `ctypes` as `ctypes.pythonapi`. It's unlikely to be immediately useful as a feature for downstream users, but it simplifies the process of us testing our own code from Python space. I had originally hoped / thought that `numba` would support calling these `ctypes` functions, but while `numba` (as of current version 0.65) does have support for calling `ctypes` functions defined purely in terms of primitive non-pointer integral types, it doesn't yet support `ctypes.py_object` or arbitrary pointers. * Remove unnecessary fast path
1 parent 7d1d9a0 commit ae91986

11 files changed

Lines changed: 700 additions & 24 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ __pycache__/
2020
*.so
2121
# Generated C header files included in package.
2222
/qiskit/capi/include
23+
/qiskit/capi/_ctypes.py
2324

2425
# Distribution / packaging
2526
.Python

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/bindgen/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ name = "qiskit_bindgen"
1616
anyhow.workspace = true
1717
cbindgen = { workspace = true, features = ["unstable_ir"] }
1818
hashbrown.workspace = true
19+
regex.version = "1.12"

crates/bindgen/src/lib.rs

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,31 @@ pub static PYTHON_BINDING_FEATURE: &str = "python_binding";
4848
pub static PYTHON_BINDING_DEFINE: &str = "QISKIT_C_PYTHON_INTERFACE";
4949

5050
pub static COPYRIGHT: &str = "\
51-
// This code is part of Qiskit.
52-
//
53-
// (C) Copyright IBM 2026
54-
//
55-
// This code is licensed under the Apache License, Version 2.0. You may
56-
// obtain a copy of this license in the LICENSE.txt file in the root directory
57-
// of this source tree or at https://www.apache.org/licenses/LICENSE-2.0.
58-
//
59-
// Any modifications or derivative works of this code must retain this
60-
// copyright notice, and modified files need to carry a notice indicating
61-
// that they have been altered from the originals.
51+
This code is part of Qiskit.
52+
53+
(C) Copyright IBM 2026
54+
55+
This code is licensed under the Apache License, Version 2.0. You may
56+
obtain a copy of this license in the LICENSE.txt file in the root directory
57+
of this source tree or at https://www.apache.org/licenses/LICENSE-2.0.
58+
59+
Any modifications or derivative works of this code must retain this
60+
copyright notice, and modified files need to carry a notice indicating
61+
that they have been altered from the originals.
6262
";
63+
pub fn copyright_with_line_comments(comment: &str) -> String {
64+
use std::fmt::Write;
65+
66+
let mut out = String::new();
67+
for line in COPYRIGHT.lines() {
68+
if line.is_empty() {
69+
_ = writeln!(out, "{comment}");
70+
} else {
71+
_ = writeln!(out, "{comment} {line}");
72+
}
73+
}
74+
out
75+
}
6376

6477
/// Crates that contain definitions of objects that are exposed through the C API.
6578
pub static QISKIT_PUBLIC_API_CRATES: &[&str] =
@@ -182,7 +195,7 @@ fn get_config() -> anyhow::Result<cbindgen::Config> {
182195
.map(|&(cfg, def)| (format!("feature = {cfg}"), String::from(def)))
183196
.collect();
184197
Ok(cbindgen::Config {
185-
header: Some(COPYRIGHT.to_owned()),
198+
header: Some(copyright_with_line_comments("//")),
186199
language: cbindgen::Language::C,
187200
includes,
188201
include_version: true,

crates/bindgen/src/render/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@
1111
// that they have been altered from the originals.
1212

1313
pub mod c;
14+
pub mod py_ctypes;

0 commit comments

Comments
 (0)