Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ rustpython-compiler-core = { path = "crates/compiler-core", version = "0.5.0" }
rustpython-compiler = { path = "crates/compiler", version = "0.5.0" }
rustpython-codegen = { path = "crates/codegen", version = "0.5.0" }
rustpython-common = { path = "crates/common", version = "0.5.0" }
rustpython-host-env = { path = "crates/host_env", version = "0.5.0" }
rustpython-derive = { path = "crates/derive", version = "0.5.0" }
rustpython-derive-impl = { path = "crates/derive-impl", version = "0.5.0" }
rustpython-jit = { path = "crates/jit", version = "0.5.0" }
Expand Down
14 changes: 0 additions & 14 deletions crates/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,5 @@ lock_api = "0.4"
siphasher = "1"
num-complex.workspace = true

[target.'cfg(unix)'.dependencies]
nix = { workspace = true }

[target.'cfg(windows)'.dependencies]
widestring = { workspace = true }
windows-sys = { workspace = true, features = [
"Win32_Foundation",
"Win32_Networking_WinSock",
"Win32_Storage_FileSystem",
"Win32_System_Ioctl",
"Win32_System_LibraryLoader",
"Win32_System_SystemServices",
] }

[lints]
workspace = true
32 changes: 32 additions & 0 deletions crates/common/clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
disallowed-methods = [
{ path = "std::fs::read", reason = "use rustpython_host_env for host filesystem access" },
{ path = "std::fs::write", reason = "use rustpython_host_env for host filesystem access" },
{ path = "std::fs::read_to_string", reason = "use rustpython_host_env for host filesystem access" },
{ path = "std::fs::read_dir", reason = "use rustpython_host_env for host filesystem access" },
{ path = "std::fs::create_dir", reason = "use rustpython_host_env for host filesystem access" },
{ path = "std::fs::create_dir_all", reason = "use rustpython_host_env for host filesystem access" },
{ path = "std::fs::remove_file", reason = "use rustpython_host_env for host filesystem access" },
{ path = "std::fs::remove_dir", reason = "use rustpython_host_env for host filesystem access" },
{ path = "std::fs::metadata", reason = "use rustpython_host_env for host filesystem access" },
{ path = "std::fs::symlink_metadata", reason = "use rustpython_host_env for host filesystem access" },
{ path = "std::fs::canonicalize", reason = "use rustpython_host_env for host filesystem access" },
{ path = "std::fs::File::open", reason = "use rustpython_host_env for host filesystem access" },
{ path = "std::fs::File::create", reason = "use rustpython_host_env for host filesystem access" },
{ path = "std::fs::OpenOptions::open", reason = "use rustpython_host_env for host filesystem access" },
{ path = "std::env::var", reason = "use rustpython_host_env for host environment access" },
{ path = "std::env::var_os", reason = "use rustpython_host_env for host environment access" },
{ path = "std::env::set_var", reason = "use rustpython_host_env for host environment access" },
{ path = "std::env::remove_var", reason = "use rustpython_host_env for host environment access" },
{ path = "std::env::vars", reason = "use rustpython_host_env for host environment access" },
{ path = "std::env::vars_os", reason = "use rustpython_host_env for host environment access" },
{ path = "std::env::current_dir", reason = "use rustpython_host_env for host environment access" },
{ path = "std::env::set_current_dir", reason = "use rustpython_host_env for host environment access" },
{ path = "std::env::temp_dir", reason = "use rustpython_host_env for host environment access" },
{ path = "std::process::Command::new", reason = "use rustpython_host_env for host process access" },
{ path = "std::process::exit", reason = "use rustpython_host_env for host process access" },
{ path = "std::process::abort", reason = "use rustpython_host_env for host process access" },
{ path = "std::process::id", reason = "use rustpython_host_env for host process access" },
{ path = "std::net::TcpStream::connect", reason = "use rustpython_host_env for host network access" },
{ path = "std::net::TcpListener::bind", reason = "use rustpython_host_env for host network access" },
{ path = "std::net::UdpSocket::bind", reason = "use rustpython_host_env for host network access" },
]
13 changes: 1 addition & 12 deletions crates/common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,26 @@
//! A crate to hold types and functions common to all rustpython components.

