Skip to content

Commit dd5e460

Browse files
committed
initial _ctypes implementation with _CData, get_errno, and set_errno
Signed-off-by: Ashwin Naren <arihant2math@gmail.com>
1 parent 6e35e20 commit dd5e460

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

Cargo.lock

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vm/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ uname = "0.1.1"
9999
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
100100
rustyline = { workspace = true }
101101
which = "6"
102+
errno = "0.3"
102103

103104
[target.'cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))'.dependencies]
104105
num_cpus = "1.13.1"

vm/src/stdlib/ctypes.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
pub(crate) use _ctypes::make_module;
2+
3+
#[pymodule]
4+
mod _ctypes {
5+
use crate::{common::lock::PyRwLock, PyObjectRef};
6+
use crossbeam_utils::atomic::AtomicCell;
7+
8+
pub struct RawBuffer {
9+
pub inner: Box<[u8]>,
10+
pub size: usize,
11+
}
12+
13+
#[pyattr]
14+
#[pyclass(name = "_CData")]
15+
pub struct PyCData {
16+
_objects: AtomicCell<Vec<PyObjectRef>>,
17+
_buffer: PyRwLock<RawBuffer>,
18+
}
19+
20+
#[pyclass]
21+
impl PyCData {}
22+
23+
#[pyfunction]
24+
fn get_errno() -> i32 {
25+
errno::errno().0
26+
}
27+
28+
#[pyfunction]
29+
fn set_errno(value: i32) {
30+
errno::set_errno(errno::Errno(value));
31+
}
32+
}

vm/src/stdlib/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub mod typing;
2525
pub mod warnings;
2626
mod weakref;
2727

28-
#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))]
28+
#[cfg(not(target_arch = "wasm32"))]
2929
#[macro_use]
3030
pub mod os;
3131
#[cfg(windows)]
@@ -37,6 +37,8 @@ pub mod posix;
3737
#[path = "posix_compat.rs"]
3838
pub mod posix;
3939

40+
#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))]
41+
mod ctypes;
4042
#[cfg(windows)]
4143
pub(crate) mod msvcrt;
4244
#[cfg(all(unix, not(any(target_os = "android", target_os = "redox"))))]
@@ -124,5 +126,9 @@ pub fn get_module_inits() -> StdlibMap {
124126
"_winapi" => winapi::make_module,
125127
"winreg" => winreg::make_module,
126128
}
129+
#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))]
130+
{
131+
"_ctypes" => ctypes::make_module,
132+
}
127133
}
128134
}

0 commit comments

Comments
 (0)