From 572f3ba50bb11f29046c45ad82fb51c0a15ce032 Mon Sep 17 00:00:00 2001 From: Ashwin Naren Date: Wed, 12 Feb 2025 20:48:46 -0800 Subject: [PATCH] initial _ctypes implementation with _CData, get_errno, and set_errno Signed-off-by: Ashwin Naren --- Cargo.lock | 5 +++-- vm/Cargo.toml | 1 + vm/src/stdlib/ctypes.rs | 34 ++++++++++++++++++++++++++++++++++ vm/src/stdlib/mod.rs | 6 ++++++ 4 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 vm/src/stdlib/ctypes.rs diff --git a/Cargo.lock b/Cargo.lock index 8f4c765e023..97236f95ec8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1265,9 +1265,9 @@ checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" [[package]] name = "memmap2" -version = "0.9.5" +version = "0.5.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd3f7eed9d3848f8b98834af67102b720745c4ec028fcd0aa0239277e7de374f" +checksum = "83faa42c0a078c393f6b29d5db232d8be22776a891f8f56e5284faee4a20b327" dependencies = [ "libc", ] @@ -2205,6 +2205,7 @@ dependencies = [ "cfg-if", "chrono", "crossbeam-utils", + "errno", "exitcode", "flame", "flamer", diff --git a/vm/Cargo.toml b/vm/Cargo.toml index 6eaca281f86..0c992605ffa 100644 --- a/vm/Cargo.toml +++ b/vm/Cargo.toml @@ -99,6 +99,7 @@ uname = "0.1.1" [target.'cfg(not(target_arch = "wasm32"))'.dependencies] rustyline = { workspace = true } which = "6" +errno = "0.3" [target.'cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))'.dependencies] num_cpus = "1.13.1" diff --git a/vm/src/stdlib/ctypes.rs b/vm/src/stdlib/ctypes.rs new file mode 100644 index 00000000000..e0976fa4a69 --- /dev/null +++ b/vm/src/stdlib/ctypes.rs @@ -0,0 +1,34 @@ +pub(crate) use _ctypes::make_module; + +#[pymodule] +mod _ctypes { + use crate::{common::lock::PyRwLock, PyObjectRef}; + use crossbeam_utils::atomic::AtomicCell; + + pub struct RawBuffer { + #[allow(dead_code)] + pub inner: Box<[u8]>, + #[allow(dead_code)] + pub size: usize, + } + + #[pyattr] + #[pyclass(name = "_CData")] + pub struct PyCData { + _objects: AtomicCell>, + _buffer: PyRwLock, + } + + #[pyclass] + impl PyCData {} + + #[pyfunction] + fn get_errno() -> i32 { + errno::errno().0 + } + + #[pyfunction] + fn set_errno(value: i32) { + errno::set_errno(errno::Errno(value)); + } +} diff --git a/vm/src/stdlib/mod.rs b/vm/src/stdlib/mod.rs index 12baee11f7a..ca8f8f0bfd8 100644 --- a/vm/src/stdlib/mod.rs +++ b/vm/src/stdlib/mod.rs @@ -37,6 +37,8 @@ pub mod posix; #[path = "posix_compat.rs"] pub mod posix; +#[cfg(any(target_family = "unix", target_family = "windows"))] +mod ctypes; #[cfg(windows)] pub(crate) mod msvcrt; #[cfg(all(unix, not(any(target_os = "android", target_os = "redox"))))] @@ -124,5 +126,9 @@ pub fn get_module_inits() -> StdlibMap { "_winapi" => winapi::make_module, "winreg" => winreg::make_module, } + #[cfg(any(target_family = "unix", target_family = "windows"))] + { + "_ctypes" => ctypes::make_module, + } } }