#![cfg_attr(not(feature = "std"), no_std)]
#![deny(clippy::disallowed_methods)]

extern crate alloc;

#[macro_use]
mod macros;
pub use macros::*;

pub mod atomic;
pub mod borrow;
pub mod boxvec;
pub mod cformat;
#[cfg(all(feature = "std", any(unix, windows, target_os = "wasi")))]
pub mod crt_fd;
pub mod encodings;
#[cfg(all(feature = "std", any(not(target_arch = "wasm32"), target_os = "wasi")))]
pub mod fileutils;
pub mod float_ops;
pub mod format;
pub mod hash;
pub mod int;
pub mod linked_list;
pub mod lock;
#[cfg(feature = "std")]
pub mod os;
pub mod rand;
pub mod rc;
pub mod refcount;
pub mod static_cell;
pub mod str;
#[cfg(all(feature = "std", windows))]
pub mod windows;

pub use rustpython_wtf8 as wtf8;

Expand Down
9 changes: 7 additions & 2 deletions crates/common/src/refcount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ const WEAK_COUNT: usize = 1 << STRONG_WIDTH;
#[inline(never)]
#[cold]
fn refcount_overflow() -> ! {
#[cfg(feature = "std")]
std::process::abort();
#[cfg(all(feature = "std", not(target_arch = "wasm32")))]
// SAFETY: abort terminates the process immediately and does not return.
unsafe {
libc::abort()
};
#[cfg(all(feature = "std", target_arch = "wasm32"))]
core::panic!("refcount overflow");
#[cfg(not(feature = "std"))]
core::panic!("refcount overflow");
}
Expand Down
41 changes: 41 additions & 0 deletions crates/host_env/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
[package]
name = "rustpython-host-env"
description = "Host OS API abstractions for RustPython"
version.workspace = true
authors.workspace = true
edition.workspace = true
rust-version.workspace = true
repository.workspace = true
license.workspace = true

[dependencies]
rustpython-wtf8 = { workspace = true }

libc = { workspace = true }
num-traits = { workspace = true }

[target.'cfg(unix)'.dependencies]
nix = { workspace = true }

[target.'cfg(all(unix, not(target_os = "ios"), not(target_os = "redox")))'.dependencies]
termios = "0.3.3"

[target.'cfg(windows)'.dependencies]
widestring = { workspace = true }
windows-sys = { workspace = true, features = [
"Win32_Foundation",
"Win32_Globalization",
"Win32_Networking_WinSock",
"Win32_Storage_FileSystem",
"Win32_System_Console",
"Win32_System_Diagnostics_Debug",
"Win32_System_Ioctl",
"Win32_System_LibraryLoader",
"Win32_System_SystemInformation",
"Win32_System_SystemServices",
"Win32_System_Threading",
"Win32_System_Time",
] }

[lints]
workspace = true
1 change: 1 addition & 0 deletions crates/host_env/clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
disallowed-methods = []
File renamed without changes.
75 changes: 75 additions & 0 deletions crates/host_env/src/fcntl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use std::io;

pub fn fcntl_int(fd: i32, cmd: i32, arg: i32) -> io::Result<i32> {
let ret = unsafe { libc::fcntl(fd, cmd, arg) };
if ret < 0 {
Err(io::Error::last_os_error())
} else {
Ok(ret)
}
}

pub fn fcntl_with_bytes(fd: i32, cmd: i32, arg: &mut [u8]) -> io::Result<i32> {
let ret = unsafe { libc::fcntl(fd, cmd, arg.as_mut_ptr()) };
if ret < 0 {
Err(io::Error::last_os_error())
} else {
Ok(ret)
}
}

