Skip to content

rustpython-vm public API for rustpython-stdlib #3104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
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 derive/src/pystructseq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub(crate) fn impl_pystruct_sequence(
self.#field_names,
vm,
)),*];
::rustpython_vm::builtins::tuple::PyTuple::_new(items.into_boxed_slice())
::rustpython_vm::builtins::tuple::PyTuple::new_unchecked(items.into_boxed_slice())
}
}
impl ::rustpython_vm::IntoPyObject for #ty {
Expand Down
2 changes: 1 addition & 1 deletion vm/src/builtins/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
protocol::{BufferInternal, BufferOptions, PyBuffer},
sliceable::{convert_slice, wrap_index, SequenceIndex},
slots::{AsBuffer, Comparable, Hashable, PyComparisonOp, SlotConstructor},
stdlib::pystruct::_struct::FormatSpec,
stdlib::pystruct::FormatSpec,
utils::Either,
IdProtocol, IntoPyObject, PyClassImpl, PyComparisonValue, PyContext, PyObjectRef, PyRef,
PyResult, PyValue, TryFromBorrowedObject, TryFromObject, TypeProtocol, VirtualMachine,
Expand Down
5 changes: 1 addition & 4 deletions vm/src/builtins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub use pytype::{PyType, PyTypeRef};
pub(crate) mod range;
pub use range::PyRange;
pub(crate) mod set;
pub use set::PySet;
pub use set::{PyFrozenSet, PySet};
pub(crate) mod singletons;
pub use singletons::{PyNone, PyNotImplemented};
pub(crate) mod slice;
Expand All @@ -84,7 +84,4 @@ pub use zip::PyZip;
pub use float::try_to_bigint as try_f64_to_bigint;
pub use int::try_to_float as try_bigint_to_f64;

mod make_module;
pub use make_module::{ascii, make_module, print};

pub use crate::exceptions::types::*;
2 changes: 1 addition & 1 deletion vm/src/builtins/pystr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ impl PyStr {
}

