Skip to content
Merged
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
2 changes: 1 addition & 1 deletion crates/common/src/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub fn bytes_as_os_str(b: &[u8]) -> Result<&std::ffi::OsStr, Utf8Error> {

#[cfg(not(unix))]
pub fn bytes_as_os_str(b: &[u8]) -> Result<&std::ffi::OsStr, Utf8Error> {
Ok(std::str::from_utf8(b)?.as_ref())
Ok(core::str::from_utf8(b)?.as_ref())
}

#[cfg(unix)]
Expand Down
2 changes: 1 addition & 1 deletion crates/jit/tests/common.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use core::ops::ControlFlow;
use rustpython_compiler_core::bytecode::{
CodeObject, ConstantData, Instruction, OpArg, OpArgState,
};
use rustpython_jit::{CompiledCode, JitType};
use std::collections::HashMap;
use std::ops::ControlFlow;

#[derive(Debug, Clone)]
pub struct Function {
Expand Down
2 changes: 1 addition & 1 deletion crates/sre_engine/benches/benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl Pattern {
fn state_range<'a, S: StrDrive>(
&self,
string: S,
range: std::ops::Range<usize>,
range: core::ops::Range<usize>,
) -> (Request<'a, S>, State) {
let req = Request::new(string, range.start, range.end, self.code, false);
let state = State::default();
Expand Down
2 changes: 1 addition & 1 deletion crates/stdlib/src/scproxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ mod _scproxy {
.find(host_key)
.and_then(|v| v.downcast::<CFString>())
{
let h = std::borrow::Cow::<str>::from(&host);
let h = alloc::borrow::Cow::<str>::from(&host);
let v = if let Some(port) = proxy_dict
.find(port_key)
.and_then(|v| v.downcast::<CFNumber>())
Expand Down
32 changes: 15 additions & 17 deletions crates/stdlib/src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1228,7 +1228,7 @@ mod _socket {
.new_os_error("interface name too long".to_owned())
.into());
}
let cstr = std::ffi::CString::new(ifname)
let cstr = alloc::ffi::CString::new(ifname)
.map_err(|_| vm.new_os_error("invalid interface name".to_owned()))?;
let idx = unsafe { libc::if_nametoindex(cstr.as_ptr()) };
if idx == 0 {
Expand All @@ -1238,7 +1238,7 @@ mod _socket {
};

// Create sockaddr_can
let mut storage: libc::sockaddr_storage = unsafe { std::mem::zeroed() };
let mut storage: libc::sockaddr_storage = unsafe { core::mem::zeroed() };
let can_addr =
&mut storage as *mut libc::sockaddr_storage as *mut libc::sockaddr_can;
unsafe {
Expand Down Expand Up @@ -1294,7 +1294,7 @@ mod _socket {
}

// Create sockaddr_alg
let mut storage: libc::sockaddr_storage = unsafe { std::mem::zeroed() };
let mut storage: libc::sockaddr_storage = unsafe { core::mem::zeroed() };
let alg_addr =
&mut storage as *mut libc::sockaddr_storage as *mut libc::sockaddr_alg;
unsafe {
Expand Down Expand Up @@ -1936,7 +1936,7 @@ mod _socket {
flags: OptionalArg<i32>,
vm: &VirtualMachine,
) -> PyResult<PyTupleRef> {
use std::mem::MaybeUninit;
use core::mem::MaybeUninit;

if bufsize < 0 {
return Err(vm.new_value_error("negative buffer size in recvmsg".to_owned()));
Expand All @@ -1955,7 +1955,7 @@ mod _socket {
// Allocate buffers
let mut data_buf: Vec<MaybeUninit<u8>> = vec![MaybeUninit::uninit(); bufsize];
let mut anc_buf: Vec<MaybeUninit<u8>> = vec![MaybeUninit::uninit(); ancbufsize];
let mut addr_storage: libc::sockaddr_storage = unsafe { std::mem::zeroed() };
let mut addr_storage: libc::sockaddr_storage = unsafe { core::mem::zeroed() };

// Set up iovec
let mut iov = [libc::iovec {
Expand All @@ -1964,9 +1964,9 @@ mod _socket {
}];

// Set up msghdr
let mut msg: libc::msghdr = unsafe { std::mem::zeroed() };
let mut msg: libc::msghdr = unsafe { core::mem::zeroed() };
msg.msg_name = (&mut addr_storage as *mut libc::sockaddr_storage).cast();
msg.msg_namelen = std::mem::size_of::<libc::sockaddr_storage>() as libc::socklen_t;
msg.msg_namelen = core::mem::size_of::<libc::sockaddr_storage>() as libc::socklen_t;
msg.msg_iov = iov.as_mut_ptr();
msg.msg_iovlen = 1;
if ancbufsize > 0 {
Expand All @@ -1990,7 +1990,7 @@ mod _socket {
// Build data bytes
let data = unsafe {
data_buf.set_len(n);
std::mem::transmute::<Vec<MaybeUninit<u8>>, Vec<u8>>(data_buf)
core::mem::transmute::<Vec<MaybeUninit<u8>>, Vec<u8>>(data_buf)
};

// Build ancdata list
Expand All @@ -1999,7 +1999,7 @@ mod _socket {
// Build address tuple
let address = if msg.msg_namelen > 0 {
let storage: socket2::SockAddrStorage =
unsafe { std::mem::transmute(addr_storage) };
unsafe { core::mem::transmute(addr_storage) };
let addr = unsafe { socket2::SockAddr::new(storage, msg.msg_namelen) };
get_addr_tuple(&addr, vm)
} else {
Expand Down Expand Up @@ -2034,7 +2034,7 @@ mod _socket {
let available = ctrl_end as usize - data_ptr as usize;
let data_len = data_len_from_cmsg.min(available);

let data = unsafe { std::slice::from_raw_parts(data_ptr, data_len) };
let data = unsafe { core::slice::from_raw_parts(data_ptr, data_len) };

let tuple = vm.ctx.new_tuple(vec![
vm.ctx.new_int(cmsg_ref.cmsg_level).into(),
Expand Down Expand Up @@ -2820,13 +2820,11 @@ mod _socket {
let host = host_encoded.as_deref();

// Encode port using UTF-8
let port: Option<std::borrow::Cow<'_, str>> = match opts.port.as_ref() {
Some(Either::A(s)) => {
Some(std::borrow::Cow::Borrowed(s.to_str().ok_or_else(|| {
vm.new_unicode_encode_error("surrogates not allowed".to_owned())
})?))
}
Some(Either::B(i)) => Some(std::borrow::Cow::Owned(i.to_string())),
let port: Option<alloc::borrow::Cow<'_, str>> = match opts.port.as_ref() {
Some(Either::A(s)) => Some(alloc::borrow::Cow::Borrowed(s.to_str().ok_or_else(
|| vm.new_unicode_encode_error("surrogates not allowed".to_owned()),
)?)),
Some(Either::B(i)) => Some(alloc::borrow::Cow::Owned(i.to_string())),
None => None,
};
let port = port.as_ref().map(|p| p.as_ref());
Expand Down
6 changes: 4 additions & 2 deletions crates/stdlib/src/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3191,7 +3191,9 @@ mod _sqlite {
}

fn aggregate_context<T>(self) -> *mut T {
unsafe { sqlite3_aggregate_context(self.ctx, std::mem::size_of::<T>() as c_int).cast() }
unsafe {
sqlite3_aggregate_context(self.ctx, core::mem::size_of::<T>() as c_int).cast()
}
}

fn result_exception(self, vm: &VirtualMachine, exc: PyBaseExceptionRef, msg: &str) {
Expand Down Expand Up @@ -3297,7 +3299,7 @@ mod _sqlite {
} else if nbytes < 0 {
Err(vm.new_system_error("negative size with ptr"))
} else {
Ok(unsafe { std::slice::from_raw_parts(p.cast(), nbytes as usize) }.to_vec())
Ok(unsafe { core::slice::from_raw_parts(p.cast(), nbytes as usize) }.to_vec())
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/function/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ into_py_native_fn_tuple!(
#[cfg(test)]
mod tests {
use super::*;
use std::mem::size_of_val;
use core::mem::size_of_val;

#[test]
fn test_into_native_fn_noalloc() {
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/stdlib/ctypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1180,7 +1180,7 @@ pub(crate) mod _ctypes {
path: Option<PyObjectRef>,
vm: &VirtualMachine,
) -> PyResult<bool> {
use std::ffi::CString;
use alloc::ffi::CString;

let path = match path {
Some(p) if !vm.is_none(&p) => p,
Expand Down
4 changes: 2 additions & 2 deletions crates/vm/src/stdlib/ctypes/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use crate::{
protocol::{BufferDescriptor, PyBuffer, PyNumberMethods, PySequenceMethods},
types::{AsBuffer, AsNumber, AsSequence, Constructor, Initializer},
};
use alloc::borrow::Cow;
use num_traits::{Signed, ToPrimitive};
use std::borrow::Cow;

/// Get itemsize from a PEP 3118 format string
/// Extracts the type code (last char after endianness prefix) and returns its size
Expand Down Expand Up @@ -890,7 +890,7 @@ impl PyCArray {
// Python's from_buffer requires writable buffer, so this is safe.
let ptr = slice.as_ptr() as *mut u8;
let len = slice.len();
let owned_slice = unsafe { std::slice::from_raw_parts_mut(ptr, len) };
let owned_slice = unsafe { core::slice::from_raw_parts_mut(ptr, len) };
Self::write_element_to_buffer(
owned_slice,
final_offset,
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/stdlib/ctypes/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1304,7 +1304,7 @@ impl PyCSimple {
let ptr = slice.as_ptr() as *mut u8;
let len = slice.len().min(buffer_bytes.len());
unsafe {
std::ptr::copy_nonoverlapping(buffer_bytes.as_ptr(), ptr, len);
core::ptr::copy_nonoverlapping(buffer_bytes.as_ptr(), ptr, len);
}
}
Cow::Owned(vec) => {
Expand Down
10 changes: 5 additions & 5 deletions crates/vm/src/stdlib/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1747,16 +1747,16 @@ pub(super) mod _os {
// We extract raw bytes and interpret as a native-endian integer.
// Note: The value may differ across architectures due to endianness.
let f_fsid = {
let ptr = std::ptr::addr_of!(st.f_fsid) as *const u8;
let size = std::mem::size_of_val(&st.f_fsid);
let ptr = core::ptr::addr_of!(st.f_fsid) as *const u8;
let size = core::mem::size_of_val(&st.f_fsid);
if size >= 8 {
let bytes = unsafe { std::slice::from_raw_parts(ptr, 8) };
let bytes = unsafe { core::slice::from_raw_parts(ptr, 8) };
u64::from_ne_bytes([
bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6],
bytes[7],
]) as libc::c_ulong
} else if size >= 4 {
let bytes = unsafe { std::slice::from_raw_parts(ptr, 4) };
let bytes = unsafe { core::slice::from_raw_parts(ptr, 4) };
u32::from_ne_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]) as libc::c_ulong
} else {
0
Expand Down Expand Up @@ -1784,7 +1784,7 @@ pub(super) mod _os {
#[pyfunction]
#[pyfunction(name = "fstatvfs")]
fn statvfs(path: OsPathOrFd<'_>, vm: &VirtualMachine) -> PyResult {
let mut st: libc::statvfs = unsafe { std::mem::zeroed() };
let mut st: libc::statvfs = unsafe { core::mem::zeroed() };
let ret = match &path {
OsPathOrFd::Path(p) => {
let cpath = p.clone().into_cstring(vm)?;
Expand Down
4 changes: 2 additions & 2 deletions crates/vm/src/stdlib/posix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,9 @@ pub mod module {

#[cfg(any(target_os = "macos", target_os = "ios"))]
fn getgroups_impl() -> nix::Result<Vec<Gid>> {
use core::ptr;
use libc::{c_int, gid_t};
use nix::errno::Errno;
use std::ptr;
let ret = unsafe { libc::getgroups(0, ptr::null_mut()) };
let mut groups = Vec::<Gid>::with_capacity(Errno::result(ret)? as usize);
let ret = unsafe {
Expand Down Expand Up @@ -1825,7 +1825,7 @@ pub mod module {
#[cfg(target_os = "macos")]
#[pyfunction]
fn _fcopyfile(in_fd: i32, out_fd: i32, flags: i32, vm: &VirtualMachine) -> PyResult<()> {
let ret = unsafe { fcopyfile(in_fd, out_fd, std::ptr::null_mut(), flags as u32) };
let ret = unsafe { fcopyfile(in_fd, out_fd, core::ptr::null_mut(), flags as u32) };
if ret < 0 {
Err(vm.new_last_errno_error())
} else {
Expand Down
14 changes: 7 additions & 7 deletions crates/vm/src/stdlib/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ pub(crate) mod _thread {
#[derive(PyPayload)]
struct RLock {
mu: RawRMutex,
count: std::sync::atomic::AtomicUsize,
count: core::sync::atomic::AtomicUsize,
}

impl fmt::Debug for RLock {
Expand All @@ -207,7 +207,7 @@ pub(crate) mod _thread {
fn slot_new(cls: PyTypeRef, _args: FuncArgs, vm: &VirtualMachine) -> PyResult {
Self {
mu: RawRMutex::INIT,
count: std::sync::atomic::AtomicUsize::new(0),
count: core::sync::atomic::AtomicUsize::new(0),
}
.into_ref_with_type(vm, cls)
.map(Into::into)
Expand All @@ -220,7 +220,7 @@ pub(crate) mod _thread {
let result = acquire_lock_impl!(&self.mu, args, vm)?;
if result {
self.count
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
.fetch_add(1, core::sync::atomic::Ordering::Relaxed);
}
Ok(result)
}
Expand All @@ -231,11 +231,11 @@ pub(crate) mod _thread {
return Err(vm.new_runtime_error("release unlocked lock"));
}
debug_assert!(
self.count.load(std::sync::atomic::Ordering::Relaxed) > 0,
self.count.load(core::sync::atomic::Ordering::Relaxed) > 0,
"RLock count underflow"
);
self.count
.fetch_sub(1, std::sync::atomic::Ordering::Relaxed);
.fetch_sub(1, core::sync::atomic::Ordering::Relaxed);
unsafe { self.mu.unlock() };
Ok(())
}
Expand All @@ -247,7 +247,7 @@ pub(crate) mod _thread {
self.mu.unlock();
};
}
self.count.store(0, std::sync::atomic::Ordering::Relaxed);
self.count.store(0, core::sync::atomic::Ordering::Relaxed);
let new_mut = RawRMutex::INIT;

let old_mutex: AtomicCell<&RawRMutex> = AtomicCell::new(&self.mu);
Expand All @@ -264,7 +264,7 @@ pub(crate) mod _thread {
#[pymethod]
fn _recursion_count(&self) -> usize {
if self.mu.is_owned_by_current_thread() {
self.count.load(std::sync::atomic::Ordering::Relaxed)
self.count.load(core::sync::atomic::Ordering::Relaxed)
} else {
0
}
Expand Down
3 changes: 2 additions & 1 deletion crates/wasm/src/browser_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ mod _browser {

#[pyfunction]
fn request_animation_frame(func: ArgCallable, vm: &VirtualMachine) -> PyResult {
use std::{cell::RefCell, rc::Rc};
use alloc::rc::Rc;
use core::cell::RefCell;

// this basic setup for request_animation_frame taken from:
// https://rustwasm.github.io/wasm-bindgen/examples/request-animation-frame.html
Expand Down
2 changes: 1 addition & 1 deletion crates/wasm/src/js_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod _js {
vm_class::{WASMVirtualMachine, stored_vm_from_wasm},
weak_vm,
};
use core::{cell, fmt, future};
use js_sys::{Array, Object, Promise, Reflect};
use rustpython_vm::{
Py, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject, VirtualMachine,
Expand All @@ -17,7 +18,6 @@ mod _js {
protocol::PyIterReturn,
types::{IterNext, Representable, SelfIter},
};
use std::{cell, fmt, future};
use wasm_bindgen::{JsCast, closure::Closure, prelude::*};
use wasm_bindgen_futures::{JsFuture, future_to_promise};

Expand Down
2 changes: 2 additions & 0 deletions crates/wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
extern crate alloc;

pub mod browser_module;
pub mod convert;
pub mod js_module;
Expand Down
8 changes: 3 additions & 5 deletions crates/wasm/src/vm_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@ use crate::{
convert::{self, PyResultExt},
js_module, wasm_builtins,
};
use alloc::rc::{Rc, Weak};
use core::cell::RefCell;
use js_sys::{Object, TypeError};
use rustpython_vm::{
Interpreter, PyObjectRef, PyPayload, PyRef, PyResult, Settings, VirtualMachine,
builtins::{PyModule, PyWeak},
compiler::Mode,
scope::Scope,
};
use std::{
cell::RefCell,
collections::HashMap,
rc::{Rc, Weak},
};
use std::collections::HashMap;
use wasm_bindgen::prelude::*;

pub(crate) struct StoredVirtualMachine {
Expand Down
Loading