/// # Safety
///
/// `arg` must be a valid pointer for the `request` passed to `ioctl` and must
/// satisfy the platform ABI requirements for that request.
pub unsafe fn ioctl_ptr(
fd: i32,
request: libc::c_ulong,
arg: *mut libc::c_void,
) -> io::Result<i32> {
let ret = unsafe { libc::ioctl(fd, request as _, arg) };
if ret < 0 {
Err(io::Error::last_os_error())
} else {
Ok(ret)
}
}

pub fn ioctl_int(fd: i32, request: libc::c_ulong, arg: i32) -> io::Result<i32> {
let ret = unsafe { libc::ioctl(fd, request as _, arg) };
if ret < 0 {
Err(io::Error::last_os_error())
} else {
Ok(ret)
}
}

#[cfg(not(any(target_os = "wasi", target_os = "redox")))]
pub fn flock(fd: i32, operation: i32) -> io::Result<i32> {
let ret = unsafe { libc::flock(fd, operation) };
if ret < 0 {
Err(io::Error::last_os_error())
} else {
Ok(ret)
}
}

#[cfg(not(any(target_os = "wasi", target_os = "redox")))]
pub fn lockf(fd: i32, cmd: i32, lock: &libc::flock) -> io::Result<i32> {
let ret = unsafe {
libc::fcntl(
fd,
if (cmd & libc::LOCK_NB) != 0 {
libc::F_SETLK
} else {
libc::F_SETLKW
},
lock,
)
};
if ret < 0 {
Err(io::Error::last_os_error())
} else {
Ok(ret)
}
}
File renamed without changes.
45 changes: 45 additions & 0 deletions crates/host_env/src/fs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use std::{
fs::{self, File, Metadata, ReadDir},
io,
path::Path,
};

pub fn open(path: impl AsRef<Path>) -> io::Result<File> {
File::open(path)
}

pub fn read(path: impl AsRef<Path>) -> io::Result<Vec<u8>> {
fs::read(path)
}

pub fn read_to_string(path: impl AsRef<Path>) -> io::Result<String> {
fs::read_to_string(path)
}

pub fn read_dir(path: impl AsRef<Path>) -> io::Result<ReadDir> {
fs::read_dir(path)
}

pub fn create_dir_all(path: impl AsRef<Path>) -> io::Result<()> {
fs::create_dir_all(path)
}

pub fn remove_dir(path: impl AsRef<Path>) -> io::Result<()> {
fs::remove_dir(path)
}

pub fn remove_file(path: impl AsRef<Path>) -> io::Result<()> {
fs::remove_file(path)
}

pub fn metadata(path: impl AsRef<Path>) -> io::Result<Metadata> {
fs::metadata(path)
}

pub fn symlink_metadata(path: impl AsRef<Path>) -> io::Result<Metadata> {
fs::symlink_metadata(path)
}

pub fn open_write(path: impl AsRef<Path>) -> io::Result<File> {
fs::OpenOptions::new().write(true).open(path)
}
42 changes: 42 additions & 0 deletions crates/host_env/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
extern crate alloc;

#[macro_use]
mod macros;
pub use macros::*;

pub mod os;

#[cfg(any(unix, windows, target_os = "wasi"))]
pub mod crt_fd;

#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))]
pub mod fileutils;
#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))]
pub mod fs;

#[cfg(windows)]
pub mod windows;

#[cfg(any(unix, target_os = "wasi"))]
pub mod fcntl;
#[cfg(any(unix, windows, target_os = "wasi"))]
pub mod select;
#[cfg(unix)]
pub mod syslog;
#[cfg(all(unix, not(target_os = "redox"), not(target_os = "ios")))]
pub mod termios;

#[cfg(unix)]
pub mod posix;
#[cfg(all(unix, not(target_os = "redox"), not(target_os = "android")))]
pub mod shm;
#[cfg(unix)]
pub mod signal;
pub mod time;

#[cfg(windows)]
pub mod msvcrt;
#[cfg(windows)]
pub mod nt;
#[cfg(windows)]
pub mod winapi;
File renamed without changes.
Loading
Loading