Skip to content
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
2 changes: 1 addition & 1 deletion 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 Down
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
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
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
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};
4 changes: 2 additions & 2 deletions vm/src/stdlib/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,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 +210,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
64 changes: 33 additions & 31 deletions vm/src/stdlib/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

// See also:
// https://docs.python.org/3/library/time.html
use crate::{PyObjectRef, PyResult, VirtualMachine};
use crate::{PyObjectRef, VirtualMachine};

pub use time::*;

pub(crate) fn make_module(vm: &VirtualMachine) -> PyObjectRef {
let module = time::make_module(vm);
Expand All @@ -14,32 +16,6 @@ pub(crate) fn make_module(vm: &VirtualMachine) -> PyObjectRef {
module
}

#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))]
pub(crate) fn get_time(vm: &VirtualMachine) -> PyResult<f64> {
Ok(duration_since_system_now(vm)?.as_secs_f64())
}

#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))]
pub(crate) fn get_time(_vm: &VirtualMachine) -> PyResult<f64> {
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
type Date;
#[wasm_bindgen(static_method_of = Date)]
fn now() -> f64;
}
// Date.now returns unix time in milliseconds, we want it in seconds
Ok(Date::now() / 1000.0)
}

fn duration_since_system_now(vm: &VirtualMachine) -> PyResult<std::time::Duration> {
use std::time::{SystemTime, UNIX_EPOCH};

SystemTime::now()
.duration_since(UNIX_EPOCH)
.map_err(|e| vm.new_value_error(format!("Time error: {:?}", e)))
}

#[pymodule(name = "time")]
mod time {
use crate::{
Expand Down Expand Up @@ -70,6 +46,14 @@ mod time {
#[allow(dead_code)]
pub(super) const NS_TO_US: i64 = 1000;

fn duration_since_system_now(vm: &VirtualMachine) -> PyResult<std::time::Duration> {
use std::time::{SystemTime, UNIX_EPOCH};

SystemTime::now()
.duration_since(UNIX_EPOCH)
.map_err(|e| vm.new_value_error(format!("Time error: {:?}", e)))
}

#[cfg(not(unix))]
#[pyfunction]
fn sleep(dur: std::time::Duration) {
Expand All @@ -79,19 +63,37 @@ mod time {
#[cfg(not(target_os = "wasi"))]
#[pyfunction]
fn time_ns(vm: &VirtualMachine) -> PyResult<u64> {
Ok(super::duration_since_system_now(vm)?.as_nanos() as u64)
Ok(duration_since_system_now(vm)?.as_nanos() as u64)
}

#[pyfunction(name = "perf_counter")] // TODO: fix
#[pyfunction]
fn time(vm: &VirtualMachine) -> PyResult<f64> {
super::get_time(vm)
pub fn time(vm: &VirtualMachine) -> PyResult<f64> {
_time(vm)
}

#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))]
fn _time(vm: &VirtualMachine) -> PyResult<f64> {
Ok(duration_since_system_now(vm)?.as_secs_f64())
}

#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))]
fn _time(_vm: &VirtualMachine) -> PyResult<f64> {
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
type Date;
#[wasm_bindgen(static_method_of = Date)]
fn now() -> f64;
}
// Date.now returns unix time in milliseconds, we want it in seconds
Ok(Date::now() / 1000.0)
}

#[pyfunction]
fn monotonic(vm: &VirtualMachine) -> PyResult<f64> {
// TODO: implement proper monotonic time!
Ok(super::duration_since_system_now(vm)?.as_secs_f64())
Ok(duration_since_system_now(vm)?.as_secs_f64())
}

fn pyobj_to_naive_date_time(
Expand Down