#[pymethod(magic)]
pub(crate) fn repr(&self, vm: &VirtualMachine) -> PyResult<String> {
pub fn repr(&self, vm: &VirtualMachine) -> PyResult<String> {
let in_len = self.byte_len();
let mut out_len = 0usize;
// let mut max = 127;
Expand Down
2 changes: 1 addition & 1 deletion vm/src/builtins/pytype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ impl PyType {
#[pyproperty(name = "__mro__")]
fn get_mro(zelf: PyRef<Self>) -> PyTuple {
let elements: Vec<PyObjectRef> = zelf.iter_mro().map(|x| x.as_object().clone()).collect();
PyTuple::_new(elements.into_boxed_slice())
PyTuple::new_unchecked(elements.into_boxed_slice())
}

#[pyproperty(magic)]
Expand Down
2 changes: 1 addition & 1 deletion vm/src/builtins/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl PyTuple {
/// Creating a new tuple with given boxed slice.
/// NOTE: for usual case, you probably want to use PyTupleRef::with_elements.
/// Calling this function implies trying micro optimization for non-zero-sized tuple.
pub(crate) fn _new(elements: Box<[PyObjectRef]>) -> Self {
pub fn new_unchecked(elements: Box<[PyObjectRef]>) -> Self {
Self { elements }
}

Expand Down
3 changes: 2 additions & 1 deletion vm/src/format.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::{
builtins::{self, PyBaseExceptionRef, PyStrRef},
builtins::{PyBaseExceptionRef, PyStrRef},
common::float_ops,
exceptions::IntoPyException,
function::FuncArgs,
stdlib::builtins,
ItemProtocol, PyObjectRef, PyResult, TypeProtocol, VirtualMachine,
};
use itertools::{Itertools, PeekingNext};
Expand Down
5 changes: 2 additions & 3 deletions vm/src/frame.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
use crate::common::{boxvec::BoxVec, lock::PyMutex};
use crate::{
builtins::PyBaseExceptionRef,
builtins::{
self,
asyncgenerator::PyAsyncGenWrappedValue,
coroutine::PyCoroutine,
function::{PyCell, PyCellRef, PyFunction},
generator::PyGenerator,
list, pystr, set,
traceback::PyTraceback,
tuple::{PyTuple, PyTupleTyped},
PyCode, PyDict, PyDictRef, PySlice, PyStr, PyStrRef, PyTypeRef,
PyBaseExceptionRef, PyCode, PyDict, PyDictRef, PySlice, PyStr, PyStrRef, PyTypeRef,
},
bytecode,
coroutine::Coro,
Expand All @@ -20,6 +18,7 @@ use crate::{
protocol::PyIter,
scope::Scope,
slots::PyComparisonOp,
stdlib::builtins,
IdProtocol, ItemProtocol, PyMethod, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject,
TypeProtocol, VirtualMachine,
};
Expand Down
6 changes: 3 additions & 3 deletions vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ mod frozen;
pub mod function;
pub mod import;
pub mod iterator;
mod protocol;
pub mod protocol;
pub mod py_io;
pub mod py_serde;
mod pyobject;
Expand All @@ -69,12 +69,12 @@ pub mod readline;
pub mod scope;
mod sequence;
mod signal;
mod sliceable;
pub mod sliceable;
pub mod slots;
pub mod stdlib;
pub mod types;
pub mod utils;
mod version;
pub mod version;
mod vm;

// pub use self::Executor;
Expand Down
2 changes: 1 addition & 1 deletion vm/src/pyobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl PyContext {
let false_value = create_object(PyInt::from(0), &types.bool_type);

let empty_tuple = create_object(
PyTuple::_new(Vec::new().into_boxed_slice()),
PyTuple::new_unchecked(Vec::new().into_boxed_slice()),
&types.tuple_type,
);
let empty_frozenset =
Expand Down
2 changes: 1 addition & 1 deletion vm/src/sliceable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ pub(crate) fn wrap_index(p: isize, len: usize) -> Option<usize> {
}

// return pos is in range [0, len] inclusive
pub(crate) fn saturate_index(p: isize, len: usize) -> usize {
pub fn saturate_index(p: isize, len: usize) -> usize {
let mut p = p;
let len = len.to_isize().unwrap();
if p < 0 {
Expand Down
8 changes: 4 additions & 4 deletions vm/src/builtins/make_module.rs → vm/src/stdlib/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use crate::{PyObjectRef, VirtualMachine};
/// Built-in functions, exceptions, and other objects.
///
/// Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.
#[pymodule(name = "builtins")]
mod decl {
#[pymodule]
mod builtins {
use crate::builtins::{
enumerate::PyReverseSequenceIterator,
function::{PyCellRef, PyFunctionRef},
Expand Down Expand Up @@ -891,12 +891,12 @@ mod decl {
}
}

pub use decl::{ascii, print};
pub use builtins::{ascii, print};

pub fn make_module(vm: &VirtualMachine, module: PyObjectRef) {
let ctx = &vm.ctx;

decl::extend_module(vm, &module);
builtins::extend_module(vm, &module);

let debug_mode: bool = vm.state.settings.optimize == 0;
extend_module!(vm, module, {
Expand Down
4 changes: 2 additions & 2 deletions vm/src/stdlib/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub(crate) use fcntl::make_module;
#[pymodule]
mod fcntl {
use crate::{
builtins::int,
builtins::PyIntRef,
function::{ArgMemoryBuffer, ArgStrOrBytesLike, OptionalArg},
stdlib::{io, os},
utils::Either,
Expand All @@ -25,7 +25,7 @@ mod fcntl {
fn fcntl(
io::Fildes(fd): io::Fildes,
cmd: i32,
arg: OptionalArg<Either<ArgStrOrBytesLike, int::PyIntRef>>,
arg: OptionalArg<Either<ArgStrOrBytesLike, PyIntRef>>,
vm: &VirtualMachine,
) -> PyResult {
let int = match arg {
Expand Down
2 changes: 1 addition & 1 deletion vm/src/stdlib/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub(crate) fn make_module(vm: &VirtualMachine) -> PyObjectRef {
#[allow(unused)]
#[derive(Copy, Clone)]
#[repr(transparent)]
pub(crate) struct Fildes(pub i32);
pub struct Fildes(pub i32);

impl TryFromObject for Fildes {
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
Expand Down
1 change: 1 addition & 0 deletions vm/src/stdlib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub(crate) mod ast;
mod atexit;
mod binascii;
mod bisect;
pub mod builtins;
mod cmath;
mod codecs;
mod collections;
Expand Down
8 changes: 8 additions & 0 deletions vm/src/stdlib/nt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,3 +427,11 @@ macro_rules! suppress_iph {
ret
}};
}

pub fn init_winsock() {
static WSA_INIT: parking_lot::Once = parking_lot::Once::new();
WSA_INIT.call_once(|| unsafe {
let mut wsa_data = std::mem::MaybeUninit::uninit();
let _ = winapi::um::winsock2::WSAStartup(0x0101, wsa_data.as_mut_ptr());
})
}
2 changes: 1 addition & 1 deletion vm/src/stdlib/posix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{PyObjectRef, PyResult, VirtualMachine};
use nix;
use std::os::unix::io::RawFd;

pub(crate) fn raw_set_inheritable(fd: RawFd, inheritable: bool) -> nix::Result<()> {
pub fn raw_set_inheritable(fd: RawFd, inheritable: bool) -> nix::Result<()> {
use nix::fcntl;
let flags = fcntl::FdFlag::from_bits_truncate(fcntl::fcntl(fd, fcntl::FcntlArg::F_GETFD)?);
let mut new_flags = flags;
Expand Down
2 changes: 1 addition & 1 deletion vm/src/stdlib/pystruct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -966,4 +966,4 @@ pub(crate) mod _struct {
}
}

pub(crate) use _struct::make_module;
pub(crate) use _struct::{make_module, FormatSpec};
8 changes: 5 additions & 3 deletions vm/src/stdlib/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use crate::{PyObjectRef, PyResult, TryFromBorrowedObject, TryFromObject, Virtual
use std::{io, mem};

pub(crate) fn make_module(vm: &VirtualMachine) -> PyObjectRef {
super::socket::init_winsock();
#[cfg(windows)]
super::nt::init_winsock();

#[cfg(unix)]
{
use crate::PyClassImpl;
Expand Down Expand Up @@ -171,7 +173,7 @@ mod decl {
return Err(vm.new_value_error("timeout must be positive".to_owned()));
}
}
let deadline = timeout.map(|s| time::get_time(vm).unwrap() + s);
let deadline = timeout.map(|s| time::time(vm).unwrap() + s);

let seq2set = |list| -> PyResult<(Vec<Selectable>, FdSet)> {
let v = vm.extract_elements::<Selectable>(list)?;
Expand Down Expand Up @@ -210,7 +212,7 @@ mod decl {
vm.check_signals()?;

if let Some(ref mut timeout) = timeout {
*timeout = deadline.unwrap() - time::get_time(vm).unwrap();
*timeout = deadline.unwrap() - time::time(vm).unwrap();
if *timeout < 0.0 {
r.clear();
w.clear();
Expand Down
2 changes: 1 addition & 1 deletion vm/src/stdlib/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ pub(crate) mod _signal {

#[cfg(windows)]
let is_socket = if fd != INVALID_WAKEUP {
crate::stdlib::socket::init_winsock();
crate::stdlib::nt::init_winsock();
let mut res = 0i32;
let mut res_size = std::mem::size_of::<i32>() as i32;
let res = unsafe {
Expand Down
14 changes: 2 additions & 12 deletions vm/src/stdlib/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1818,7 +1818,8 @@ rustpython_common::static_cell! {
}

pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
init_winsock();
#[cfg(windows)]
super::nt::init_winsock();

let ctx = &vm.ctx;
let socket_timeout = TIMEOUT_ERROR
Expand Down Expand Up @@ -1978,14 +1979,3 @@ fn extend_module_platform_specific(vm: &VirtualMachine, module: &PyObjectRef) {
"sethostname" => named_function!(ctx, _socket, sethostname),
});
}

pub fn init_winsock() {
#[cfg(windows)]
{
static WSA_INIT: parking_lot::Once = parking_lot::Once::new();
WSA_INIT.call_once(|| unsafe {
let mut wsa_data = std::mem::MaybeUninit::uninit();
let _ = winapi::um::winsock2::WSAStartup(0x0101, wsa_data.as_mut_ptr());
})
}
}
18 changes: 10 additions & 8 deletions vm/src/stdlib/sys.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
use num_traits::ToPrimitive;
use std::{env, mem, path};

use crate::builtins::{PyStr, PyStrRef, PyTypeRef};
use crate::common::{
ascii,
hash::{PyHash, PyUHash},
};
use crate::frame::FrameRef;
use crate::function::{FuncArgs, OptionalArg, PosArgs};
use crate::vm::{PySettings, VirtualMachine};
use crate::{builtins, exceptions, py_io, version};
use crate::{
builtins::{PyStr, PyStrRef, PyTypeRef},
exceptions,
frame::FrameRef,
function::{FuncArgs, OptionalArg, PosArgs},
py_io,
stdlib::builtins,
version,
vm::{PySettings, VirtualMachine},
ItemProtocol, PyClassImpl, PyContext, PyObjectRef, PyRefExact, PyResult, PyStructSequence,
};
use num_traits::ToPrimitive;
use std::{env, mem, path};

/*
* The magic sys module.
Expand Down
Loading