From 250b77aa063d4f5bef774ac998cc90d0d4d873f6 Mon Sep 17 00:00:00 2001 From: Cary Hawkins Date: Wed, 18 Nov 2026 09:09:26 -0800 Subject: [PATCH 1/4] feat: initial scaffolding for cpython-api crate --- Cargo.lock | 7 +++++++ Cargo.toml | 4 ++-- Modules/cpython-api/Cargo.toml | 16 ++++++++++++++++ Modules/cpython-api/src/lib.rs | 1 + 4 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 Modules/cpython-api/Cargo.toml create mode 100644 Modules/cpython-api/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 14e6791704c1963..70c9773ee83c4f7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -71,6 +71,13 @@ dependencies = [ "libloading", ] +[[package]] +name = "cpython-api" +version = "0.1.0" +dependencies = [ + "cpython-sys", +] + [[package]] name = "cpython-build-helper" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 0eecc3f7643a67b..33d8d31c120bf5a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,8 @@ [workspace] resolver = "3" members = [ - "Modules/_base64", "Modules/cpython-build-helper", "Modules/cpython-rust-staticlib", - "Modules/cpython-sys" + "Modules/_base64", "Modules/cpython-api", "Modules/cpython-build-helper", + "Modules/cpython-rust-staticlib", "Modules/cpython-sys" ] [workspace.package] diff --git a/Modules/cpython-api/Cargo.toml b/Modules/cpython-api/Cargo.toml new file mode 100644 index 000000000000000..be540465147b77d --- /dev/null +++ b/Modules/cpython-api/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "cpython-api" +version = "0.1.0" +edition = "2024" +rust-version.workspace = true +publish = false + +[dependencies] +cpython-sys = { path = "../cpython-sys" } + +[lints.rust] +unsafe_op_in_unsafe_fn = "deny" + +[lints.clippy] +undocumented_unsafe_blocks = "deny" +missing_safety_doc = "deny" diff --git a/Modules/cpython-api/src/lib.rs b/Modules/cpython-api/src/lib.rs new file mode 100644 index 000000000000000..a86ca9f0857ef49 --- /dev/null +++ b/Modules/cpython-api/src/lib.rs @@ -0,0 +1 @@ +//! Safe Rust API for in-tree CPython extension modules. From 5e7dff133cdf795b17bfd5e21db296e484e7881e Mon Sep 17 00:00:00 2001 From: Cary Hawkins Date: Wed, 18 Nov 2026 09:50:39 -0800 Subject: [PATCH 2/4] Add scaffolding for cpython-api crate. --- Modules/_base64/src/lib.rs | 8 ++++---- Modules/cpython-api/src/c_api/bytes.rs | 17 ++++++++++++++++ Modules/cpython-api/src/c_api/err.rs | 15 ++++++++++++++ Modules/cpython-api/src/c_api/mod.rs | 8 ++++++++ Modules/cpython-api/src/c_api/object.rs | 13 ++++++++++++ Modules/cpython-api/src/c_api/refcount.rs | 9 +++++++++ Modules/cpython-api/src/lib.rs | 5 +++++ Modules/cpython-api/src/object.rs | 24 +++++++++++++++++++++++ Modules/cpython-sys/build.rs | 1 - Modules/cpython-sys/src/lib.rs | 20 +++++-------------- 10 files changed, 100 insertions(+), 20 deletions(-) create mode 100644 Modules/cpython-api/src/c_api/bytes.rs create mode 100644 Modules/cpython-api/src/c_api/err.rs create mode 100644 Modules/cpython-api/src/c_api/mod.rs create mode 100644 Modules/cpython-api/src/c_api/object.rs create mode 100644 Modules/cpython-api/src/c_api/refcount.rs create mode 100644 Modules/cpython-api/src/object.rs diff --git a/Modules/_base64/src/lib.rs b/Modules/_base64/src/lib.rs index 64e4d718789372c..d4518fbca83bb2b 100644 --- a/Modules/_base64/src/lib.rs +++ b/Modules/_base64/src/lib.rs @@ -82,10 +82,10 @@ struct BorrowedBuffer { } impl BorrowedBuffer { - fn from_object(obj: &PyObject) -> Result { + fn from_object(obj: *mut PyObject) -> Result { let mut view = MaybeUninit::::uninit(); let buffer = unsafe { - if PyObject_GetBuffer(obj.as_raw(), view.as_mut_ptr(), PYBUF_SIMPLE) != 0 { + if PyObject_GetBuffer(obj, view.as_mut_ptr(), PYBUF_SIMPLE) != 0 { return Err(()); } Self { @@ -130,7 +130,7 @@ pub unsafe extern "C" fn standard_b64encode( return ptr::null_mut(); } - let source = unsafe { &**args }; + let source = unsafe { *args }; // Safe cast by Safety match standard_b64encode_impl(source) { @@ -139,7 +139,7 @@ pub unsafe extern "C" fn standard_b64encode( } } -fn standard_b64encode_impl(source: &PyObject) -> Result<*mut PyObject, ()> { +fn standard_b64encode_impl(source: *mut PyObject) -> Result<*mut PyObject, ()> { let buffer = match BorrowedBuffer::from_object(source) { Ok(buf) => buf, Err(_) => return Err(()), diff --git a/Modules/cpython-api/src/c_api/bytes.rs b/Modules/cpython-api/src/c_api/bytes.rs new file mode 100644 index 000000000000000..44161971867f098 --- /dev/null +++ b/Modules/cpython-api/src/c_api/bytes.rs @@ -0,0 +1,17 @@ +use std::ffi::c_char; + +use cpython_sys::Py_ssize_t; + +use crate::PyAny; + +pub(crate) unsafe fn PyBytes_FromStringAndSize(v: *const c_char, len: Py_ssize_t) -> *mut PyAny { + todo!() +} + +pub(crate) unsafe fn PyBytes_AsString(o: *mut PyAny) -> *mut c_char { + todo!() +} + +pub(crate) unsafe fn PyBytes_Size(o: *mut PyAny) -> Py_ssize_t { + todo!() +} diff --git a/Modules/cpython-api/src/c_api/err.rs b/Modules/cpython-api/src/c_api/err.rs new file mode 100644 index 000000000000000..3cde998072d2e4f --- /dev/null +++ b/Modules/cpython-api/src/c_api/err.rs @@ -0,0 +1,15 @@ +use std::ffi::c_char; + +use crate::PyAny; + +pub(crate) unsafe fn PyErr_SetString(exception: *mut PyAny, message: *const c_char) { + todo!() +} + +pub(crate) unsafe fn PyErr_NoMemory() -> *mut PyAny { + todo!() +} + +pub(crate) unsafe fn PyErr_Occurred() -> *mut PyAny { + todo!() +} diff --git a/Modules/cpython-api/src/c_api/mod.rs b/Modules/cpython-api/src/c_api/mod.rs new file mode 100644 index 000000000000000..d92363628ed7d6e --- /dev/null +++ b/Modules/cpython-api/src/c_api/mod.rs @@ -0,0 +1,8 @@ +#![allow(dead_code)] +#![allow(non_snake_case)] +#![allow(unused_variables)] + +mod bytes; +mod err; +mod object; +mod refcount; diff --git a/Modules/cpython-api/src/c_api/object.rs b/Modules/cpython-api/src/c_api/object.rs new file mode 100644 index 000000000000000..adf373472d659bf --- /dev/null +++ b/Modules/cpython-api/src/c_api/object.rs @@ -0,0 +1,13 @@ +use std::ffi::c_int; + +use cpython_sys::PyTypeObject; + +use crate::PyAny; + +pub(crate) unsafe fn Py_TYPE(ob: *mut PyAny) -> *mut PyTypeObject { + todo!() +} + +pub(crate) unsafe fn Py_IsNone(x: *mut PyAny) -> c_int { + todo!() +} diff --git a/Modules/cpython-api/src/c_api/refcount.rs b/Modules/cpython-api/src/c_api/refcount.rs new file mode 100644 index 000000000000000..4ce674d27f8d329 --- /dev/null +++ b/Modules/cpython-api/src/c_api/refcount.rs @@ -0,0 +1,9 @@ +use crate::PyAny; + +pub(crate) unsafe fn Py_IncRef(op: *mut PyAny) { + todo!() +} + +pub(crate) unsafe fn Py_DecRef(op: *mut PyAny) { + todo!() +} diff --git a/Modules/cpython-api/src/lib.rs b/Modules/cpython-api/src/lib.rs index a86ca9f0857ef49..2752c7550c50938 100644 --- a/Modules/cpython-api/src/lib.rs +++ b/Modules/cpython-api/src/lib.rs @@ -1 +1,6 @@ //! Safe Rust API for in-tree CPython extension modules. + +mod c_api; +mod object; + +pub use object::PyAny; diff --git a/Modules/cpython-api/src/object.rs b/Modules/cpython-api/src/object.rs new file mode 100644 index 000000000000000..c362feea2ea2fd1 --- /dev/null +++ b/Modules/cpython-api/src/object.rs @@ -0,0 +1,24 @@ +use std::cell::UnsafeCell; + +use cpython_sys::PyObject; + +#[repr(transparent)] +pub struct PyAny(UnsafeCell); + +#[cfg(test)] +mod tests { + use std::mem::{align_of, size_of}; + + use super::*; + + #[test] + fn layout_matches_pyobject() { + assert_eq!(size_of::(), size_of::()); + assert_eq!(align_of::(), align_of::()); + } + + #[test] + fn pointer_layout_matches_pyobject() { + assert_eq!(size_of::<*mut PyAny>(), size_of::<*mut PyObject>()); + } +} diff --git a/Modules/cpython-sys/build.rs b/Modules/cpython-sys/build.rs index 385f99a466594d4..2a0a6b8c0c5d168 100644 --- a/Modules/cpython-sys/build.rs +++ b/Modules/cpython-sys/build.rs @@ -289,7 +289,6 @@ fn generate_c_api_bindings( .allowlist_type("_?Py.*") .allowlist_var("_?Py.*") .blocklist_type("^PyMethodDef$") - .blocklist_type("PyObject") .parse_callbacks(Box::new(bindgen::CargoCallbacks::new())) .generate() .expect("Unable to generate bindings"); diff --git a/Modules/cpython-sys/src/lib.rs b/Modules/cpython-sys/src/lib.rs index 17924f0e5fb00a9..9916b09d3b44743 100644 --- a/Modules/cpython-sys/src/lib.rs +++ b/Modules/cpython-sys/src/lib.rs @@ -57,16 +57,6 @@ pub const _Py_STATIC_IMMORTAL_INITIAL_REFCNT: Py_ssize_t = #[cfg(not(target_pointer_width = "64"))] pub const _Py_STATIC_IMMORTAL_INITIAL_REFCNT: Py_ssize_t = (7u32 << 28) as Py_ssize_t; -#[repr(transparent)] -pub struct PyObject(std::cell::UnsafeCell<_object>); - -impl PyObject { - #[inline] - pub fn as_raw(&self) -> *mut Self { - self.0.get() as *mut Self - } -} - #[repr(C)] pub union PyMethodDefFuncPointer { pub PyCFunction: unsafe extern "C" fn(slf: *mut PyObject, args: *mut PyObject) -> *mut PyObject, @@ -126,25 +116,25 @@ unsafe impl Send for PyMethodDef {} pub const PyObject_HEAD_INIT: PyObject = { let mut obj: _object = unsafe { std::mem::MaybeUninit::zeroed().assume_init() }; obj.ob_flags = _Py_STATICALLY_ALLOCATED_FLAG as _; - PyObject(std::cell::UnsafeCell::new(obj)) + obj }; #[cfg(all(not(py_gil_disabled), target_pointer_width = "64"))] -pub const PyObject_HEAD_INIT: PyObject = PyObject(std::cell::UnsafeCell::new(_object { +pub const PyObject_HEAD_INIT: PyObject = _object { __bindgen_anon_1: _object__bindgen_ty_1 { ob_refcnt_full: _Py_STATIC_IMMORTAL_INITIAL_REFCNT as i64, }, ob_type: std::ptr::null_mut(), -})); +}; // On 32-bit platforms, the refcount union only has ob_refcnt (no ob_refcnt_split). #[cfg(all(not(py_gil_disabled), not(target_pointer_width = "64")))] -pub const PyObject_HEAD_INIT: PyObject = PyObject(std::cell::UnsafeCell::new(_object { +pub const PyObject_HEAD_INIT: PyObject = _object { __bindgen_anon_1: _object__bindgen_ty_1 { ob_refcnt: _Py_STATIC_IMMORTAL_INITIAL_REFCNT, }, ob_type: std::ptr::null_mut(), -})); +}; pub const PyModuleDef_HEAD_INIT: PyModuleDef_Base = PyModuleDef_Base { ob_base: PyObject_HEAD_INIT, From 16293d601795d4eb6ea56092595fd4b87a5ce98a Mon Sep 17 00:00:00 2001 From: Cary Hawkins Date: Fri, 20 Nov 2026 12:05:40 -0800 Subject: [PATCH 3/4] Remove _Base64 and c_api for now until we are ready for it. --- Cargo.lock | 11 - Cargo.toml | 4 +- Modules/Setup | 1 - Modules/Setup.stdlib.in | 1 - Modules/_base64/Cargo.toml | 16 -- Modules/_base64/build.rs | 5 - Modules/_base64/src/lib.rs | 249 -------------------- Modules/cpython-api/src/c_api/bytes.rs | 17 -- Modules/cpython-api/src/c_api/err.rs | 15 -- Modules/cpython-api/src/c_api/mod.rs | 8 - Modules/cpython-api/src/c_api/object.rs | 13 - Modules/cpython-api/src/c_api/refcount.rs | 9 - Modules/cpython-api/src/lib.rs | 1 - Modules/cpython-rust-staticlib/Cargo.toml | 1 - Modules/cpython-rust-staticlib/src/lib.rs | 2 +- PCbuild/_base64.vcxproj | 164 ------------- PCbuild/_base64.vcxproj.filters | 19 -- PCbuild/pcbuild.proj | 2 +- PCbuild/pcbuild.sln | 35 --- PCbuild/readme.txt | 1 - Python/stdlib_module_names.h | 1 - Tools/build/generate_stdlib_module_names.py | 1 + configure | 47 ---- configure.ac | 4 - 24 files changed, 5 insertions(+), 622 deletions(-) delete mode 100644 Modules/_base64/Cargo.toml delete mode 100644 Modules/_base64/build.rs delete mode 100644 Modules/_base64/src/lib.rs delete mode 100644 Modules/cpython-api/src/c_api/bytes.rs delete mode 100644 Modules/cpython-api/src/c_api/err.rs delete mode 100644 Modules/cpython-api/src/c_api/mod.rs delete mode 100644 Modules/cpython-api/src/c_api/object.rs delete mode 100644 Modules/cpython-api/src/c_api/refcount.rs delete mode 100644 PCbuild/_base64.vcxproj delete mode 100644 PCbuild/_base64.vcxproj.filters diff --git a/Cargo.lock b/Cargo.lock index 70c9773ee83c4f7..f9cc395a47ec0c5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,14 +2,6 @@ # It is not intended for manual editing. version = 4 -[[package]] -name = "_base64" -version = "0.1.0" -dependencies = [ - "cpython-build-helper", - "cpython-sys", -] - [[package]] name = "aho-corasick" version = "1.1.5" @@ -88,9 +80,6 @@ dependencies = [ [[package]] name = "cpython-rust-staticlib" version = "0.1.0" -dependencies = [ - "_base64", -] [[package]] name = "cpython-sys" diff --git a/Cargo.toml b/Cargo.toml index 33d8d31c120bf5a..a9e1bfad048884f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,8 @@ [workspace] resolver = "3" members = [ - "Modules/_base64", "Modules/cpython-api", "Modules/cpython-build-helper", - "Modules/cpython-rust-staticlib", "Modules/cpython-sys" + "Modules/cpython-api", "Modules/cpython-build-helper", "Modules/cpython-rust-staticlib", + "Modules/cpython-sys" ] [workspace.package] diff --git a/Modules/Setup b/Modules/Setup index d313bd1fedc186b..0a183ae30f8f0ae 100644 --- a/Modules/Setup +++ b/Modules/Setup @@ -182,7 +182,6 @@ PYTHONPATH=$(COREPYTHONPATH) #_codecs_tw cjkcodecs/_codecs_tw.c #_multibytecodec cjkcodecs/multibytecodec.c #unicodedata unicodedata.c -#_base64 _base64/Cargo.toml _base64/src/lib.rs lib_base64.a # Modules with some UNIX dependencies diff --git a/Modules/Setup.stdlib.in b/Modules/Setup.stdlib.in index e5300fcb481a08b..60d2c7533facb5c 100644 --- a/Modules/Setup.stdlib.in +++ b/Modules/Setup.stdlib.in @@ -118,7 +118,6 @@ ############################################################################ # Rust modules # -@MODULE__BASE64_TRUE@_base64 _base64/Cargo.toml _base64/src/lib.rs ############################################################################ # Modules with some UNIX dependencies diff --git a/Modules/_base64/Cargo.toml b/Modules/_base64/Cargo.toml deleted file mode 100644 index dbe739fa7a19b46..000000000000000 --- a/Modules/_base64/Cargo.toml +++ /dev/null @@ -1,16 +0,0 @@ -[package] -name = "_base64" -version = "0.1.0" -edition = "2024" -rust-version.workspace = true - -[dependencies] -cpython-sys ={ path = "../cpython-sys" } - -[build-dependencies] -cpython-build-helper = { path = "../cpython-build-helper" } - -[lib] -name = "_base64" -# For shared builds, we generate a c dynamic library. Static builds use the rlib -crate-type = ["cdylib", "rlib"] diff --git a/Modules/_base64/build.rs b/Modules/_base64/build.rs deleted file mode 100644 index 8cca32f8f8c3dff..000000000000000 --- a/Modules/_base64/build.rs +++ /dev/null @@ -1,5 +0,0 @@ -use cpython_build_helper::print_linker_args; - -fn main() { - print_linker_args(); -} diff --git a/Modules/_base64/src/lib.rs b/Modules/_base64/src/lib.rs deleted file mode 100644 index d4518fbca83bb2b..000000000000000 --- a/Modules/_base64/src/lib.rs +++ /dev/null @@ -1,249 +0,0 @@ -use std::cell::UnsafeCell; -use std::ffi::{c_char, c_int, c_void}; -use std::mem::MaybeUninit; -use std::ptr; -use std::slice; - -use cpython_sys::METH_FASTCALL; -use cpython_sys::Py_DecRef; -use cpython_sys::Py_buffer; -use cpython_sys::Py_ssize_t; -use cpython_sys::PyBuffer_Release; -use cpython_sys::PyBytes_AsString; -use cpython_sys::PyBytes_FromStringAndSize; -use cpython_sys::PyErr_NoMemory; -use cpython_sys::PyErr_SetString; -use cpython_sys::PyExc_TypeError; -use cpython_sys::PyMethodDef; -use cpython_sys::PyMethodDefFuncPointer; -use cpython_sys::PyModuleDef; -use cpython_sys::PyModuleDef_HEAD_INIT; -use cpython_sys::PyModuleDef_Init; -use cpython_sys::PyObject; -use cpython_sys::PyObject_GetBuffer; - -const PYBUF_SIMPLE: c_int = 0; -const PAD_BYTE: u8 = b'='; -const ENCODE_TABLE: [u8; 64] = *b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - -#[inline] -fn encoded_output_len(input_len: usize) -> Option { - input_len - .checked_add(2) - .map(|n| n / 3) - .and_then(|blocks| blocks.checked_mul(4)) -} - -#[inline] -fn encode_into(input: &[u8], output: &mut [u8]) -> usize { - let mut src_index = 0; - let mut dst_index = 0; - let len = input.len(); - - while src_index + 3 <= len { - let chunk = (u32::from(input[src_index]) << 16) - | (u32::from(input[src_index + 1]) << 8) - | u32::from(input[src_index + 2]); - output[dst_index] = ENCODE_TABLE[((chunk >> 18) & 0x3f) as usize]; - output[dst_index + 1] = ENCODE_TABLE[((chunk >> 12) & 0x3f) as usize]; - output[dst_index + 2] = ENCODE_TABLE[((chunk >> 6) & 0x3f) as usize]; - output[dst_index + 3] = ENCODE_TABLE[(chunk & 0x3f) as usize]; - src_index += 3; - dst_index += 4; - } - - match len - src_index { - 0 => {} - 1 => { - let chunk = u32::from(input[src_index]) << 16; - output[dst_index] = ENCODE_TABLE[((chunk >> 18) & 0x3f) as usize]; - output[dst_index + 1] = ENCODE_TABLE[((chunk >> 12) & 0x3f) as usize]; - output[dst_index + 2] = PAD_BYTE; - output[dst_index + 3] = PAD_BYTE; - dst_index += 4; - } - 2 => { - let chunk = - (u32::from(input[src_index]) << 16) | (u32::from(input[src_index + 1]) << 8); - output[dst_index] = ENCODE_TABLE[((chunk >> 18) & 0x3f) as usize]; - output[dst_index + 1] = ENCODE_TABLE[((chunk >> 12) & 0x3f) as usize]; - output[dst_index + 2] = ENCODE_TABLE[((chunk >> 6) & 0x3f) as usize]; - output[dst_index + 3] = PAD_BYTE; - dst_index += 4; - } - _ => unreachable!("len - src_index cannot exceed 2"), - } - - dst_index -} - -struct BorrowedBuffer { - view: Py_buffer, -} - -impl BorrowedBuffer { - fn from_object(obj: *mut PyObject) -> Result { - let mut view = MaybeUninit::::uninit(); - let buffer = unsafe { - if PyObject_GetBuffer(obj, view.as_mut_ptr(), PYBUF_SIMPLE) != 0 { - return Err(()); - } - Self { - view: view.assume_init(), - } - }; - Ok(buffer) - } - - fn len(&self) -> Py_ssize_t { - self.view.len - } - - fn as_ptr(&self) -> *const u8 { - self.view.buf.cast::() as *const u8 - } -} - -impl Drop for BorrowedBuffer { - fn drop(&mut self) { - unsafe { - PyBuffer_Release(&mut self.view); - } - } -} - -/// # Safety -/// `module` must be a valid pointer of PyObject representing the module. -/// `args` must be a valid pointer to an array of valid PyObject pointers with length `nargs`. -pub unsafe extern "C" fn standard_b64encode( - _module: *mut PyObject, - args: *mut *mut PyObject, - nargs: Py_ssize_t, -) -> *mut PyObject { - if nargs != 1 { - unsafe { - PyErr_SetString( - PyExc_TypeError, - c"standard_b64encode() takes exactly one argument".as_ptr(), - ); - } - return ptr::null_mut(); - } - - let source = unsafe { *args }; - - // Safe cast by Safety - match standard_b64encode_impl(source) { - Ok(result) => result, - Err(_) => ptr::null_mut(), - } -} - -fn standard_b64encode_impl(source: *mut PyObject) -> Result<*mut PyObject, ()> { - let buffer = match BorrowedBuffer::from_object(source) { - Ok(buf) => buf, - Err(_) => return Err(()), - }; - - let view_len = buffer.len(); - if view_len < 0 { - unsafe { - PyErr_SetString( - PyExc_TypeError, - c"standard_b64encode() argument has negative length".as_ptr(), - ); - } - return Err(()); - } - - let input_len = view_len as usize; - let input = unsafe { slice::from_raw_parts(buffer.as_ptr(), input_len) }; - - let Some(output_len) = encoded_output_len(input_len) else { - unsafe { - PyErr_NoMemory(); - } - return Err(()); - }; - - if output_len > isize::MAX as usize { - unsafe { - PyErr_NoMemory(); - } - return Err(()); - } - - let result = unsafe { PyBytes_FromStringAndSize(ptr::null(), output_len as Py_ssize_t) }; - if result.is_null() { - return Err(()); - } - - let dest_ptr = unsafe { PyBytes_AsString(result) }; - if dest_ptr.is_null() { - unsafe { - Py_DecRef(result); - } - return Err(()); - } - let dest = unsafe { slice::from_raw_parts_mut(dest_ptr.cast::(), output_len) }; - - let written = encode_into(input, dest); - debug_assert_eq!(written, output_len); - Ok(result) -} - -pub extern "C" fn _base64_clear(_obj: *mut PyObject) -> c_int { - //TODO - 0 -} - -pub extern "C" fn _base64_free(_o: *mut c_void) { - //TODO -} - -pub struct ModuleDef { - ffi: UnsafeCell, -} - -impl ModuleDef { - fn init_multi_phase(&'static self) -> *mut PyObject { - unsafe { PyModuleDef_Init(self.ffi.get()) } - } -} - -unsafe impl Sync for ModuleDef {} - -pub static _BASE64_MODULE_METHODS: [PyMethodDef; 2] = { - [ - PyMethodDef { - ml_name: c"standard_b64encode".as_ptr() as *mut c_char, - ml_meth: PyMethodDefFuncPointer { - PyCFunctionFast: standard_b64encode, - }, - ml_flags: METH_FASTCALL, - ml_doc: c"Demo for the _base64 module".as_ptr() as *mut c_char, - }, - PyMethodDef::zeroed(), - ] -}; - -pub static _BASE64_MODULE: ModuleDef = { - ModuleDef { - ffi: UnsafeCell::new(PyModuleDef { - m_base: PyModuleDef_HEAD_INIT, - m_name: c"_base64".as_ptr() as *mut _, - m_doc: c"A test Rust module".as_ptr() as *mut _, - m_size: 0, - m_methods: &_BASE64_MODULE_METHODS as *const PyMethodDef as *mut _, - m_slots: std::ptr::null_mut(), - m_traverse: None, - m_clear: Some(_base64_clear), - m_free: Some(_base64_free), - }), - } -}; - -#[unsafe(no_mangle)] -pub extern "C" fn PyInit__base64() -> *mut PyObject { - _BASE64_MODULE.init_multi_phase() -} diff --git a/Modules/cpython-api/src/c_api/bytes.rs b/Modules/cpython-api/src/c_api/bytes.rs deleted file mode 100644 index 44161971867f098..000000000000000 --- a/Modules/cpython-api/src/c_api/bytes.rs +++ /dev/null @@ -1,17 +0,0 @@ -use std::ffi::c_char; - -use cpython_sys::Py_ssize_t; - -use crate::PyAny; - -pub(crate) unsafe fn PyBytes_FromStringAndSize(v: *const c_char, len: Py_ssize_t) -> *mut PyAny { - todo!() -} - -pub(crate) unsafe fn PyBytes_AsString(o: *mut PyAny) -> *mut c_char { - todo!() -} - -pub(crate) unsafe fn PyBytes_Size(o: *mut PyAny) -> Py_ssize_t { - todo!() -} diff --git a/Modules/cpython-api/src/c_api/err.rs b/Modules/cpython-api/src/c_api/err.rs deleted file mode 100644 index 3cde998072d2e4f..000000000000000 --- a/Modules/cpython-api/src/c_api/err.rs +++ /dev/null @@ -1,15 +0,0 @@ -use std::ffi::c_char; - -use crate::PyAny; - -pub(crate) unsafe fn PyErr_SetString(exception: *mut PyAny, message: *const c_char) { - todo!() -} - -pub(crate) unsafe fn PyErr_NoMemory() -> *mut PyAny { - todo!() -} - -pub(crate) unsafe fn PyErr_Occurred() -> *mut PyAny { - todo!() -} diff --git a/Modules/cpython-api/src/c_api/mod.rs b/Modules/cpython-api/src/c_api/mod.rs deleted file mode 100644 index d92363628ed7d6e..000000000000000 --- a/Modules/cpython-api/src/c_api/mod.rs +++ /dev/null @@ -1,8 +0,0 @@ -#![allow(dead_code)] -#![allow(non_snake_case)] -#![allow(unused_variables)] - -mod bytes; -mod err; -mod object; -mod refcount; diff --git a/Modules/cpython-api/src/c_api/object.rs b/Modules/cpython-api/src/c_api/object.rs deleted file mode 100644 index adf373472d659bf..000000000000000 --- a/Modules/cpython-api/src/c_api/object.rs +++ /dev/null @@ -1,13 +0,0 @@ -use std::ffi::c_int; - -use cpython_sys::PyTypeObject; - -use crate::PyAny; - -pub(crate) unsafe fn Py_TYPE(ob: *mut PyAny) -> *mut PyTypeObject { - todo!() -} - -pub(crate) unsafe fn Py_IsNone(x: *mut PyAny) -> c_int { - todo!() -} diff --git a/Modules/cpython-api/src/c_api/refcount.rs b/Modules/cpython-api/src/c_api/refcount.rs deleted file mode 100644 index 4ce674d27f8d329..000000000000000 --- a/Modules/cpython-api/src/c_api/refcount.rs +++ /dev/null @@ -1,9 +0,0 @@ -use crate::PyAny; - -pub(crate) unsafe fn Py_IncRef(op: *mut PyAny) { - todo!() -} - -pub(crate) unsafe fn Py_DecRef(op: *mut PyAny) { - todo!() -} diff --git a/Modules/cpython-api/src/lib.rs b/Modules/cpython-api/src/lib.rs index 2752c7550c50938..ea619bd121fcf22 100644 --- a/Modules/cpython-api/src/lib.rs +++ b/Modules/cpython-api/src/lib.rs @@ -1,6 +1,5 @@ //! Safe Rust API for in-tree CPython extension modules. -mod c_api; mod object; pub use object::PyAny; diff --git a/Modules/cpython-rust-staticlib/Cargo.toml b/Modules/cpython-rust-staticlib/Cargo.toml index 2b21ab576a09c31..42b2ff52c6df988 100644 --- a/Modules/cpython-rust-staticlib/Cargo.toml +++ b/Modules/cpython-rust-staticlib/Cargo.toml @@ -5,7 +5,6 @@ edition = "2024" rust-version.workspace = true [dependencies] -_base64 ={ path = "../_base64" } [lib] name = "cpython_rust_staticlib" diff --git a/Modules/cpython-rust-staticlib/src/lib.rs b/Modules/cpython-rust-staticlib/src/lib.rs index a440707833f65d6..8b137891791fe96 100644 --- a/Modules/cpython-rust-staticlib/src/lib.rs +++ b/Modules/cpython-rust-staticlib/src/lib.rs @@ -1 +1 @@ -pub use _base64::PyInit__base64; + diff --git a/PCbuild/_base64.vcxproj b/PCbuild/_base64.vcxproj deleted file mode 100644 index 916069c1aea82aa..000000000000000 --- a/PCbuild/_base64.vcxproj +++ /dev/null @@ -1,164 +0,0 @@ - - - - - Debug - ARM - - - Debug - ARM64 - - - Debug - Win32 - - - Debug - x64 - - - PGInstrument - ARM - - - PGInstrument - ARM64 - - - PGInstrument - Win32 - - - PGInstrument - x64 - - - PGUpdate - ARM - - - PGUpdate - ARM64 - - - PGUpdate - Win32 - - - PGUpdate - x64 - - - Release - ARM - - - Release - ARM64 - - - Release - Win32 - - - Release - x64 - - - - {D7B7A122-80A0-4AFB-9C06-18C29B3178E4} - _base64 - MakeFileProj - - - - - Makefile - NotSet - false - - - - $(PyStdlibPydExt) - - - - - - - - - - <_ProjectFileVersion>10.0.30319.1 - llvm-21.1.4.0 - dev - release - debug - release - i686-pc-windows-msvc - x86_64-pc-windows-msvc - thumbv7a-pc-windows-msvc - aarch64-pc-windows-msvc - $(IntDir)cargo\ - $(CargoTargetDir.TrimEnd(`\`)) - $(BuildPath.TrimEnd(`\`)) - $(PySourcePath)PC - $(PySourcePath)Cargo.toml - $(LIBCLANG_PATH) - $(ExternalsDir)$(BundledLlvmTag)\bin - $(ExternalsDir)$(BundledLlvmTag)\bin - $(LLVMInstallDir)\bin - $(LLVMInstallDir)\bin - $(VCInstallDir)Tools\Llvm\x64\bin - $(VCInstallDir)Tools\Llvm\x64\bin - $(VCInstallDir)Tools\Llvm\bin - $(VCInstallDir)Tools\Llvm\bin - C:\Program Files\LLVM\bin - C:\Program Files\LLVM\bin - $(OutDir) - $(TargetDir)$(TargetName)$(TargetExt) - $(CargoTargetDirNoSlash)\$(CargoTarget)\$(CargoBuildSubdir)\_base64.dll - $(CargoTargetDirNoSlash)\$(CargoTarget)\$(CargoBuildSubdir)\_base64.pdb - $(OutDir)$(TargetName).pdb - setlocal -if "$(Configuration)"=="Debug" (set "PY_DEBUG=1") else (set "PY_DEBUG=") -if "$(DisableGil)"=="true" (set "PY_GIL_DISABLED=1") else (set "PY_GIL_DISABLED=") -where cargo >NUL 2>NUL || (echo WARNING: cargo was not found on PATH, skipping _base64 & exit /b 0) -if not "$(LibClangPath)"=="" set "LIBCLANG_PATH=$(LibClangPath)" -if "$(LibClangPath)"=="" if not defined LIBCLANG_PATH (echo WARNING: libclang was not found, skipping _base64 & exit /b 0) -if defined LIBCLANG_PATH if not exist "%LIBCLANG_PATH%\libclang.dll" if not exist "%LIBCLANG_PATH%\clang.dll" (echo WARNING: libclang.dll not found at %LIBCLANG_PATH%, skipping _base64 & exit /b 0) -if not exist "$(CargoTargetDirNoSlash)" mkdir "$(CargoTargetDirNoSlash)" -set "CARGO_TARGET_DIR=$(CargoTargetDirNoSlash)" -set "PYTHON_BUILD_DIR=$(PythonBuildDir)" -set "LIBPYTHON="/LIBPATH:$(BuildPathNoSlash)" "$(PyDllName).lib"" -cargo build --lib --locked --package _base64 --manifest-path "$(CargoWorkspaceManifest)" --profile $(CargoProfile) --target $(CargoTarget) -if errorlevel 1 exit /b 1 -if not exist "$(RustExtensionPath)" (echo ERROR: cargo did not produce $(RustExtensionPath) & exit /b 1) -copy /Y "$(RustExtensionPath)" "$(TargetPath)" >NUL -if errorlevel 1 exit /b 1 -if exist "$(RustExtensionPdb)" copy /Y "$(RustExtensionPdb)" "$(TargetPdb)" >NUL - if exist "$(TargetPath)" del /F /Q "$(TargetPath)" -if exist "$(TargetPdb)" del /F /Q "$(TargetPdb)" -if exist "$(CargoTargetDirNoSlash)" rmdir /S /Q "$(CargoTargetDirNoSlash)" - $(TargetPath) - - - - - - - - - {CF7AC3D1-E2DF-41D2-BEA6-1E2556CDEA26} - false - - - - - - - - - - diff --git a/PCbuild/_base64.vcxproj.filters b/PCbuild/_base64.vcxproj.filters deleted file mode 100644 index 0ba48e32c44518a..000000000000000 --- a/PCbuild/_base64.vcxproj.filters +++ /dev/null @@ -1,19 +0,0 @@ - - - - - {86A93473-99A3-4B0B-9B78-4A09FF3A0A6F} - - - - - Rust Sources - - - Rust Sources - - - Rust Sources - - - diff --git a/PCbuild/pcbuild.proj b/PCbuild/pcbuild.proj index 6e5848a201bf030..95e135705dac2ff 100644 --- a/PCbuild/pcbuild.proj +++ b/PCbuild/pcbuild.proj @@ -64,7 +64,7 @@ - + diff --git a/PCbuild/pcbuild.sln b/PCbuild/pcbuild.sln index e45d48b47d12c21..e2cac3158831eed 100644 --- a/PCbuild/pcbuild.sln +++ b/PCbuild/pcbuild.sln @@ -23,7 +23,6 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "python", "python.vcxproj", {28B5D777-DDF2-4B6B-B34F-31D938813856} = {28B5D777-DDF2-4B6B-B34F-31D938813856} {36D0C52C-DF4E-45D0-8BC7-E294C3ABC781} = {36D0C52C-DF4E-45D0-8BC7-E294C3ABC781} {384C224A-7474-476E-A01B-750EA7DE918C} = {384C224A-7474-476E-A01B-750EA7DE918C} - {D7B7A122-80A0-4AFB-9C06-18C29B3178E4} = {D7B7A122-80A0-4AFB-9C06-18C29B3178E4} {447F05A8-F581-4CAC-A466-5AC7936E207E} = {447F05A8-F581-4CAC-A466-5AC7936E207E} {4946ECAC-2E69-4BF8-A90A-F5136F5094DF} = {4946ECAC-2E69-4BF8-A90A-F5136F5094DF} {494BAC80-A60C-43A9-99E7-ACB691CE2C4D} = {494BAC80-A60C-43A9-99E7-ACB691CE2C4D} @@ -126,8 +125,6 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "_testconsole", "_testconsol EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "_asyncio", "_asyncio.vcxproj", "{384C224A-7474-476E-A01B-750EA7DE918C}" EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "_base64", "_base64.vcxproj", "{D7B7A122-80A0-4AFB-9C06-18C29B3178E4}" -EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "_zoneinfo", "_zoneinfo.vcxproj", "{FCBE1EF2-E0F0-40B1-88B5-00A35D378742}" EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "_queue", "_queue.vcxproj", "{78D80A15-BD8C-44E2-B49E-1F05B0A0A687}" @@ -1284,38 +1281,6 @@ Global {384C224A-7474-476E-A01B-750EA7DE918C}.Release|Win32.Build.0 = Release|Win32 {384C224A-7474-476E-A01B-750EA7DE918C}.Release|x64.ActiveCfg = Release|x64 {384C224A-7474-476E-A01B-750EA7DE918C}.Release|x64.Build.0 = Release|x64 - {D7B7A122-80A0-4AFB-9C06-18C29B3178E4}.Debug|ARM.ActiveCfg = Debug|ARM - {D7B7A122-80A0-4AFB-9C06-18C29B3178E4}.Debug|ARM.Build.0 = Debug|ARM - {D7B7A122-80A0-4AFB-9C06-18C29B3178E4}.Debug|ARM64.ActiveCfg = Debug|ARM64 - {D7B7A122-80A0-4AFB-9C06-18C29B3178E4}.Debug|ARM64.Build.0 = Debug|ARM64 - {D7B7A122-80A0-4AFB-9C06-18C29B3178E4}.Debug|Win32.ActiveCfg = Debug|Win32 - {D7B7A122-80A0-4AFB-9C06-18C29B3178E4}.Debug|Win32.Build.0 = Debug|Win32 - {D7B7A122-80A0-4AFB-9C06-18C29B3178E4}.Debug|x64.ActiveCfg = Debug|x64 - {D7B7A122-80A0-4AFB-9C06-18C29B3178E4}.Debug|x64.Build.0 = Debug|x64 - {D7B7A122-80A0-4AFB-9C06-18C29B3178E4}.PGInstrument|ARM.ActiveCfg = PGInstrument|ARM - {D7B7A122-80A0-4AFB-9C06-18C29B3178E4}.PGInstrument|ARM.Build.0 = PGInstrument|ARM - {D7B7A122-80A0-4AFB-9C06-18C29B3178E4}.PGInstrument|ARM64.ActiveCfg = PGInstrument|ARM64 - {D7B7A122-80A0-4AFB-9C06-18C29B3178E4}.PGInstrument|ARM64.Build.0 = PGInstrument|ARM64 - {D7B7A122-80A0-4AFB-9C06-18C29B3178E4}.PGInstrument|Win32.ActiveCfg = PGInstrument|Win32 - {D7B7A122-80A0-4AFB-9C06-18C29B3178E4}.PGInstrument|Win32.Build.0 = PGInstrument|Win32 - {D7B7A122-80A0-4AFB-9C06-18C29B3178E4}.PGInstrument|x64.ActiveCfg = PGInstrument|x64 - {D7B7A122-80A0-4AFB-9C06-18C29B3178E4}.PGInstrument|x64.Build.0 = PGInstrument|x64 - {D7B7A122-80A0-4AFB-9C06-18C29B3178E4}.PGUpdate|ARM.ActiveCfg = PGUpdate|ARM - {D7B7A122-80A0-4AFB-9C06-18C29B3178E4}.PGUpdate|ARM.Build.0 = PGUpdate|ARM - {D7B7A122-80A0-4AFB-9C06-18C29B3178E4}.PGUpdate|ARM64.ActiveCfg = PGUpdate|ARM64 - {D7B7A122-80A0-4AFB-9C06-18C29B3178E4}.PGUpdate|ARM64.Build.0 = PGUpdate|ARM64 - {D7B7A122-80A0-4AFB-9C06-18C29B3178E4}.PGUpdate|Win32.ActiveCfg = PGUpdate|Win32 - {D7B7A122-80A0-4AFB-9C06-18C29B3178E4}.PGUpdate|Win32.Build.0 = PGUpdate|Win32 - {D7B7A122-80A0-4AFB-9C06-18C29B3178E4}.PGUpdate|x64.ActiveCfg = PGUpdate|x64 - {D7B7A122-80A0-4AFB-9C06-18C29B3178E4}.PGUpdate|x64.Build.0 = PGUpdate|x64 - {D7B7A122-80A0-4AFB-9C06-18C29B3178E4}.Release|ARM.ActiveCfg = Release|ARM - {D7B7A122-80A0-4AFB-9C06-18C29B3178E4}.Release|ARM.Build.0 = Release|ARM - {D7B7A122-80A0-4AFB-9C06-18C29B3178E4}.Release|ARM64.ActiveCfg = Release|ARM64 - {D7B7A122-80A0-4AFB-9C06-18C29B3178E4}.Release|ARM64.Build.0 = Release|ARM64 - {D7B7A122-80A0-4AFB-9C06-18C29B3178E4}.Release|Win32.ActiveCfg = Release|Win32 - {D7B7A122-80A0-4AFB-9C06-18C29B3178E4}.Release|Win32.Build.0 = Release|Win32 - {D7B7A122-80A0-4AFB-9C06-18C29B3178E4}.Release|x64.ActiveCfg = Release|x64 - {D7B7A122-80A0-4AFB-9C06-18C29B3178E4}.Release|x64.Build.0 = Release|x64 {FCBE1EF2-E0F0-40B1-88B5-00A35D378742}.Debug|ARM.ActiveCfg = Debug|ARM {FCBE1EF2-E0F0-40B1-88B5-00A35D378742}.Debug|ARM.Build.0 = Debug|ARM {FCBE1EF2-E0F0-40B1-88B5-00A35D378742}.Debug|ARM64.ActiveCfg = Debug|ARM64 diff --git a/PCbuild/readme.txt b/PCbuild/readme.txt index 3eed265716d75b4..d232b4b61e87e26 100644 --- a/PCbuild/readme.txt +++ b/PCbuild/readme.txt @@ -168,7 +168,6 @@ The following sub-projects are for individual modules of the standard library which are implemented in C; each one builds a DLL (renamed to .pyd) of the same name as the project: * _asyncio - * _base64 * _ctypes * _ctypes_test * _decimal diff --git a/Python/stdlib_module_names.h b/Python/stdlib_module_names.h index 45e76b737c60084..565be27b7cebda5 100644 --- a/Python/stdlib_module_names.h +++ b/Python/stdlib_module_names.h @@ -10,7 +10,6 @@ static const char* _Py_stdlib_module_names[] = { "_ast", "_ast_unparse", "_asyncio", -"_base64", "_bisect", "_blake2", "_bz2", diff --git a/Tools/build/generate_stdlib_module_names.py b/Tools/build/generate_stdlib_module_names.py index fb091fd713fb2ef..2bda83733f22fcd 100644 --- a/Tools/build/generate_stdlib_module_names.py +++ b/Tools/build/generate_stdlib_module_names.py @@ -23,6 +23,7 @@ 'site-packages', # Rust build-system support modules + 'cpython-api', 'cpython-build-helper', 'cpython-rust-staticlib', diff --git a/configure b/configure index 7e5bb59c6e6bb0f..56a235a22428de3 100755 --- a/configure +++ b/configure @@ -691,8 +691,6 @@ MODULE_BINASCII_FALSE MODULE_BINASCII_TRUE MODULE_ZLIB_FALSE MODULE_ZLIB_TRUE -MODULE__BASE64_FALSE -MODULE__BASE64_TRUE MODULE__UUID_FALSE MODULE__UUID_TRUE MODULE__TKINTER_FALSE @@ -38262,47 +38260,6 @@ printf "%s\n" "$py_cv_module__uuid" >&6; } - { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for stdlib extension module _base64" >&5 -printf %s "checking for stdlib extension module _base64... " >&6; } - if test "$py_cv_module__base64" != "n/a" -then : - - if test "$HAVE_CARGO" = yes -then : - if test "$HAVE_CARGO" = yes -then : - py_cv_module__base64=yes -else case e in #( - e) py_cv_module__base64=missing ;; -esac -fi -else case e in #( - e) py_cv_module__base64=disabled ;; -esac -fi - -fi - as_fn_append MODULE_BLOCK "MODULE__BASE64_STATE=$py_cv_module__base64$as_nl" - if test "x$py_cv_module__base64" = xyes -then : - - - - -fi - if test "$py_cv_module__base64" = yes; then - MODULE__BASE64_TRUE= - MODULE__BASE64_FALSE='#' -else - MODULE__BASE64_TRUE='#' - MODULE__BASE64_FALSE= -fi - - { printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $py_cv_module__base64" >&5 -printf "%s\n" "$py_cv_module__base64" >&6; } - - - { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for stdlib extension module zlib" >&5 printf %s "checking for stdlib extension module zlib... " >&6; } if test "$py_cv_module_zlib" != "n/a" @@ -39606,10 +39563,6 @@ if test -z "${MODULE__UUID_TRUE}" && test -z "${MODULE__UUID_FALSE}"; then as_fn_error $? "conditional \"MODULE__UUID\" was never defined. Usually this means the macro was only invoked conditionally." "$LINENO" 5 fi -if test -z "${MODULE__BASE64_TRUE}" && test -z "${MODULE__BASE64_FALSE}"; then - as_fn_error $? "conditional \"MODULE__BASE64\" was never defined. -Usually this means the macro was only invoked conditionally." "$LINENO" 5 -fi if test -z "${MODULE_ZLIB_TRUE}" && test -z "${MODULE_ZLIB_FALSE}"; then as_fn_error $? "conditional \"MODULE_ZLIB\" was never defined. Usually this means the macro was only invoked conditionally." "$LINENO" 5 diff --git a/configure.ac b/configure.ac index db6ecc9ad92b04c..7fad11564cccc5b 100644 --- a/configure.ac +++ b/configure.ac @@ -9005,10 +9005,6 @@ PY_STDLIB_MOD([_uuid], [], [test "$have_uuid" = "yes"], [$LIBUUID_CFLAGS], [$LIBUUID_LIBS]) -PY_STDLIB_MOD([_base64], - [test "$HAVE_CARGO" = yes], [test "$HAVE_CARGO" = yes], - [], []) - dnl compression libs PY_STDLIB_MOD([zlib], [], [test "$have_zlib" = yes], [$ZLIB_CFLAGS], [$ZLIB_LIBS]) From ce37614c34538fbb785602be5205eaa69536ee86 Mon Sep 17 00:00:00 2001 From: Cary Hawkins Date: Tue, 24 Nov 2026 07:51:40 -0800 Subject: [PATCH 4/4] Update Tools/build/generate_stdlib_module_names.py Co-authored-by: Emma Smith --- Tools/build/generate_stdlib_module_names.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tools/build/generate_stdlib_module_names.py b/Tools/build/generate_stdlib_module_names.py index 2bda83733f22fcd..a46f7b611b9d75d 100644 --- a/Tools/build/generate_stdlib_module_names.py +++ b/Tools/build/generate_stdlib_module_names.py @@ -22,7 +22,7 @@ '__pycache__', 'site-packages', - # Rust build-system support modules + # Rust API and build-system support modules 'cpython-api', 'cpython-build-helper', 'cpython-rust-staticlib',