From 0f4b3ece5f815a43de7f3830468cba237f96ace4 Mon Sep 17 00:00:00 2001 From: Jeong Yunwon Date: Mon, 18 Apr 2022 04:02:47 +0900 Subject: [PATCH 1/8] hide PyObjectPayload from crate root --- vm/src/function.rs | 5 ++--- vm/src/lib.rs | 3 +-- vm/src/protocol/buffer.rs | 5 +++-- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/vm/src/function.rs b/vm/src/function.rs index c24685bc9d..a070047d4d 100644 --- a/vm/src/function.rs +++ b/vm/src/function.rs @@ -5,9 +5,8 @@ mod number; use crate::{ builtins::{PyBaseExceptionRef, PyTupleRef, PyTypeRef}, - pyobject::PyThreadingConstraint, - AsPyObject, PyObject, PyObjectPayload, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, - VirtualMachine, + pyobject::{PyObjectPayload, PyThreadingConstraint}, + AsPyObject, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, VirtualMachine, }; use indexmap::IndexMap; use itertools::Itertools; diff --git a/vm/src/lib.rs b/vm/src/lib.rs index 72f62139ba..702cc1f003 100644 --- a/vm/src/lib.rs +++ b/vm/src/lib.rs @@ -89,8 +89,7 @@ mod pyobject { pub use self::convert::{TryFromBorrowedObject, TryFromObject}; // pyobject items pub use self::pyobject::{ - AsPyObject, PyContext, PyMethod, PyObjectPayload, PyObjectWrap, PyRefExact, PyResult, - PyStructSequence, PyValue, + AsPyObject, PyContext, PyMethod, PyObjectWrap, PyRefExact, PyResult, PyStructSequence, PyValue, }; // pyobjectrc items pub use self::pyobject::{PyObject, PyObjectRef, PyObjectView, PyObjectWeak, PyRef, PyWeakRef}; diff --git a/vm/src/protocol/buffer.rs b/vm/src/protocol/buffer.rs index a152f442f4..00e6b410ed 100644 --- a/vm/src/protocol/buffer.rs +++ b/vm/src/protocol/buffer.rs @@ -6,10 +6,11 @@ use crate::{ borrow::{BorrowedValue, BorrowedValueMut}, lock::{MapImmutable, PyMutex, PyMutexGuard}, }, + pyobject::PyObjectPayload, sliceable::wrap_index, types::{Constructor, Unconstructible}, - AsPyObject, PyObject, PyObjectPayload, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, - PyResult, PyValue, TryFromBorrowedObject, VirtualMachine, + AsPyObject, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult, PyValue, + TryFromBorrowedObject, VirtualMachine, }; use itertools::Itertools; use std::{borrow::Cow, fmt::Debug, ops::Range}; From f6d70c817690c1b00050a143f4c46068e0a7da2f Mon Sep 17 00:00:00 2001 From: Jeong Yunwon Date: Mon, 18 Apr 2022 05:06:51 +0900 Subject: [PATCH 2/8] Into{PyObject,PyException,Pyresult} -> To{..} The naming convention was wrong --- derive/src/pystructseq.rs | 6 +- stdlib/src/array.rs | 30 ++++---- stdlib/src/json.rs | 8 +- stdlib/src/posixsubprocess.rs | 4 +- stdlib/src/re.rs | 10 +-- stdlib/src/resource.rs | 12 +-- stdlib/src/scproxy.rs | 4 +- stdlib/src/select.rs | 15 ++-- stdlib/src/socket.rs | 81 ++++++++++---------- stdlib/src/ssl.rs | 14 ++-- stdlib/src/termios.rs | 16 ++-- vm/src/buffer.rs | 6 +- vm/src/builtins/bytearray.rs | 6 +- vm/src/builtins/bytes.rs | 14 ++-- vm/src/builtins/complex.rs | 8 +- vm/src/builtins/dict.rs | 30 ++++---- vm/src/builtins/enumerate.rs | 4 +- vm/src/builtins/float.rs | 18 ++--- vm/src/builtins/function.rs | 4 +- vm/src/builtins/function/jitfunc.rs | 12 +-- vm/src/builtins/genericalias.rs | 11 +-- vm/src/builtins/getset.rs | 18 ++--- vm/src/builtins/int.rs | 14 ++-- vm/src/builtins/list.rs | 6 +- vm/src/builtins/mappingproxy.rs | 14 ++-- vm/src/builtins/memory.rs | 8 +- vm/src/builtins/module.rs | 4 +- vm/src/builtins/pybool.rs | 18 ++--- vm/src/builtins/pystr.rs | 64 ++++++++-------- vm/src/builtins/pyunion.rs | 6 +- vm/src/builtins/singletons.rs | 12 +-- vm/src/builtins/slice.rs | 6 +- vm/src/builtins/tuple.rs | 14 ++-- vm/src/codecs.rs | 6 +- vm/src/dictdatatype.rs | 24 +++--- vm/src/exceptions.rs | 16 ++-- vm/src/format.rs | 12 +-- vm/src/frame.rs | 4 +- vm/src/function.rs | 38 +++++----- vm/src/function/argument.rs | 6 +- vm/src/function/arithmetic.rs | 10 +-- vm/src/protocol/iter.rs | 16 ++-- vm/src/protocol/mapping.rs | 10 +-- vm/src/protocol/object.rs | 18 ++--- vm/src/protocol/sequence.rs | 14 ++-- vm/src/pyobject.rs | 44 +++++------ vm/src/scope.rs | 4 +- vm/src/stdlib/io.rs | 30 ++++---- vm/src/stdlib/itertools.rs | 6 +- vm/src/stdlib/marshal.rs | 26 +++---- vm/src/stdlib/nt.rs | 21 +++-- vm/src/stdlib/os.rs | 114 ++++++++++++++-------------- vm/src/stdlib/posix.rs | 106 +++++++++++++------------- vm/src/stdlib/pwd.rs | 8 +- vm/src/stdlib/signal.rs | 6 +- vm/src/stdlib/sre.rs | 20 ++--- vm/src/stdlib/string.rs | 20 ++--- vm/src/stdlib/sys.rs | 4 +- vm/src/stdlib/sysconfigdata.rs | 4 +- vm/src/stdlib/thread.rs | 4 +- vm/src/stdlib/winapi.rs | 4 +- vm/src/stdlib/winreg.rs | 18 ++--- vm/src/types/slot.rs | 10 +-- vm/src/utils.rs | 18 ++--- vm/src/vm.rs | 9 +-- vm/src/vm_new.rs | 8 +- wasm/lib/src/browser_module.rs | 4 +- wasm/lib/src/js_module.rs | 4 +- 68 files changed, 569 insertions(+), 594 deletions(-) diff --git a/derive/src/pystructseq.rs b/derive/src/pystructseq.rs index e7263e7230..6ec2352cfb 100644 --- a/derive/src/pystructseq.rs +++ b/derive/src/pystructseq.rs @@ -29,15 +29,15 @@ pub(crate) fn impl_pystruct_sequence(input: DeriveInput) -> Result impl ::rustpython_vm::PyStructSequence for #ty { const FIELD_NAMES: &'static [&'static str] = &[#(stringify!(#field_names)),*]; fn into_tuple(self, vm: &::rustpython_vm::VirtualMachine) -> ::rustpython_vm::builtins::PyTuple { - let items = vec![#(::rustpython_vm::function::IntoPyObject::into_pyobject( + let items = vec![#(::rustpython_vm::function::ToPyObject::to_pyobject( self.#field_names, vm, )),*]; ::rustpython_vm::builtins::PyTuple::new_unchecked(items.into_boxed_slice()) } } - impl ::rustpython_vm::function::IntoPyObject for #ty { - fn into_pyobject(self, vm: &::rustpython_vm::VirtualMachine) -> ::rustpython_vm::PyObjectRef { + impl ::rustpython_vm::function::ToPyObject for #ty { + fn to_pyobject(self, vm: &::rustpython_vm::VirtualMachine) -> ::rustpython_vm::PyObjectRef { ::rustpython_vm::PyStructSequence::into_struct_sequence(self, vm).into() } } diff --git a/stdlib/src/array.rs b/stdlib/src/array.rs index 8f9951b068..243fbbaace 100644 --- a/stdlib/src/array.rs +++ b/stdlib/src/array.rs @@ -18,8 +18,8 @@ mod array { }, class_or_notimplemented, function::{ - ArgBytesLike, ArgIntoFloat, ArgIterable, IntoPyObject, IntoPyResult, OptionalArg, - PyComparisonValue, + ArgBytesLike, ArgIntoFloat, ArgIterable, OptionalArg, PyComparisonValue, + ToPyObject, ToPyResult, }, protocol::{ BufferDescriptor, BufferMethods, BufferResizeGuard, PyBuffer, PyIterReturn, @@ -117,7 +117,7 @@ mod array { let i = v.wrap_index(i).ok_or_else(|| { vm.new_index_error("pop index out of range".to_owned()) })?; - v.remove(i).into_pyresult(vm) + v.remove(i).to_pyresult(vm) })* } } @@ -258,7 +258,7 @@ mod array { ) -> Option { match self { $(ArrayContentType::$n(v) => { - v.get(i).map(|x| x.into_pyresult(vm)) + v.get(i).map(|x| x.to_pyresult(vm)) })* } } @@ -266,7 +266,7 @@ mod array { fn getitem_by_index(&self, i: isize, vm: &VirtualMachine) -> PyResult { match self { $(ArrayContentType::$n(v) => { - v.get_item_by_index(vm, i).map(|x| x.into_pyresult(vm))? + v.get_item_by_index(vm, i).map(|x| x.to_pyresult(vm))? })* } } @@ -276,7 +276,7 @@ mod array { $(ArrayContentType::$n(v) => { let r = v.get_item_by_slice(vm, slice)?; let array = PyArray::from(ArrayContentType::$n(r)); - array.into_pyresult(vm) + array.to_pyresult(vm) })* } } @@ -568,11 +568,11 @@ mod array { } } - impl IntoPyResult for WideChar { - fn into_pyresult(self, vm: &VirtualMachine) -> PyResult { + impl ToPyResult for WideChar { + fn to_pyresult(self, vm: &VirtualMachine) -> PyResult { Ok( String::from(char::try_from(self).map_err(|e| vm.new_unicode_encode_error(e))?) - .into_pyobject(vm), + .to_pyobject(vm), ) } } @@ -1139,7 +1139,7 @@ mod array { let typecode = vm.ctx.new_str(array.typecode_str()); let values = if array.typecode() == 'u' { let s = Self::_wchar_bytes_to_string(array.get_bytes(), array.itemsize(), vm)?; - s.chars().map(|x| x.into_pyobject(vm)).collect() + s.chars().map(|x| x.to_pyobject(vm)).collect() } else { array.get_objects(vm) }; @@ -1472,15 +1472,15 @@ mod array { } }}; ($VM:ident, $BYTE:ident, $TY:ty, $BIG_ENDIAN:ident) => { - chunk_to_obj!($BYTE, $TY, $BIG_ENDIAN).into_pyobject($VM) + chunk_to_obj!($BYTE, $TY, $BIG_ENDIAN).to_pyobject($VM) }; ($VM:ident, $BYTE:ident, $SIGNED_TY:ty, $UNSIGNED_TY:ty, $SIGNED:ident, $BIG_ENDIAN:ident) => {{ let b = <[u8; ::std::mem::size_of::<$SIGNED_TY>()]>::try_from($BYTE).unwrap(); match ($SIGNED, $BIG_ENDIAN) { - (false, false) => <$UNSIGNED_TY>::from_le_bytes(b).into_pyobject($VM), - (false, true) => <$UNSIGNED_TY>::from_be_bytes(b).into_pyobject($VM), - (true, false) => <$SIGNED_TY>::from_le_bytes(b).into_pyobject($VM), - (true, true) => <$SIGNED_TY>::from_be_bytes(b).into_pyobject($VM), + (false, false) => <$UNSIGNED_TY>::from_le_bytes(b).to_pyobject($VM), + (false, true) => <$UNSIGNED_TY>::from_be_bytes(b).to_pyobject($VM), + (true, false) => <$SIGNED_TY>::from_le_bytes(b).to_pyobject($VM), + (true, true) => <$SIGNED_TY>::from_be_bytes(b).to_pyobject($VM), } }}; } diff --git a/stdlib/src/json.rs b/stdlib/src/json.rs index a1fd0fa688..658fd4edae 100644 --- a/stdlib/src/json.rs +++ b/stdlib/src/json.rs @@ -6,7 +6,7 @@ mod _json { use super::machinery; use crate::vm::{ builtins::{PyBaseExceptionRef, PyStrRef, PyTypeRef}, - function::{IntoPyObject, IntoPyResult, OptionalArg}, + function::{OptionalArg, ToPyObject, ToPyResult}, protocol::PyIterReturn, types::{Callable, Constructor}, AsPyObject, PyObjectRef, PyObjectView, PyResult, PyValue, VirtualMachine, @@ -85,7 +85,7 @@ mod _json { match c { '"' => { return scanstring(pystr, next_idx, OptionalArg::Present(self.strict), vm) - .map(|x| PyIterReturn::Return(x.into_pyobject(vm))) + .map(|x| PyIterReturn::Return(x.to_pyobject(vm))) } '{' => { // TODO: parse the object in rust @@ -206,7 +206,7 @@ mod _json { let idx = idx as usize; let mut chars = pystr.as_str().chars(); if idx > 0 && chars.nth(idx - 1).is_none() { - PyIterReturn::StopIteration(Some(vm.ctx.new_int(idx).into())).into_pyresult(vm) + PyIterReturn::StopIteration(Some(vm.ctx.new_int(idx).into())).to_pyresult(vm) } else { zelf.parse( chars.as_str(), @@ -215,7 +215,7 @@ mod _json { zelf.to_owned().into(), vm, ) - .and_then(|x| x.into_pyresult(vm)) + .and_then(|x| x.to_pyresult(vm)) } } } diff --git a/stdlib/src/posixsubprocess.rs b/stdlib/src/posixsubprocess.rs index 5044f488de..563b978487 100644 --- a/stdlib/src/posixsubprocess.rs +++ b/stdlib/src/posixsubprocess.rs @@ -19,7 +19,7 @@ pub(crate) use _posixsubprocess::make_module; #[pymodule] mod _posixsubprocess { use super::{exec, CStrPathLike, ForkExecArgs, ProcArgs}; - use crate::vm::{function::IntoPyException, PyResult, VirtualMachine}; + use crate::vm::{function::ToPyException, PyResult, VirtualMachine}; #[pyfunction] fn fork_exec(args: ForkExecArgs, vm: &VirtualMachine) -> PyResult { @@ -37,7 +37,7 @@ mod _posixsubprocess { let argv = &argv; let envp = args.env_list.as_ref().map(|s| cstrs_to_ptrs(s.as_slice())); let envp = envp.as_deref(); - match unsafe { nix::unistd::fork() }.map_err(|err| err.into_pyexception(vm))? { + match unsafe { nix::unistd::fork() }.map_err(|err| err.to_pyexception(vm))? { nix::unistd::ForkResult::Child => exec(&args, ProcArgs { argv, envp }), nix::unistd::ForkResult::Parent { child } => Ok(child.as_raw()), } diff --git a/stdlib/src/re.rs b/stdlib/src/re.rs index 3939840c11..7b341b596e 100644 --- a/stdlib/src/re.rs +++ b/stdlib/src/re.rs @@ -10,7 +10,7 @@ mod re { */ use crate::vm::{ builtins::{PyInt, PyIntRef, PyStr, PyStrRef}, - function::{IntoPyObject, OptionalArg, PosArgs}, + function::{ToPyObject, OptionalArg, PosArgs}, match_class, PyObjectRef, PyResult, PyValue, TryFromObject, VirtualMachine, }; use num_traits::Signed; @@ -418,14 +418,14 @@ mod re { match groups.len() { 0 => Ok(self .subgroup(self.captures[0].clone().unwrap()) - .into_pyobject(vm)), + .to_pyobject(vm)), 1 => self .get_group(groups.pop().unwrap(), vm) - .map(|g| g.into_pyobject(vm)), + .map(|g| g.to_pyobject(vm)), _ => { let output: Result, _> = groups .into_iter() - .map(|id| self.get_group(id, vm).map(|g| g.into_pyobject(vm))) + .map(|id| self.get_group(id, vm).map(|g| g.to_pyobject(vm))) .collect(); Ok(vm.ctx.new_tuple(output?)).into() } @@ -442,7 +442,7 @@ mod re { vm.unwrap_or_none( capture .as_ref() - .map(|bounds| self.subgroup(bounds.clone()).into_pyobject(vm)) + .map(|bounds| self.subgroup(bounds.clone()).to_pyobject(vm)) .or_else(|| default.clone()), ) }) diff --git a/stdlib/src/resource.rs b/stdlib/src/resource.rs index bad79e6a84..a3abbe223f 100644 --- a/stdlib/src/resource.rs +++ b/stdlib/src/resource.rs @@ -3,7 +3,7 @@ pub(crate) use resource::make_module; #[pymodule] mod resource { use crate::vm::{ - function::{IntoPyException, IntoPyObject}, + function::{ToPyException, ToPyObject}, stdlib::os, PyObject, PyObjectRef, PyResult, PyStructSequence, TryFromBorrowedObject, VirtualMachine, }; @@ -122,7 +122,7 @@ mod resource { if e.kind() == io::ErrorKind::InvalidInput { vm.new_value_error("invalid who parameter".to_owned()) } else { - e.into_pyexception(vm) + e.to_pyexception(vm) } }) } @@ -140,9 +140,9 @@ mod resource { } } } - impl IntoPyObject for Limits { - fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { - (self.0.rlim_cur, self.0.rlim_max).into_pyobject(vm) + impl ToPyObject for Limits { + fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { + (self.0.rlim_cur, self.0.rlim_max).to_pyobject(vm) } } @@ -180,7 +180,7 @@ mod resource { io::ErrorKind::PermissionDenied => { vm.new_value_error("not allowed to raise maximum limit".to_owned()) } - _ => e.into_pyexception(vm), + _ => e.to_pyexception(vm), }) } } diff --git a/stdlib/src/scproxy.rs b/stdlib/src/scproxy.rs index 9e5c63c855..3b9bbf67d8 100644 --- a/stdlib/src/scproxy.rs +++ b/stdlib/src/scproxy.rs @@ -6,7 +6,7 @@ mod _scproxy { use crate::vm::{ builtins::{PyDictRef, PyStr}, - function::IntoPyObject, + function::ToPyObject, PyResult, VirtualMachine, }; use system_configuration::core_foundation::{ @@ -62,7 +62,7 @@ mod _scproxy { let a_string: std::borrow::Cow = (&s).into(); PyStr::from(a_string.into_owned()) }) - .into_pyobject(vm) + .to_pyobject(vm) }) .collect(); result.set_item("exceptions", vm.ctx.new_tuple(v).into(), vm)?; diff --git a/stdlib/src/select.rs b/stdlib/src/select.rs index c4d9267c6d..ba24e45c91 100644 --- a/stdlib/src/select.rs +++ b/stdlib/src/select.rs @@ -155,7 +155,7 @@ mod decl { use super::*; use crate::vm::{ builtins::PyTypeRef, - function::{IntoPyException, OptionalOption}, + function::{OptionalOption, ToPyException}, stdlib::time, utils::Either, PyObjectRef, PyResult, VirtualMachine, @@ -215,7 +215,7 @@ mod decl { match res { Ok(_) => break, Err(err) if err.kind() == io::ErrorKind::Interrupted => {} - Err(err) => return Err(err.into_pyexception(vm)), + Err(err) => return Err(err.to_pyexception(vm)), } vm.check_signals()?; @@ -264,7 +264,7 @@ mod decl { use crate::vm::{ builtins::PyFloat, common::lock::PyMutex, - function::{IntoPyObject, OptionalArg}, + function::{OptionalArg, ToPyObject}, stdlib::io::Fildes, AsPyObject, PyValue, }; @@ -327,9 +327,8 @@ mod decl { vm: &VirtualMachine, ) -> PyResult<()> { let mut fds = self.fds.lock(); - let pfd = get_fd_mut(&mut fds, fd).ok_or_else(|| { - io::Error::from_raw_os_error(libc::ENOENT).into_pyexception(vm) - })?; + let pfd = get_fd_mut(&mut fds, fd) + .ok_or_else(|| io::Error::from_raw_os_error(libc::ENOENT).to_pyexception(vm))?; pfd.events = eventmask as i16; Ok(()) } @@ -388,13 +387,13 @@ mod decl { } } } - Err(e) => return Err(e.into_pyexception(vm)), + Err(e) => return Err(e.to_pyexception(vm)), } } Ok(fds .iter() .filter(|pfd| pfd.revents != 0) - .map(|pfd| (pfd.fd, pfd.revents & 0xfff).into_pyobject(vm)) + .map(|pfd| (pfd.fd, pfd.revents & 0xfff).to_pyobject(vm)) .collect()) } } diff --git a/stdlib/src/socket.rs b/stdlib/src/socket.rs index b1f2065719..bb1b6a2538 100644 --- a/stdlib/src/socket.rs +++ b/stdlib/src/socket.rs @@ -14,8 +14,8 @@ mod _socket { use crate::vm::{ builtins::{PyBaseExceptionRef, PyListRef, PyStrRef, PyTupleRef, PyTypeRef}, function::{ - ArgBytesLike, ArgMemoryBuffer, FuncArgs, IntoPyException, IntoPyObject, OptionalArg, - OptionalOption, + ArgBytesLike, ArgMemoryBuffer, FuncArgs, OptionalArg, OptionalOption, ToPyException, + ToPyObject, }, utils::{Either, ToCString}, AsPyObject, PyObjectRef, PyResult, PyValue, TryFromBorrowedObject, TryFromObject, @@ -286,7 +286,7 @@ mod _socket { } pub fn sock(&self, vm: &VirtualMachine) -> PyResult> { - self.sock_io().map_err(|e| e.into_pyexception(vm)) + self.sock_io().map_err(|e| e.to_pyexception(vm)) } fn init_inner( @@ -306,7 +306,7 @@ mod _socket { self.timeout.store(timeout); if timeout >= 0.0 { sock.set_nonblocking(true) - .map_err(|e| e.into_pyexception(vm))?; + .map_err(|e| e.to_pyexception(vm))?; } Ok(()) } @@ -317,7 +317,7 @@ mod _socket { F: FnMut() -> io::Result, { self.sock_op_err(vm, select, f) - .map_err(|e| e.into_pyexception(vm)) + .map_err(|e| e.to_pyexception(vm)) } /// returns Err(blocking) @@ -582,13 +582,13 @@ mod _socket { ) => { std::mem::forget(sock); - return Err(e.into_pyexception(vm)); + return Err(e.to_pyexception(vm)); } _ => {} } if socket_kind == -1 { // TODO: when socket2 cuts a new release, type will be available on all os - // socket_kind = sock.r#type().map_err(|e| e.into_pyexception(vm))?.into(); + // socket_kind = sock.r#type().map_err(|e| e.to_pyexception(vm))?.into(); let res = unsafe { c::getsockopt( sock_fileno(&sock) as _, @@ -610,7 +610,7 @@ mod _socket { target_os = "linux", ))] { if proto == -1 { - proto = sock.protocol().map_err(|e| e.into_pyexception(vm))?.map_or(0, Into::into); + proto = sock.protocol().map_err(|e| e.to_pyexception(vm))?.map_or(0, Into::into); } } else { proto = 0; @@ -631,7 +631,7 @@ mod _socket { SocketType::from(socket_kind), Some(Protocol::from(proto)), ) - .map_err(|err| err.into_pyexception(vm))?; + .map_err(|err| err.to_pyexception(vm))?; }; self.init_inner(family, socket_kind, proto, sock, vm) } @@ -639,7 +639,7 @@ mod _socket { #[pymethod] fn connect(&self, address: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> { self.connect_inner(address, "connect", vm) - .map_err(|e| e.into_pyexception(vm)) + .map_err(|e| e.to_pyexception(vm)) } #[pymethod] @@ -655,7 +655,7 @@ mod _socket { let sock_addr = self.extract_address(address, "bind", vm)?; self.sock(vm)? .bind(&sock_addr) - .map_err(|err| err.into_pyexception(vm)) + .map_err(|err| err.to_pyexception(vm)) } #[pymethod] @@ -664,7 +664,7 @@ mod _socket { let backlog = if backlog < 0 { 0 } else { backlog }; self.sock(vm)? .listen(backlog) - .map_err(|err| err.into_pyexception(vm)) + .map_err(|err| err.to_pyexception(vm)) } #[pymethod] @@ -793,14 +793,14 @@ mod _socket { while buf_offset < buf.len() { let interval = deadline .as_ref() - .map(|d| d.time_until().map_err(|e| e.into_pyexception(vm))) + .map(|d| d.time_until().map_err(|e| e.to_pyexception(vm))) .transpose()?; self.sock_op_timeout_err(vm, SelectKind::Write, interval, || { let subbuf = &buf[buf_offset..]; buf_offset += self.sock_io()?.send_with_flags(subbuf, flags)?; Ok(()) }) - .map_err(|e| e.into_pyexception(vm))?; + .map_err(|e| e.to_pyexception(vm))?; vm.check_signals()?; } Ok(()) @@ -859,7 +859,7 @@ mod _socket { let addr = self .sock(vm)? .local_addr() - .map_err(|err| err.into_pyexception(vm))?; + .map_err(|err| err.to_pyexception(vm))?; Ok(get_addr_tuple(&addr, vm)) } @@ -868,7 +868,7 @@ mod _socket { let addr = self .sock(vm)? .peer_addr() - .map_err(|err| err.into_pyexception(vm))?; + .map_err(|err| err.to_pyexception(vm))?; Ok(get_addr_tuple(&addr, vm)) } @@ -888,7 +888,7 @@ mod _socket { self.timeout.store(if block { -1.0 } else { 0.0 }); self.sock(vm)? .set_nonblocking(!block) - .map_err(|err| err.into_pyexception(vm)) + .map_err(|err| err.to_pyexception(vm)) } #[pymethod] @@ -904,7 +904,7 @@ mod _socket { // it self.sock(vm)? .set_nonblocking(timeout.is_some()) - .map_err(|err| err.into_pyexception(vm)) + .map_err(|err| err.to_pyexception(vm)) } #[pymethod] @@ -1003,7 +1003,7 @@ mod _socket { }; self.sock(vm)? .shutdown(how) - .map_err(|err| err.into_pyexception(vm)) + .map_err(|err| err.to_pyexception(vm)) } #[pyproperty(name = "type")] @@ -1088,14 +1088,14 @@ mod _socket { fn get_ip_addr_tuple(addr: &SocketAddr, vm: &VirtualMachine) -> PyObjectRef { match addr { - SocketAddr::V4(addr) => (addr.ip().to_string(), addr.port()).into_pyobject(vm), + SocketAddr::V4(addr) => (addr.ip().to_string(), addr.port()).to_pyobject(vm), SocketAddr::V6(addr) => ( addr.ip().to_string(), addr.port(), addr.flowinfo(), addr.scope_id(), ) - .into_pyobject(vm), + .to_pyobject(vm), } } @@ -1125,7 +1125,7 @@ mod _socket { } } // TODO: support more address families - _ => (String::new(), 0).into_pyobject(vm), + _ => (String::new(), 0).to_pyobject(vm), } } @@ -1140,7 +1140,7 @@ mod _socket { #[cfg(all(unix, not(target_os = "redox")))] #[pyfunction] fn sethostname(hostname: PyStrRef, vm: &VirtualMachine) -> PyResult<()> { - nix::unistd::sethostname(hostname.as_str()).map_err(|err| err.into_pyexception(vm)) + nix::unistd::sethostname(hostname.as_str()).map_err(|err| err.to_pyexception(vm)) } #[pyfunction] @@ -1249,12 +1249,12 @@ mod _socket { } } } - impl IntoPyException for IoOrPyException { - fn into_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef { + impl ToPyException for IoOrPyException { + fn to_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef { match self { Self::Timeout => timeout_error(vm), Self::Py(exc) => exc, - Self::Io(err) => err.into_pyexception(vm), + Self::Io(err) => err.to_pyexception(vm), } } } @@ -1381,7 +1381,7 @@ mod _socket { }) }) .collect::>>() - .map_err(|e| e.into_pyexception(vm))?; + .map_err(|e| e.to_pyexception(vm))?; Ok(list) } @@ -1532,7 +1532,7 @@ mod _socket { let socket_kind = socket_kind.unwrap_or(libc::SOCK_STREAM); let proto = proto.unwrap_or(0); let (a, b) = Socket::pair(family.into(), socket_kind.into(), Some(proto.into())) - .map_err(|e| e.into_pyexception(vm))?; + .map_err(|e| e.to_pyexception(vm))?; let py_a = PySocket::default(); py_a.init_inner(family, socket_kind, proto, a, vm)?; let py_b = PySocket::default(); @@ -1549,7 +1549,7 @@ mod _socket { #[pyfunction] fn if_nametoindex(name: PyObjectRef, vm: &VirtualMachine) -> PyResult { let name = crate::vm::stdlib::os::FsPath::try_from(name, true, vm)?; - let name = ffi::CString::new(name.as_bytes()).map_err(|err| err.into_pyexception(vm))?; + let name = ffi::CString::new(name.as_bytes()).map_err(|err| err.to_pyexception(vm))?; let ret = unsafe { c::if_nametoindex(name.as_ptr()) }; @@ -1589,13 +1589,13 @@ mod _socket { #[cfg(not(windows))] { let list = if_nameindex() - .map_err(|err| err.into_pyexception(vm))? + .map_err(|err| err.to_pyexception(vm))? .to_slice() .iter() .map(|iface| { let tup: (u32, String) = (iface.index(), iface.name().to_string_lossy().into_owned()); - tup.into_pyobject(vm) + tup.to_pyobject(vm) }) .collect(); @@ -1653,12 +1653,11 @@ mod _socket { { use std::ptr; - let table = MibTable::get_raw().map_err(|err| err.into_pyexception(vm))?; + let table = MibTable::get_raw().map_err(|err| err.to_pyexception(vm))?; let list = table.as_slice().iter().map(|entry| { - let name = - get_name(&entry.InterfaceLuid).map_err(|err| err.into_pyexception(vm))?; + let name = get_name(&entry.InterfaceLuid).map_err(|err| err.to_pyexception(vm))?; let tup = (entry.InterfaceIndex, name.to_string_lossy()); - Ok(tup.into_pyobject(vm)) + Ok(tup.to_pyobject(vm)) }); let list = list.collect::>()?; return Ok(list); @@ -1719,7 +1718,7 @@ mod _socket { }; let mut res = dns_lookup::getaddrinfo(None, Some("0"), Some(hints)) .map_err(|e| convert_socket_error(vm, e, SocketError::GaiError))?; - let ainfo = res.next().unwrap().map_err(|e| e.into_pyexception(vm))?; + let ainfo = res.next().unwrap().map_err(|e| e.to_pyexception(vm))?; if res.next().is_some() { return Err(vm.new_os_error("wildcard resolved to multiple address".to_owned())); } @@ -1760,7 +1759,7 @@ mod _socket { res.next() .unwrap() .map(|ainfo| ainfo.sockaddr) - .map_err(|e| e.into_pyexception(vm)) + .map_err(|e| e.to_pyexception(vm)) } fn sock_from_raw(fileno: RawSocket, vm: &VirtualMachine) -> PyResult { @@ -1833,7 +1832,7 @@ mod _socket { err_kind: SocketError, ) -> PyBaseExceptionRef { if let dns_lookup::LookupErrorKind::System = err.kind() { - return io::Error::from(err).into_pyexception(vm); + return io::Error::from(err).to_pyexception(vm); } let strerr = { #[cfg(unix)] @@ -1919,11 +1918,11 @@ mod _socket { fn dup(x: PyObjectRef, vm: &VirtualMachine) -> PyResult { let sock = get_raw_sock(x, vm)?; let sock = std::mem::ManuallyDrop::new(sock_from_raw(sock, vm)?); - let newsock = sock.try_clone().map_err(|e| e.into_pyexception(vm))?; + let newsock = sock.try_clone().map_err(|e| e.to_pyexception(vm))?; let fd = into_sock_fileno(newsock); #[cfg(windows)] crate::vm::stdlib::nt::raw_set_handle_inheritable(fd as _, false) - .map_err(|e| e.into_pyexception(vm))?; + .map_err(|e| e.to_pyexception(vm))?; Ok(fd) } @@ -1941,7 +1940,7 @@ mod _socket { if ret < 0 { let err = crate::vm::stdlib::os::errno(); if err.raw_os_error() != Some(errcode!(ECONNRESET)) { - return Err(err.into_pyexception(vm)); + return Err(err.to_pyexception(vm)); } } Ok(()) diff --git a/stdlib/src/ssl.rs b/stdlib/src/ssl.rs index 4784a12f5c..14c527f7d6 100644 --- a/stdlib/src/ssl.rs +++ b/stdlib/src/ssl.rs @@ -33,8 +33,8 @@ mod _ssl { builtins::{PyBaseExceptionRef, PyStrRef, PyType, PyTypeRef}, exceptions, function::{ - ArgBytesLike, ArgCallable, ArgMemoryBuffer, ArgStrOrBytesLike, IntoPyException, - IntoPyObject, OptionalArg, + ArgBytesLike, ArgCallable, ArgMemoryBuffer, ArgStrOrBytesLike, OptionalArg, + ToPyException, ToPyObject, }, stdlib::os::PyPathLike, types::Constructor, @@ -1157,7 +1157,7 @@ mod _ssl { "The operation did not complete (write)", ), ssl::ErrorCode::SYSCALL => match e.io_error() { - Some(io_err) => return io_err.into_pyexception(vm), + Some(io_err) => return io_err.to_pyexception(vm), None => ( vm.class("_ssl", "SSLSyscallError"), "EOF occurred in violation of protocol", @@ -1216,7 +1216,7 @@ mod _ssl { let list = name .entries() .map(|entry| { - let txt = obj2txt(entry.object(), false).into_pyobject(vm); + let txt = obj2txt(entry.object(), false).to_pyobject(vm); let data = vm.ctx.new_str(entry.data().as_utf8()?.to_owned()); Ok(vm.new_tuple(((txt, data),)).into()) }) @@ -1285,7 +1285,7 @@ mod _ssl { #[pyfunction] fn _test_decode_cert(path: PyPathLike, vm: &VirtualMachine) -> PyResult { - let pem = std::fs::read(&path).map_err(|e| e.into_pyexception(vm))?; + let pem = std::fs::read(&path).map_err(|e| e.to_pyexception(vm))?; let x509 = X509::from_pem(&pem).map_err(|e| convert_openssl_error(vm, e))?; cert_to_py(vm, &x509, false) } @@ -1399,7 +1399,7 @@ mod windows { common::ascii, vm::{ builtins::{PyFrozenSet, PyStrRef}, - function::IntoPyException, + function::ToPyException, PyObjectRef, PyResult, PyValue, VirtualMachine, }, }; @@ -1441,7 +1441,7 @@ mod windows { }); let certs = certs .collect::, _>>() - .map_err(|e: std::io::Error| e.into_pyexception(vm))?; + .map_err(|e: std::io::Error| e.to_pyexception(vm))?; Ok(certs) } } diff --git a/stdlib/src/termios.rs b/stdlib/src/termios.rs index 507a655fa3..b1673479ec 100644 --- a/stdlib/src/termios.rs +++ b/stdlib/src/termios.rs @@ -4,7 +4,7 @@ pub(crate) use self::termios::make_module; mod termios { use crate::vm::{ builtins::{PyBaseExceptionRef, PyBytes, PyInt, PyListRef, PyTypeRef}, - function::IntoPyObject, + function::ToPyObject, PyObjectRef, PyResult, TryFromObject, VirtualMachine, }; use termios::Termios; @@ -37,12 +37,12 @@ mod termios { }) .collect::>(); let out = vec![ - termios.c_iflag.into_pyobject(vm), - termios.c_oflag.into_pyobject(vm), - termios.c_cflag.into_pyobject(vm), - termios.c_lflag.into_pyobject(vm), - termios::cfgetispeed(&termios).into_pyobject(vm), - termios::cfgetospeed(&termios).into_pyobject(vm), + termios.c_iflag.to_pyobject(vm), + termios.c_oflag.to_pyobject(vm), + termios.c_cflag.to_pyobject(vm), + termios.c_lflag.to_pyobject(vm), + termios::cfgetispeed(&termios).to_pyobject(vm), + termios::cfgetospeed(&termios).to_pyobject(vm), vm.ctx.new_list(cc).into(), ]; Ok(out) @@ -94,7 +94,7 @@ mod termios { vm.new_exception( error_type(vm), vec![ - err.raw_os_error().into_pyobject(vm), + err.raw_os_error().to_pyobject(vm), vm.ctx.new_str(err.to_string()).into(), ], ) diff --git a/vm/src/buffer.rs b/vm/src/buffer.rs index 6bd2f66333..4a80bb0139 100644 --- a/vm/src/buffer.rs +++ b/vm/src/buffer.rs @@ -1,7 +1,7 @@ use crate::{ builtins::{float, PyBaseExceptionRef, PyBytesRef, PyTuple, PyTupleRef, PyTypeRef}, common::{static_cell, str::wchar_t}, - function::{ArgBytesLike, ArgIntoBool, IntoPyObject}, + function::{ArgBytesLike, ArgIntoBool, ToPyObject}, PyObjectRef, PyResult, TryFromObject, VirtualMachine, }; use half::f16; @@ -527,7 +527,7 @@ macro_rules! make_pack_float { fn unpack(vm: &VirtualMachine, rdr: &[u8]) -> PyObjectRef { let i = PackInt::unpack_int::(rdr); - <$T>::from_bits(i).into_pyobject(vm) + <$T>::from_bits(i).to_pyobject(vm) } } }; @@ -549,7 +549,7 @@ impl Packable for f16 { fn unpack(vm: &VirtualMachine, rdr: &[u8]) -> PyObjectRef { let i = PackInt::unpack_int::(rdr); - f16::from_bits(i).to_f64().into_pyobject(vm) + f16::from_bits(i).to_f64().to_pyobject(vm) } } diff --git a/vm/src/builtins/bytearray.rs b/vm/src/builtins/bytearray.rs index 3e67e856bf..a297b83b11 100644 --- a/vm/src/builtins/bytearray.rs +++ b/vm/src/builtins/bytearray.rs @@ -19,8 +19,8 @@ use crate::{ }, }, function::{ - ArgBytesLike, ArgIterable, FuncArgs, IntoPyObject, OptionalArg, OptionalOption, - PyComparisonValue, + ArgBytesLike, ArgIterable, FuncArgs, OptionalArg, OptionalOption, PyComparisonValue, + ToPyObject, }, protocol::{ BufferDescriptor, BufferMethods, BufferResizeGuard, PyBuffer, PyIterReturn, @@ -686,7 +686,7 @@ impl PyByteArray { zelf: PyRef, vm: &VirtualMachine, ) -> (PyTypeRef, PyTupleRef, Option) { - let bytes = PyBytes::from(zelf.borrow_buf().to_vec()).into_pyobject(vm); + let bytes = PyBytes::from(zelf.borrow_buf().to_vec()).to_pyobject(vm); ( zelf.class().clone(), PyTuple::new_ref(vec![bytes], &vm.ctx), diff --git a/vm/src/builtins/bytes.rs b/vm/src/builtins/bytes.rs index 9fecf2ebb9..6c3329246c 100644 --- a/vm/src/builtins/bytes.rs +++ b/vm/src/builtins/bytes.rs @@ -8,8 +8,8 @@ use crate::{ }, common::{hash::PyHash, lock::PyMutex}, function::{ - ArgBytesLike, ArgIterable, IntoPyObject, IntoPyResult, OptionalArg, OptionalOption, - PyComparisonValue, + ArgBytesLike, ArgIterable, OptionalArg, OptionalOption, PyComparisonValue, ToPyObject, + ToPyResult, }, protocol::{ BufferDescriptor, BufferMethods, PyBuffer, PyIterReturn, PyMappingMethods, @@ -50,8 +50,8 @@ impl From for PyBytes { } } -impl IntoPyObject for Vec { - fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { +impl ToPyObject for Vec { + fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { vm.ctx.new_bytes(self).into() } } @@ -90,7 +90,7 @@ impl Constructor for PyBytes { type Args = ByteInnerNewOptions; fn py_new(cls: PyTypeRef, options: Self::Args, vm: &VirtualMachine) -> PyResult { - options.get_bytes(cls, vm).into_pyresult(vm) + options.get_bytes(cls, vm).to_pyresult(vm) } } @@ -523,7 +523,7 @@ impl PyBytes { .inner .elements .iter() - .map(|x| x.into_pyobject(vm)) + .map(|x| x.to_pyobject(vm)) .collect(); PyTuple::new_ref(param, &vm.ctx) } @@ -542,7 +542,7 @@ impl PyBytes { zelf: PyRef, vm: &VirtualMachine, ) -> (PyTypeRef, PyTupleRef, Option) { - let bytes = PyBytes::from(zelf.inner.elements.clone()).into_pyobject(vm); + let bytes = PyBytes::from(zelf.inner.elements.clone()).to_pyobject(vm); ( zelf.class().clone(), PyTuple::new_ref(vec![bytes], &vm.ctx), diff --git a/vm/src/builtins/complex.rs b/vm/src/builtins/complex.rs index 5ac70bf013..f0767808ec 100644 --- a/vm/src/builtins/complex.rs +++ b/vm/src/builtins/complex.rs @@ -1,9 +1,9 @@ use super::{float, PyStr, PyTypeRef}; use crate::{ function::{ - IntoPyObject, OptionalArg, OptionalOption, + OptionalArg, OptionalOption, PyArithmeticValue::{self, *}, - PyComparisonValue, + PyComparisonValue, ToPyObject, }, pyclass::PyClassImpl, types::{Comparable, Constructor, Hashable, PyComparisonOp}, @@ -28,8 +28,8 @@ impl PyValue for PyComplex { } } -impl IntoPyObject for Complex64 { - fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { +impl ToPyObject for Complex64 { + fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { PyComplex::new_ref(self, &vm.ctx).into() } } diff --git a/vm/src/builtins/dict.rs b/vm/src/builtins/dict.rs index abf8c2f9d6..c4f79d67d5 100644 --- a/vm/src/builtins/dict.rs +++ b/vm/src/builtins/dict.rs @@ -11,8 +11,8 @@ use crate::{ common::ascii, dictdatatype::{self, DictKey}, function::{ - ArgIterable, FuncArgs, IntoPyObject, KwArgs, OptionalArg, PyArithmeticValue::*, - PyComparisonValue, + ArgIterable, FuncArgs, KwArgs, OptionalArg, PyArithmeticValue::*, PyComparisonValue, + ToPyObject, }, protocol::{PyIterIter, PyIterReturn, PyMappingMethods, PySequenceMethods}, pyclass::{PyClassDef, PyClassImpl}, @@ -163,7 +163,7 @@ impl PyDict { for key in iterable.iter(vm)? { pydict.setitem(key?, value.clone(), vm)?; } - Ok(pydict.into_pyobject(vm)) + Ok(pydict.to_pyobject(vm)) } Err(pyobj) => { for key in iterable.iter(vm)? { @@ -283,7 +283,7 @@ impl PyDict { /// Set item variant which can be called with multiple /// key types, such as str to name a notable one. - pub(crate) fn inner_setitem( + pub(crate) fn inner_setitem( &self, key: K, value: PyObjectRef, @@ -292,7 +292,7 @@ impl PyDict { self.entries.insert(vm, key, value) } - pub(crate) fn inner_delitem( + pub(crate) fn inner_delitem( &self, key: K, vm: &VirtualMachine, @@ -425,8 +425,8 @@ impl PyDict { Ok(PyDict { entries: dict }) } - pub fn contains_key(&self, key: K, vm: &VirtualMachine) -> bool { - let key = key.into_pyobject(vm); + pub fn contains_key(&self, key: K, vm: &VirtualMachine) -> bool { + let key = key.to_pyobject(vm); self.entries.contains(vm, &key).unwrap() } @@ -512,7 +512,7 @@ impl PyObjectView { self.class().is(&vm.ctx.types.dict_type) } - fn missing_opt( + fn missing_opt( &self, key: K, vm: &VirtualMachine, @@ -523,7 +523,7 @@ impl PyObjectView { } #[inline] - fn inner_getitem( + fn inner_getitem( &self, key: K, vm: &VirtualMachine, @@ -533,7 +533,7 @@ impl PyObjectView { } else if let Some(value) = self.missing_opt(key.clone(), vm)? { Ok(value) } else { - Err(vm.new_key_error(key.into_pyobject(vm))) + Err(vm.new_key_error(key.to_pyobject(vm))) } } @@ -547,7 +547,7 @@ impl PyObjectView { attrs } - pub fn get_item_opt( + pub fn get_item_opt( &self, key: K, vm: &VirtualMachine, @@ -566,7 +566,7 @@ impl PyObjectView { } } - pub fn get_item( + pub fn get_item( &self, key: K, vm: &VirtualMachine, @@ -578,7 +578,7 @@ impl PyObjectView { } } - pub fn set_item( + pub fn set_item( &self, key: K, value: PyObjectRef, @@ -591,7 +591,7 @@ impl PyObjectView { } } - pub fn del_item(&self, key: K, vm: &VirtualMachine) -> PyResult<()> { + pub fn del_item(&self, key: K, vm: &VirtualMachine) -> PyResult<()> { if self.exact_dict(vm) { self.inner_delitem(key, vm) } else { @@ -599,7 +599,7 @@ impl PyObjectView { } } - pub fn get_chain( + pub fn get_chain( &self, other: &Self, key: K, diff --git a/vm/src/builtins/enumerate.rs b/vm/src/builtins/enumerate.rs index 3602a5b683..76ac172a78 100644 --- a/vm/src/builtins/enumerate.rs +++ b/vm/src/builtins/enumerate.rs @@ -1,7 +1,7 @@ use super::{IterStatus, PositionIterInternal, PyGenericAlias, PyIntRef, PyTupleRef, PyTypeRef}; use crate::common::lock::{PyMutex, PyRwLock}; use crate::{ - function::{IntoPyObject, OptionalArg}, + function::{OptionalArg, ToPyObject}, protocol::{PyIter, PyIterReturn}, pyclass::PyClassImpl, types::{Constructor, IterNext, IterNextIterable}, @@ -72,7 +72,7 @@ impl IterNext for PyEnumerate { let mut counter = zelf.counter.write(); let position = counter.clone(); *counter += 1; - Ok(PyIterReturn::Return((position, next_obj).into_pyobject(vm))) + Ok(PyIterReturn::Return((position, next_obj).to_pyobject(vm))) } } diff --git a/vm/src/builtins/float.rs b/vm/src/builtins/float.rs index 7fb7e77929..c312cd9104 100644 --- a/vm/src/builtins/float.rs +++ b/vm/src/builtins/float.rs @@ -3,9 +3,9 @@ use crate::common::{float_ops, hash}; use crate::{ format::FormatSpec, function::{ - ArgBytesLike, IntoPyObject, OptionalArg, OptionalOption, + ArgBytesLike, OptionalArg, OptionalOption, PyArithmeticValue::{self, *}, - PyComparisonValue, + PyComparisonValue, ToPyObject, }, pyclass::PyClassImpl, types::{Comparable, Constructor, Hashable, PyComparisonOp}, @@ -36,13 +36,13 @@ impl PyValue for PyFloat { } } -impl IntoPyObject for f64 { - fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { +impl ToPyObject for f64 { + fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { vm.ctx.new_float(self).into() } } -impl IntoPyObject for f32 { - fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { +impl ToPyObject for f32 { + fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { vm.ctx.new_float(f64::from(self)).into() } } @@ -153,9 +153,9 @@ pub fn float_pow(v1: f64, v2: f64, vm: &VirtualMachine) -> PyResult { } else if v1.is_sign_negative() && (v2.floor() - v2).abs() > f64::EPSILON { let v1 = Complex64::new(v1, 0.); let v2 = Complex64::new(v2, 0.); - Ok(v1.powc(v2).into_pyobject(vm)) + Ok(v1.powc(v2).to_pyobject(vm)) } else { - Ok(v1.powf(v2).into_pyobject(vm)) + Ok(v1.powf(v2).to_pyobject(vm)) } } @@ -504,7 +504,7 @@ impl PyFloat { #[pymethod(magic)] fn getnewargs(&self, vm: &VirtualMachine) -> PyObjectRef { - (self.value,).into_pyobject(vm) + (self.value,).to_pyobject(vm) } } diff --git a/vm/src/builtins/function.rs b/vm/src/builtins/function.rs index 08697c565e..ded2851b54 100644 --- a/vm/src/builtins/function.rs +++ b/vm/src/builtins/function.rs @@ -17,7 +17,7 @@ use crate::{ AsPyObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine, }; #[cfg(feature = "jit")] -use crate::{common::lock::OnceCell, function::IntoPyObject}; +use crate::{common::lock::OnceCell, function::ToPyObject}; use itertools::Itertools; #[cfg(feature = "jit")] use rustpython_jit::CompiledCode; @@ -275,7 +275,7 @@ impl PyFunction { if let Some(jitted_code) = self.jitted_code.get() { match jitfunc::get_jit_args(self, &func_args, jitted_code, vm) { Ok(args) => { - return Ok(args.invoke().into_pyobject(vm)); + return Ok(args.invoke().to_pyobject(vm)); } Err(err) => info!( "jit: function `{}` is falling back to being interpreted because of the \ diff --git a/vm/src/builtins/function/jitfunc.rs b/vm/src/builtins/function/jitfunc.rs index ee38cc4025..e3fdc4bedd 100644 --- a/vm/src/builtins/function/jitfunc.rs +++ b/vm/src/builtins/function/jitfunc.rs @@ -1,7 +1,7 @@ use crate::{ builtins::{float, int, pybool, PyBaseExceptionRef, PyDictRef, PyFunction, PyStrRef}, bytecode::CodeFlags, - function::{FuncArgs, IntoPyObject}, + function::{FuncArgs, ToPyObject}, AsPyObject, PyObject, PyObjectRef, PyResult, TryFromObject, VirtualMachine, }; use num_traits::ToPrimitive; @@ -25,12 +25,12 @@ pub enum ArgsError { JitError(#[from] JitArgumentError), } -impl IntoPyObject for AbiValue { - fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { +impl ToPyObject for AbiValue { + fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { match self { - AbiValue::Int(i) => i.into_pyobject(vm), - AbiValue::Float(f) => f.into_pyobject(vm), - AbiValue::Bool(b) => b.into_pyobject(vm), + AbiValue::Int(i) => i.to_pyobject(vm), + AbiValue::Float(f) => f.to_pyobject(vm), + AbiValue::Bool(b) => b.to_pyobject(vm), _ => unimplemented!(), } } diff --git a/vm/src/builtins/genericalias.rs b/vm/src/builtins/genericalias.rs index d8a47db733..89f168694a 100644 --- a/vm/src/builtins/genericalias.rs +++ b/vm/src/builtins/genericalias.rs @@ -1,7 +1,7 @@ use crate::{ builtins::{PyList, PyStr, PyStrRef, PyTuple, PyTupleRef, PyType, PyTypeRef}, common::hash, - function::{FuncArgs, IntoPyObject, PyComparisonValue}, + function::{FuncArgs, PyComparisonValue, ToPyObject}, protocol::PyMappingMethods, pyclass::PyClassImpl, types::{AsMapping, Callable, Comparable, Constructor, GetAttr, Hashable, PyComparisonOp}, @@ -141,18 +141,15 @@ impl PyGenericAlias { vm, )?; - Ok( - PyGenericAlias::new(self.origin.clone(), new_args.into_pyobject(vm), vm) - .into_object(vm), - ) + Ok(PyGenericAlias::new(self.origin.clone(), new_args.to_pyobject(vm), vm).into_object(vm)) } #[pymethod(magic)] fn dir(&self, vm: &VirtualMachine) -> PyResult { let dir = vm.dir(Some(self.origin()))?; for exc in ATTR_EXCEPTIONS.iter() { - if !dir.contains((*exc).into_pyobject(vm), vm)? { - dir.append((*exc).into_pyobject(vm)); + if !dir.contains((*exc).to_pyobject(vm), vm)? { + dir.append((*exc).to_pyobject(vm)); } } Ok(dir) diff --git a/vm/src/builtins/getset.rs b/vm/src/builtins/getset.rs index bae3f94235..e8b2ccc4f9 100644 --- a/vm/src/builtins/getset.rs +++ b/vm/src/builtins/getset.rs @@ -3,7 +3,7 @@ */ use super::PyTypeRef; use crate::{ - function::{IntoPyResult, OwnedParam, RefParam}, + function::{OwnedParam, RefParam, ToPyResult}, pyclass::PyClassImpl, pyobject::PyThreadingConstraint, types::{Constructor, GetDescriptor, Unconstructible}, @@ -26,11 +26,11 @@ impl IntoPyGetterFunc<(OwnedParam, R, VirtualMachine)> for F where F: Fn(T, &VirtualMachine) -> R + 'static + Send + Sync, T: TryFromObject, - R: IntoPyResult, + R: ToPyResult, { fn get(&self, obj: PyObjectRef, vm: &VirtualMachine) -> PyResult { let obj = T::try_from_object(vm, obj)?; - (self)(obj, vm).into_pyresult(vm) + (self)(obj, vm).to_pyresult(vm) } } @@ -38,11 +38,11 @@ impl IntoPyGetterFunc<(RefParam, R, VirtualMachine)> for F where F: Fn(&S, &VirtualMachine) -> R + 'static + Send + Sync, S: PyValue, - R: IntoPyResult, + R: ToPyResult, { fn get(&self, obj: PyObjectRef, vm: &VirtualMachine) -> PyResult { let zelf = PyRef::::try_from_object(vm, obj)?; - (self)(&zelf, vm).into_pyresult(vm) + (self)(&zelf, vm).to_pyresult(vm) } } @@ -50,11 +50,11 @@ impl IntoPyGetterFunc<(OwnedParam, R)> for F where F: Fn(T) -> R + 'static + Send + Sync, T: TryFromObject, - R: IntoPyResult, + R: ToPyResult, { fn get(&self, obj: PyObjectRef, vm: &VirtualMachine) -> PyResult { let obj = T::try_from_object(vm, obj)?; - (self)(obj).into_pyresult(vm) + (self)(obj).to_pyresult(vm) } } @@ -62,11 +62,11 @@ impl IntoPyGetterFunc<(RefParam, R)> for F where F: Fn(&S) -> R + 'static + Send + Sync, S: PyValue, - R: IntoPyResult, + R: ToPyResult, { fn get(&self, obj: PyObjectRef, vm: &VirtualMachine) -> PyResult { let zelf = PyRef::::try_from_object(vm, obj)?; - (self)(&zelf).into_pyresult(vm) + (self)(&zelf).to_pyresult(vm) } } diff --git a/vm/src/builtins/int.rs b/vm/src/builtins/int.rs index d49a47f7b8..63a332370d 100644 --- a/vm/src/builtins/int.rs +++ b/vm/src/builtins/int.rs @@ -4,8 +4,8 @@ use crate::{ common::hash, format::FormatSpec, function::{ - ArgIntoBool, IntoPyObject, IntoPyResult, OptionalArg, OptionalOption, PyArithmeticValue, - PyComparisonValue, + ArgIntoBool, OptionalArg, OptionalOption, PyArithmeticValue, PyComparisonValue, ToPyObject, + ToPyResult, }, pyclass::PyClassImpl, types::{Comparable, Constructor, Hashable, PyComparisonOp}, @@ -71,8 +71,8 @@ impl PyValue for PyInt { macro_rules! impl_into_pyobject_int { ($($t:ty)*) => {$( - impl IntoPyObject for $t { - fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { + impl ToPyObject for $t { + fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { vm.ctx.new_int(self).into() } } @@ -227,7 +227,7 @@ impl Constructor for PyInt { let val = if cls.is(&vm.ctx.types.int_type) { match val.downcast_exact::(vm) { Ok(i) => { - return Ok(i.into_pyobject(vm)); + return Ok(i.to_pyobject(vm)); } Err(val) => val, } @@ -243,7 +243,7 @@ impl Constructor for PyInt { Ok(Zero::zero()) }?; - Self::with_value(cls, value, vm).into_pyresult(vm) + Self::with_value(cls, value, vm).to_pyresult(vm) } } @@ -717,7 +717,7 @@ impl PyInt { #[pymethod(magic)] fn getnewargs(&self, vm: &VirtualMachine) -> PyObjectRef { - (self.value.clone(),).into_pyobject(vm) + (self.value.clone(),).to_pyobject(vm) } } diff --git a/vm/src/builtins/list.rs b/vm/src/builtins/list.rs index 75fefc421a..d40f22dede 100644 --- a/vm/src/builtins/list.rs +++ b/vm/src/builtins/list.rs @@ -4,7 +4,7 @@ use crate::common::lock::{ }; use crate::TryFromBorrowedObject; use crate::{ - function::{FuncArgs, IntoPyObject, OptionalArg, PyComparisonValue}, + function::{FuncArgs, OptionalArg, PyComparisonValue, ToPyObject}, protocol::{PyIterReturn, PyMappingMethods, PySequence, PySequenceMethods}, pyclass::PyClassImpl, sequence::{MutObjectSequenceOp, ObjectSequenceOp, SequenceMutOp, SequenceOp}, @@ -57,8 +57,8 @@ impl PyValue for PyList { } } -impl IntoPyObject for Vec { - fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { +impl ToPyObject for Vec { + fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { PyList::new_ref(self, &vm.ctx).into() } } diff --git a/vm/src/builtins/mappingproxy.rs b/vm/src/builtins/mappingproxy.rs index 049e530e2d..a1effa0f3a 100644 --- a/vm/src/builtins/mappingproxy.rs +++ b/vm/src/builtins/mappingproxy.rs @@ -2,7 +2,7 @@ use std::borrow::Cow; use super::{PyDict, PyGenericAlias, PyList, PyStr, PyStrRef, PyTuple, PyTypeRef}; use crate::{ - function::{IntoPyObject, OptionalArg}, + function::{OptionalArg, ToPyObject}, protocol::{PyMapping, PyMappingMethods, PySequence, PySequenceMethods}, pyclass::PyClassImpl, types::{AsMapping, AsSequence, Constructor, Iterable}, @@ -111,7 +111,7 @@ impl PyMappingProxy { let obj = match &self.mapping { MappingProxyInner::Dict(d) => d.clone(), MappingProxyInner::Class(c) => { - PyDict::from_attributes(c.attributes.read().clone(), vm)?.into_pyobject(vm) + PyDict::from_attributes(c.attributes.read().clone(), vm)?.to_pyobject(vm) } }; vm.call_method(&obj, "items", ()) @@ -121,7 +121,7 @@ impl PyMappingProxy { let obj = match &self.mapping { MappingProxyInner::Dict(d) => d.clone(), MappingProxyInner::Class(c) => { - PyDict::from_attributes(c.attributes.read().clone(), vm)?.into_pyobject(vm) + PyDict::from_attributes(c.attributes.read().clone(), vm)?.to_pyobject(vm) } }; vm.call_method(&obj, "keys", ()) @@ -131,7 +131,7 @@ impl PyMappingProxy { let obj = match &self.mapping { MappingProxyInner::Dict(d) => d.clone(), MappingProxyInner::Class(c) => { - PyDict::from_attributes(c.attributes.read().clone(), vm)?.into_pyobject(vm) + PyDict::from_attributes(c.attributes.read().clone(), vm)?.to_pyobject(vm) } }; vm.call_method(&obj, "values", ()) @@ -141,7 +141,7 @@ impl PyMappingProxy { match &self.mapping { MappingProxyInner::Dict(d) => vm.call_method(d, "copy", ()), MappingProxyInner::Class(c) => { - Ok(PyDict::from_attributes(c.attributes.read().clone(), vm)?.into_pyobject(vm)) + Ok(PyDict::from_attributes(c.attributes.read().clone(), vm)?.to_pyobject(vm)) } } } @@ -150,7 +150,7 @@ impl PyMappingProxy { let obj = match &self.mapping { MappingProxyInner::Dict(d) => d.clone(), MappingProxyInner::Class(c) => { - PyDict::from_attributes(c.attributes.read().clone(), vm)?.into_pyobject(vm) + PyDict::from_attributes(c.attributes.read().clone(), vm)?.to_pyobject(vm) } }; Ok(format!("mappingproxy({})", obj.repr(vm)?)) @@ -200,7 +200,7 @@ impl Iterable for PyMappingProxy { MappingProxyInner::Dict(d) => d.clone(), MappingProxyInner::Class(c) => { // TODO: something that's much more efficient than this - PyDict::from_attributes(c.attributes.read().clone(), vm)?.into_pyobject(vm) + PyDict::from_attributes(c.attributes.read().clone(), vm)?.to_pyobject(vm) } }; let iter = obj.get_iter(vm)?; diff --git a/vm/src/builtins/memory.rs b/vm/src/builtins/memory.rs index 8bfebc8a85..2b95262b4d 100644 --- a/vm/src/builtins/memory.rs +++ b/vm/src/builtins/memory.rs @@ -9,7 +9,7 @@ use crate::{ hash::PyHash, lock::OnceCell, }, - function::{FuncArgs, IntoPyObject, OptionalArg, PyComparisonValue}, + function::{FuncArgs, OptionalArg, PyComparisonValue, ToPyObject}, protocol::{ BufferDescriptor, BufferMethods, PyBuffer, PyMappingMethods, PySequenceMethods, VecBuffer, }, @@ -173,7 +173,7 @@ impl PyMemoryView { self.desc .dim_desc .iter() - .map(|(shape, _, _)| shape.into_pyobject(vm)) + .map(|(shape, _, _)| shape.to_pyobject(vm)) .collect(), )) } @@ -185,7 +185,7 @@ impl PyMemoryView { self.desc .dim_desc .iter() - .map(|(_, stride, _)| stride.into_pyobject(vm)) + .map(|(_, stride, _)| stride.to_pyobject(vm)) .collect(), )) } @@ -197,7 +197,7 @@ impl PyMemoryView { self.desc .dim_desc .iter() - .map(|(_, _, suboffset)| suboffset.into_pyobject(vm)) + .map(|(_, _, suboffset)| suboffset.to_pyobject(vm)) .collect(), )) } diff --git a/vm/src/builtins/module.rs b/vm/src/builtins/module.rs index 4ff2d7de97..8fe6705509 100644 --- a/vm/src/builtins/module.rs +++ b/vm/src/builtins/module.rs @@ -1,7 +1,7 @@ use super::pystr::IntoPyStrRef; use super::{PyDictRef, PyStr, PyStrRef, PyTypeRef}; use crate::{ - function::{FuncArgs, IntoPyObject}, + function::{FuncArgs, ToPyObject}, pyclass::PyClassImpl, types::GetAttr, AsPyObject, PyContext, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, VirtualMachine, @@ -47,7 +47,7 @@ impl PyModule { .slots .flags .has_feature(crate::types::PyTypeFlags::HAS_DICT)); - zelf.init_module_dict(args.name.into(), args.doc.into_pyobject(vm), vm); + zelf.init_module_dict(args.name.into(), args.doc.to_pyobject(vm), vm); } fn getattr_inner(zelf: &PyObjectView, name: PyStrRef, vm: &VirtualMachine) -> PyResult { diff --git a/vm/src/builtins/pybool.rs b/vm/src/builtins/pybool.rs index fa3a7d8c83..4449243844 100644 --- a/vm/src/builtins/pybool.rs +++ b/vm/src/builtins/pybool.rs @@ -1,6 +1,6 @@ use super::{PyInt, PyStrRef, PyTypeRef}; use crate::{ - function::{IntoPyObject, OptionalArg}, + function::{OptionalArg, ToPyObject}, pyclass::PyClassImpl, types::Constructor, AsPyObject, PyContext, PyObject, PyObjectRef, PyResult, PyValue, TryFromBorrowedObject, @@ -10,8 +10,8 @@ use num_bigint::Sign; use num_traits::Zero; use std::fmt::{Debug, Formatter}; -impl IntoPyObject for bool { - fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { +impl ToPyObject for bool { + fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { vm.ctx.new_bool(self).into() } } @@ -134,9 +134,9 @@ impl PyBool { { let lhs = get_value(&lhs); let rhs = get_value(&rhs); - (lhs || rhs).into_pyobject(vm) + (lhs || rhs).to_pyobject(vm) } else { - get_py_int(&lhs).or(rhs, vm).into_pyobject(vm) + get_py_int(&lhs).or(rhs, vm).to_pyobject(vm) } } @@ -148,9 +148,9 @@ impl PyBool { { let lhs = get_value(&lhs); let rhs = get_value(&rhs); - (lhs && rhs).into_pyobject(vm) + (lhs && rhs).to_pyobject(vm) } else { - get_py_int(&lhs).and(rhs, vm).into_pyobject(vm) + get_py_int(&lhs).and(rhs, vm).to_pyobject(vm) } } @@ -162,9 +162,9 @@ impl PyBool { { let lhs = get_value(&lhs); let rhs = get_value(&rhs); - (lhs ^ rhs).into_pyobject(vm) + (lhs ^ rhs).to_pyobject(vm) } else { - get_py_int(&lhs).xor(rhs, vm).into_pyobject(vm) + get_py_int(&lhs).xor(rhs, vm).to_pyobject(vm) } } } diff --git a/vm/src/builtins/pystr.rs b/vm/src/builtins/pystr.rs index 939ba3d9e4..3bc791162f 100644 --- a/vm/src/builtins/pystr.rs +++ b/vm/src/builtins/pystr.rs @@ -7,8 +7,8 @@ use crate::{ anystr::{self, adjust_indices, AnyStr, AnyStrContainer, AnyStrWrapper}, format::{FormatSpec, FormatString, FromTemplate}, function::{ - ArgIterable, FuncArgs, IntoPyException, IntoPyObject, OptionalArg, OptionalOption, - PyComparisonValue, + ArgIterable, FuncArgs, OptionalArg, OptionalOption, PyComparisonValue, ToPyException, + ToPyObject, }, protocol::{PyIterReturn, PyMappingMethods, PySequenceMethods}, pyclass::PyClassImpl, @@ -267,13 +267,13 @@ impl IterNext for PyStrIterator { if let Some((offset, ch)) = value.char_indices().nth(internal.0.position) { internal.0.position += 1; internal.1 = offset + ch.len_utf8(); - return Ok(PyIterReturn::Return(ch.into_pyobject(vm))); + return Ok(PyIterReturn::Return(ch.to_pyobject(vm))); } } else if let Some(value) = value.get(internal.1..) { if let Some(ch) = value.chars().next() { internal.0.position += 1; internal.1 += ch.len_utf8(); - return Ok(PyIterReturn::Return(ch.into_pyobject(vm))); + return Ok(PyIterReturn::Return(ch.to_pyobject(vm))); } } internal.0.status = Exhausted; @@ -388,7 +388,7 @@ impl PyStr { let kind = zelf.kind.kind() | other.kind.kind(); Self::new_str_unchecked(bytes.into_bytes(), kind) } - .into_pyobject(vm)) + .to_pyobject(vm)) } else if let Some(radd) = vm.get_method(other.clone(), "__radd__") { // hack to get around not distinguishing number add from seq concat vm.invoke(&radd?, (zelf,)) @@ -574,7 +574,7 @@ impl PyStr { v.as_bytes() .split_str(s) .map(|s| { - unsafe { PyStr::new_ascii_unchecked(s.to_owned()) }.into_pyobject(vm) + unsafe { PyStr::new_ascii_unchecked(s.to_owned()) }.to_pyobject(vm) }) .collect() }, @@ -582,13 +582,13 @@ impl PyStr { v.as_bytes() .splitn_str(n, s) .map(|s| { - unsafe { PyStr::new_ascii_unchecked(s.to_owned()) }.into_pyobject(vm) + unsafe { PyStr::new_ascii_unchecked(s.to_owned()) }.to_pyobject(vm) }) .collect() }, |v, n, vm| { v.as_bytes().py_split_whitespace(n, |s| { - unsafe { PyStr::new_ascii_unchecked(s.to_owned()) }.into_pyobject(vm) + unsafe { PyStr::new_ascii_unchecked(s.to_owned()) }.to_pyobject(vm) }) }, ), @@ -752,7 +752,7 @@ impl PyStr { #[pymethod] fn format(&self, args: FuncArgs, vm: &VirtualMachine) -> PyResult { let format_string = - FormatString::from_str(self.as_str()).map_err(|e| e.into_pyexception(vm))?; + FormatString::from_str(self.as_str()).map_err(|e| e.to_pyexception(vm))?; format_string.format(&args, vm) } @@ -764,7 +764,7 @@ impl PyStr { fn format_map(&self, mapping: PyObjectRef, vm: &VirtualMachine) -> PyResult { match FormatString::from_str(self.as_str()) { Ok(format_string) => format_string.format_map(&mapping, vm), - Err(err) => Err(err.into_pyexception(vm)), + Err(err) => Err(err.to_pyexception(vm)), } } @@ -899,7 +899,7 @@ impl PyStr { #[pymethod] fn splitlines(&self, args: anystr::SplitLinesArgs, vm: &VirtualMachine) -> Vec { self.as_str() - .py_splitlines(args, |s| self.new_substr(s.to_owned()).into_pyobject(vm)) + .py_splitlines(args, |s| self.new_substr(s.to_owned()).to_pyobject(vm)) } #[pymethod] @@ -963,7 +963,7 @@ impl PyStr { }, self.new_substr(back), ); - Ok(partition.into_pyobject(vm)) + Ok(partition.to_pyobject(vm)) } #[pymethod] @@ -982,7 +982,7 @@ impl PyStr { }, self.new_substr(back), ) - .into_pyobject(vm)) + .to_pyobject(vm)) } /// Return `true` if the sequence is ASCII titlecase and the sequence is not @@ -1130,7 +1130,7 @@ impl PyStr { let mut translated = String::new(); for c in self.as_str().chars() { - match table.get_item((c as u32).into_pyobject(vm), vm) { + match table.get_item((c as u32).to_pyobject(vm), vm) { Ok(value) => { if let Some(text) = value.payload::() { translated.push_str(text.as_str()); @@ -1181,7 +1181,7 @@ impl PyStr { new_dict.set_item(vm.new_pyobj(c as u32), vm.ctx.none(), vm)?; } } - Ok(new_dict.into_pyobject(vm)) + Ok(new_dict.to_pyobject(vm)) } else { Err(vm.new_value_error( "the first two maketrans arguments must have equal length".to_owned(), @@ -1199,15 +1199,11 @@ impl PyStr { Ok(dict) => { for (key, val) in dict { if let Some(num) = key.payload::() { - new_dict.set_item( - num.as_bigint().to_i32().into_pyobject(vm), - val, - vm, - )?; + new_dict.set_item(num.as_bigint().to_i32().to_pyobject(vm), val, vm)?; } else if let Some(string) = key.payload::() { if string.len() == 1 { let num_value = string.as_str().chars().next().unwrap() as u32; - new_dict.set_item(num_value.into_pyobject(vm), val, vm)?; + new_dict.set_item(num_value.to_pyobject(vm), val, vm)?; } else { return Err(vm.new_value_error( "string keys in translate table must be of length 1".to_owned(), @@ -1215,7 +1211,7 @@ impl PyStr { } } } - Ok(new_dict.into_pyobject(vm)) + Ok(new_dict.to_pyobject(vm)) } _ => Err(vm.new_value_error( "if you give only one argument to maketrans it must be a dict".to_owned(), @@ -1344,38 +1340,38 @@ impl PyValue for PyStr { } } -impl IntoPyObject for String { - fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { +impl ToPyObject for String { + fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { vm.ctx.new_str(self).into() } } -impl IntoPyObject for char { - fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { +impl ToPyObject for char { + fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { vm.ctx.new_str(self.to_string()).into() } } -impl IntoPyObject for &str { - fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { +impl ToPyObject for &str { + fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { vm.ctx.new_str(self).into() } } -impl IntoPyObject for &String { - fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { +impl ToPyObject for &String { + fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { vm.ctx.new_str(self.clone()).into() } } -impl IntoPyObject for &AsciiStr { - fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { +impl ToPyObject for &AsciiStr { + fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { vm.ctx.new_str(self).into() } } -impl IntoPyObject for AsciiString { - fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { +impl ToPyObject for AsciiString { + fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { vm.ctx.new_str(self).into() } } diff --git a/vm/src/builtins/pyunion.rs b/vm/src/builtins/pyunion.rs index 46b735d69e..f8cdfbecaf 100644 --- a/vm/src/builtins/pyunion.rs +++ b/vm/src/builtins/pyunion.rs @@ -2,7 +2,7 @@ use super::genericalias; use crate::{ builtins::{PyFrozenSet, PyStr, PyStrRef, PyTuple, PyTupleRef, PyTypeRef}, common::hash, - function::{IntoPyObject, PyComparisonValue}, + function::{PyComparisonValue, ToPyObject}, protocol::PyMappingMethods, pyclass::PyClassImpl, types::{AsMapping, Comparable, GetAttr, Hashable, Iterable, PyComparisonOp}, @@ -195,7 +195,7 @@ pub fn make_union(args: PyTupleRef, vm: &VirtualMachine) -> PyObjectRef { let args = dedup_and_flatten_args(args, vm); match args.len() { 1 => args.fast_getitem(0), - _ => PyUnion::new(args, vm).into_pyobject(vm), + _ => PyUnion::new(args, vm).to_pyobject(vm), } } @@ -269,7 +269,7 @@ impl GetAttr for PyUnion { return vm.generic_getattribute(zelf.as_object().to_owned(), attr); } } - zelf.as_object().into_pyobject(vm).get_attr(attr, vm) + zelf.as_object().to_pyobject(vm).get_attr(attr, vm) } } diff --git a/vm/src/builtins/singletons.rs b/vm/src/builtins/singletons.rs index 3ec03e952f..2544039dcc 100644 --- a/vm/src/builtins/singletons.rs +++ b/vm/src/builtins/singletons.rs @@ -1,6 +1,6 @@ use super::PyTypeRef; use crate::{ - function::IntoPyObject, pyclass::PyClassImpl, types::Constructor, AsPyObject, PyContext, + function::ToPyObject, pyclass::PyClassImpl, types::Constructor, AsPyObject, PyContext, PyObjectRef, PyResult, PyValue, VirtualMachine, }; @@ -16,16 +16,16 @@ impl PyValue for PyNone { // This allows a built-in function to not return a value, mapping to // Python's behavior of returning `None` in this situation. -impl IntoPyObject for () { - fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { +impl ToPyObject for () { + fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { vm.ctx.none() } } -impl IntoPyObject for Option { - fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { +impl ToPyObject for Option { + fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { match self { - Some(x) => x.into_pyobject(vm), + Some(x) => x.to_pyobject(vm), None => vm.ctx.none(), } } diff --git a/vm/src/builtins/slice.rs b/vm/src/builtins/slice.rs index de00b5057b..aad2e90f37 100644 --- a/vm/src/builtins/slice.rs +++ b/vm/src/builtins/slice.rs @@ -1,7 +1,7 @@ // sliceobject.{h,c} in CPython use super::{PyInt, PyIntRef, PyTupleRef, PyTypeRef}; use crate::{ - function::{FuncArgs, IntoPyObject, OptionalArg, PyComparisonValue}, + function::{FuncArgs, OptionalArg, PyComparisonValue, ToPyObject}, pyclass::PyClassImpl, types::{Comparable, Constructor, Hashable, PyComparisonOp, Unhashable}, AsPyObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine, @@ -29,7 +29,7 @@ impl PyValue for PySlice { impl PySlice { #[pyproperty] fn start(&self, vm: &VirtualMachine) -> PyObjectRef { - self.start.clone().into_pyobject(vm) + self.start.clone().to_pyobject(vm) } fn start_ref<'a>(&'a self, vm: &'a VirtualMachine) -> &'a PyObject { @@ -46,7 +46,7 @@ impl PySlice { #[pyproperty] fn step(&self, vm: &VirtualMachine) -> PyObjectRef { - self.step.clone().into_pyobject(vm) + self.step.clone().to_pyobject(vm) } fn step_ref<'a>(&'a self, vm: &'a VirtualMachine) -> &'a PyObject { diff --git a/vm/src/builtins/tuple.rs b/vm/src/builtins/tuple.rs index b727a5cf10..36fbf4e034 100644 --- a/vm/src/builtins/tuple.rs +++ b/vm/src/builtins/tuple.rs @@ -2,7 +2,7 @@ use super::{PositionIterInternal, PyGenericAlias, PyTypeRef}; use crate::common::{hash::PyHash, lock::PyMutex}; use crate::{ convert::{TransmuteFromObject, TryFromBorrowedObject}, - function::{IntoPyObject, OptionalArg, PyArithmeticValue, PyComparisonValue}, + function::{OptionalArg, PyArithmeticValue, PyComparisonValue, ToPyObject}, protocol::{PyIterReturn, PyMappingMethods, PySequenceMethods}, pyclass::PyClassImpl, sequence::{ObjectSequenceOp, SequenceOp}, @@ -58,14 +58,14 @@ impl IntoPyTuple for Vec { macro_rules! impl_intopyobj_tuple { ($(($T:ident, $idx:tt)),+) => { - impl<$($T: IntoPyObject),*> IntoPyTuple for ($($T,)*) { + impl<$($T: ToPyObject),*> IntoPyTuple for ($($T,)*) { fn into_pytuple(self, vm: &VirtualMachine) -> PyTupleRef { - PyTuple::new_ref(vec![$(self.$idx.into_pyobject(vm)),*], &vm.ctx) + PyTuple::new_ref(vec![$(self.$idx.to_pyobject(vm)),*], &vm.ctx) } } - impl<$($T: IntoPyObject),*> IntoPyObject for ($($T,)*) { - fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { + impl<$($T: ToPyObject),*> ToPyObject for ($($T,)*) { + fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { self.into_pytuple(vm).into() } } @@ -507,9 +507,9 @@ impl From> for PyTupleRef { } } -impl IntoPyObject for PyTupleTyped { +impl ToPyObject for PyTupleTyped { #[inline] - fn into_pyobject(self, _vm: &VirtualMachine) -> PyObjectRef { + fn to_pyobject(self, _vm: &VirtualMachine) -> PyObjectRef { self.tuple.into() } } diff --git a/vm/src/codecs.rs b/vm/src/codecs.rs index ead3e024b8..996c0e233a 100644 --- a/vm/src/codecs.rs +++ b/vm/src/codecs.rs @@ -1,7 +1,7 @@ use crate::{ builtins::{PyBaseExceptionRef, PyBytesRef, PyStr, PyStrRef, PyTuple, PyTupleRef}, common::{ascii, lock::PyRwLock}, - function::IntoPyObject, + function::ToPyObject, AsPyObject, PyContext, PyObject, PyObjectRef, PyResult, PyValue, TryFromObject, VirtualMachine, }; use std::{borrow::Cow, collections::HashMap, fmt::Write, ops::Range}; @@ -133,9 +133,9 @@ impl TryFromObject for PyCodec { } } -impl IntoPyObject for PyCodec { +impl ToPyObject for PyCodec { #[inline] - fn into_pyobject(self, _vm: &VirtualMachine) -> PyObjectRef { + fn to_pyobject(self, _vm: &VirtualMachine) -> PyObjectRef { self.0.into() } } diff --git a/vm/src/dictdatatype.rs b/vm/src/dictdatatype.rs index 398912c682..ec52e8707c 100644 --- a/vm/src/dictdatatype.rs +++ b/vm/src/dictdatatype.rs @@ -9,7 +9,7 @@ use crate::common::{ }; use crate::{ builtins::{PyInt, PyStr, PyStrRef}, - function::IntoPyObject, + function::ToPyObject, AsPyObject, PyObject, PyObjectRef, PyRefExact, PyResult, VirtualMachine, }; use num_traits::ToPrimitive; @@ -254,7 +254,7 @@ impl Dict { /// Store a key pub fn insert(&self, vm: &VirtualMachine, key: K, value: T) -> PyResult<()> where - K: DictKey + IntoPyObject, + K: DictKey + ToPyObject, { let hash = key.key_hash(vm)?; let _removed = loop { @@ -278,7 +278,7 @@ impl Dict { } } else { // New key: - inner.unchecked_push(index_index, hash, key.into_pyobject(vm), value, entry_index); + inner.unchecked_push(index_index, hash, key.to_pyobject(vm), value, entry_index); break None; } }; @@ -355,12 +355,12 @@ impl Dict { /// Delete a key pub fn delete(&self, vm: &VirtualMachine, key: K) -> PyResult<()> where - K: DictKey + IntoPyObject, + K: DictKey + ToPyObject, { if self.delete_if_exists(vm, &key)? { Ok(()) } else { - Err(vm.new_key_error(key.into_pyobject(vm))) + Err(vm.new_key_error(key.to_pyobject(vm))) } } @@ -410,7 +410,7 @@ impl Dict { pub fn setdefault(&self, vm: &VirtualMachine, key: K, default: F) -> PyResult where - K: DictKey + IntoPyObject, + K: DictKey + ToPyObject, F: FnOnce() -> T, { let hash = key.key_hash(vm)?; @@ -433,13 +433,7 @@ impl Dict { } else { let value = default(); let mut inner = self.write(); - inner.unchecked_push( - index_index, - hash, - key.into_pyobject(vm), - value.clone(), - entry, - ); + inner.unchecked_push(index_index, hash, key.to_pyobject(vm), value.clone(), entry); break value; } }; @@ -453,7 +447,7 @@ impl Dict { default: F, ) -> PyResult<(PyObjectRef, T)> where - K: DictKey + IntoPyObject, + K: DictKey + ToPyObject, F: FnOnce() -> T, { let hash = key.key_hash(vm)?; @@ -475,7 +469,7 @@ impl Dict { } } else { let value = default(); - let key = key.into_pyobject(vm); + let key = key.to_pyobject(vm); let mut inner = self.write(); let ret = (key.clone(), value.clone()); inner.unchecked_push(index_index, hash, key, value, entry); diff --git a/vm/src/exceptions.rs b/vm/src/exceptions.rs index 705aeee5d9..70e50f27b8 100644 --- a/vm/src/exceptions.rs +++ b/vm/src/exceptions.rs @@ -4,7 +4,7 @@ use crate::{ builtins::{ traceback::PyTracebackRef, PyNone, PyStr, PyStrRef, PyTuple, PyTupleRef, PyType, PyTypeRef, }, - function::{ArgIterable, FuncArgs, IntoPyException, IntoPyObject}, + function::{ArgIterable, FuncArgs, ToPyException, ToPyObject}, py_io::{self, Write}, pyclass::{PyClassImpl, StaticType}, stdlib::sys, @@ -181,7 +181,7 @@ impl VirtualMachine { &self, exc: PyBaseExceptionRef, ) -> (PyObjectRef, PyObjectRef, PyObjectRef) { - let tb = exc.traceback().into_pyobject(self); + let tb = exc.traceback().to_pyobject(self); let class = exc.class().clone(); (class.into(), exc.into(), tb) } @@ -981,15 +981,15 @@ pub fn cstring_error(vm: &VirtualMachine) -> PyBaseExceptionRef { vm.new_value_error("embedded null character".to_owned()) } -impl IntoPyException for std::ffi::NulError { - fn into_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef { +impl ToPyException for std::ffi::NulError { + fn to_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef { cstring_error(vm) } } #[cfg(windows)] -impl IntoPyException for widestring::NulError { - fn into_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef { +impl ToPyException for widestring::NulError { + fn to_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef { cstring_error(vm) } } @@ -1027,7 +1027,7 @@ pub(super) mod types { #[cfg_attr(target_os = "wasi", allow(unused_imports))] use crate::{ builtins::{traceback::PyTracebackRef, PyInt, PyTupleRef, PyTypeRef}, - function::{FuncArgs, IntoPyResult}, + function::{FuncArgs, ToPyResult}, PyObjectRef, PyRef, PyResult, VirtualMachine, }; use crossbeam_utils::atomic::AtomicCell; @@ -1253,7 +1253,7 @@ pub(super) mod types { // See: `BaseException_new` if cls.name().deref() == vm.ctx.exceptions.os_error.name().deref() { match os_error_optional_new(args.args.to_vec(), vm) { - Some(error) => error.into_pyresult(vm), + Some(error) => error.to_pyresult(vm), None => PyBaseException::slot_new(cls, args, vm), } } else { diff --git a/vm/src/format.rs b/vm/src/format.rs index 448b5c86e4..ad31dbbf52 100644 --- a/vm/src/format.rs +++ b/vm/src/format.rs @@ -1,7 +1,7 @@ use crate::{ builtins::{PyBaseExceptionRef, PyStrRef}, common::float_ops, - function::{FuncArgs, IntoPyException}, + function::{FuncArgs, ToPyException}, stdlib::builtins, AsPyObject, PyObject, PyObjectRef, PyResult, VirtualMachine, }; @@ -554,8 +554,8 @@ pub(crate) enum FormatParseError { InvalidCharacterAfterRightBracket, } -impl IntoPyException for FormatParseError { - fn into_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef { +impl ToPyException for FormatParseError { + fn to_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef { match self { FormatParseError::UnmatchedBracket => { vm.new_value_error("expected '}' before end of string".to_owned()) @@ -800,8 +800,8 @@ impl FormatString { preconversion_spec, format_spec, } => { - let FieldName { field_type, parts } = FieldName::parse(field_name.as_str()) - .map_err(|e| e.into_pyexception(vm))?; + let FieldName { field_type, parts } = + FieldName::parse(field_name.as_str()).map_err(|e| e.to_pyexception(vm))?; let mut argument = field_func(field_type)?; @@ -820,7 +820,7 @@ impl FormatString { } let nested_format = - FormatString::from_str(format_spec).map_err(|e| e.into_pyexception(vm))?; + FormatString::from_str(format_spec).map_err(|e| e.to_pyexception(vm))?; let format_spec = nested_format.format_internal(vm, field_func)?; pystr = call_object_format(vm, argument, *preconversion_spec, &format_spec)?; diff --git a/vm/src/frame.rs b/vm/src/frame.rs index 6970cd4767..6889ce4296 100644 --- a/vm/src/frame.rs +++ b/vm/src/frame.rs @@ -10,7 +10,7 @@ use crate::{ bytecode, coroutine::Coro, exceptions::ExceptionCtor, - function::{ArgMapping, FuncArgs, IntoPyResult}, + function::{ArgMapping, FuncArgs, ToPyResult}, protocol::{PyIter, PyIterReturn}, scope::Scope, stdlib::builtins, @@ -398,7 +398,7 @@ impl ExecutingFrame<'_> { let ret = match thrower { Either::A(coro) => coro .throw(gen, exc_type, exc_val, exc_tb, vm) - .into_pyresult(vm), // FIXME: + .to_pyresult(vm), // FIXME: Either::B(meth) => vm.invoke(&meth, (exc_type, exc_val, exc_tb)), }; return ret.map(ExecutionResult::Yield).or_else(|err| { diff --git a/vm/src/function.rs b/vm/src/function.rs index a070047d4d..eface4b2ee 100644 --- a/vm/src/function.rs +++ b/vm/src/function.rs @@ -19,23 +19,23 @@ pub use number::{ArgIntoBool, ArgIntoComplex, ArgIntoFloat}; /// Implemented by any type that can be returned from a built-in Python function. /// -/// `IntoPyObject` has a blanket implementation for any built-in object payload, +/// `ToPyObject` has a blanket implementation for any built-in object payload, /// and should be implemented by many primitive Rust types, allowing a built-in /// function to simply return a `bool` or a `usize` for example. -pub trait IntoPyObject { - fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef; +pub trait ToPyObject { + fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef; } -pub trait IntoPyResult { - fn into_pyresult(self, vm: &VirtualMachine) -> PyResult; +pub trait ToPyResult { + fn to_pyresult(self, vm: &VirtualMachine) -> PyResult; } pub trait IntoPyRef { fn into_pyref(self, vm: &VirtualMachine) -> PyRef; } -pub trait IntoPyException { - fn into_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef; +pub trait ToPyException { + fn to_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef; } pub trait IntoFuncArgs: Sized { @@ -56,24 +56,24 @@ where } } -// A tuple of values that each implement `IntoPyObject` represents a sequence of +// A tuple of values that each implement `ToPyObject` represents a sequence of // arguments that can be bound and passed to a built-in function. macro_rules! into_func_args_from_tuple { ($(($n:tt, $T:ident)),*) => { impl<$($T,)*> IntoFuncArgs for ($($T,)*) where - $($T: IntoPyObject,)* + $($T: ToPyObject,)* { #[inline] fn into_args(self, vm: &VirtualMachine) -> FuncArgs { let ($($n,)*) = self; - PosArgs::new(vec![$($n.into_pyobject(vm),)*]).into() + PosArgs::new(vec![$($n.to_pyobject(vm),)*]).into() } #[inline] fn into_method_args(self, obj: PyObjectRef, vm: &VirtualMachine) -> FuncArgs { let ($($n,)*) = self; - PosArgs::new(vec![obj, $($n.into_pyobject(vm),)*]).into() + PosArgs::new(vec![obj, $($n.to_pyobject(vm),)*]).into() } } }; @@ -642,12 +642,12 @@ macro_rules! into_py_native_func_tuple { where F: Fn($($T,)* &VirtualMachine) -> R + PyThreadingConstraint + 'static, $($T: FromArgs,)* - R: IntoPyResult, + R: ToPyResult, { fn call_(&self, vm: &VirtualMachine, args: FuncArgs) -> PyResult { let ($($n,)*) = args.bind::<($($T,)*)>(vm)?; - (self)($($n,)* vm).into_pyresult(vm) + (self)($($n,)* vm).to_pyresult(vm) } } @@ -656,12 +656,12 @@ macro_rules! into_py_native_func_tuple { F: Fn(&S, $($T,)* &VirtualMachine) -> R + PyThreadingConstraint + 'static, S: PyValue, $($T: FromArgs,)* - R: IntoPyResult, + R: ToPyResult, { fn call_(&self, vm: &VirtualMachine, args: FuncArgs) -> PyResult { let (zelf, $($n,)*) = args.bind::<(PyRef, $($T,)*)>(vm)?; - (self)(&zelf, $($n,)* vm).into_pyresult(vm) + (self)(&zelf, $($n,)* vm).to_pyresult(vm) } } @@ -669,12 +669,12 @@ macro_rules! into_py_native_func_tuple { where F: Fn($($T,)*) -> R + PyThreadingConstraint + 'static, $($T: FromArgs,)* - R: IntoPyResult, + R: ToPyResult, { fn call_(&self, vm: &VirtualMachine, args: FuncArgs) -> PyResult { let ($($n,)*) = args.bind::<($($T,)*)>(vm)?; - (self)($($n,)*).into_pyresult(vm) + (self)($($n,)*).to_pyresult(vm) } } @@ -683,12 +683,12 @@ macro_rules! into_py_native_func_tuple { F: Fn(&S, $($T,)*) -> R + PyThreadingConstraint + 'static, S: PyValue, $($T: FromArgs,)* - R: IntoPyResult, + R: ToPyResult, { fn call_(&self, vm: &VirtualMachine, args: FuncArgs) -> PyResult { let (zelf, $($n,)*) = args.bind::<(PyRef, $($T,)*)>(vm)?; - (self)(&zelf, $($n,)*).into_pyresult(vm) + (self)(&zelf, $($n,)*).to_pyresult(vm) } } }; diff --git a/vm/src/function/argument.rs b/vm/src/function/argument.rs index c3b7b2fe99..5eec8ba621 100644 --- a/vm/src/function/argument.rs +++ b/vm/src/function/argument.rs @@ -1,4 +1,4 @@ -use super::{IntoFuncArgs, IntoPyObject}; +use super::{IntoFuncArgs, ToPyObject}; use crate::{ builtins::{iter::PySequenceIterator, PyDict, PyDictRef}, protocol::PyIter, @@ -137,9 +137,9 @@ impl PyObjectWrap for ArgMapping { } } -impl IntoPyObject for ArgMapping { +impl ToPyObject for ArgMapping { #[inline(always)] - fn into_pyobject(self, _vm: &VirtualMachine) -> PyObjectRef { + fn to_pyobject(self, _vm: &VirtualMachine) -> PyObjectRef { self.obj } } diff --git a/vm/src/function/arithmetic.rs b/vm/src/function/arithmetic.rs index a4871e5964..dc4788930e 100644 --- a/vm/src/function/arithmetic.rs +++ b/vm/src/function/arithmetic.rs @@ -1,6 +1,6 @@ use crate::{ convert::TryFromObject, - function::IntoPyObject, + function::ToPyObject, pyobject::{AsPyObject, PyObjectRef, PyResult}, VirtualMachine, }; @@ -29,13 +29,13 @@ impl TryFromObject for PyArithmeticValue { } } -impl IntoPyObject for PyArithmeticValue +impl ToPyObject for PyArithmeticValue where - T: IntoPyObject, + T: ToPyObject, { - fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { + fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { match self { - PyArithmeticValue::Implemented(v) => v.into_pyobject(vm), + PyArithmeticValue::Implemented(v) => v.to_pyobject(vm), PyArithmeticValue::NotImplemented => vm.ctx.not_implemented(), } } diff --git a/vm/src/protocol/iter.rs b/vm/src/protocol/iter.rs index 9a47d50dd5..85c60976fe 100644 --- a/vm/src/protocol/iter.rs +++ b/vm/src/protocol/iter.rs @@ -1,6 +1,6 @@ use crate::{ builtins::iter::PySequenceIterator, - function::{IntoPyObject, IntoPyResult}, + function::{ToPyObject, ToPyResult}, AsPyObject, PyObject, PyObjectRef, PyObjectWrap, PyResult, PyValue, TryFromObject, VirtualMachine, }; @@ -107,9 +107,9 @@ where } } -impl IntoPyObject for PyIter { +impl ToPyObject for PyIter { #[inline(always)] - fn into_pyobject(self, _vm: &VirtualMachine) -> PyObjectRef { + fn to_pyobject(self, _vm: &VirtualMachine) -> PyObjectRef { self.into() } } @@ -187,8 +187,8 @@ impl PyIterReturn { } } -impl IntoPyResult for PyIterReturn { - fn into_pyresult(self, vm: &VirtualMachine) -> PyResult { +impl ToPyResult for PyIterReturn { + fn to_pyresult(self, vm: &VirtualMachine) -> PyResult { match self { Self::Return(obj) => Ok(obj), Self::StopIteration(v) => Err(vm.new_stop_iteration(v)), @@ -196,9 +196,9 @@ impl IntoPyResult for PyIterReturn { } } -impl IntoPyResult for PyResult { - fn into_pyresult(self, vm: &VirtualMachine) -> PyResult { - self.and_then(|obj| obj.into_pyresult(vm)) +impl ToPyResult for PyResult { + fn to_pyresult(self, vm: &VirtualMachine) -> PyResult { + self.and_then(|obj| obj.to_pyresult(vm)) } } diff --git a/vm/src/protocol/mapping.rs b/vm/src/protocol/mapping.rs index d9a65707c0..336a80b563 100644 --- a/vm/src/protocol/mapping.rs +++ b/vm/src/protocol/mapping.rs @@ -4,7 +4,7 @@ use crate::{ PyDict, }, common::lock::OnceCell, - function::IntoPyResult, + function::ToPyResult, AsPyObject, PyObject, PyObjectRef, PyResult, VirtualMachine, }; @@ -129,7 +129,7 @@ impl PyMapping<'_> { pub fn keys(&self, vm: &VirtualMachine) -> PyResult { if let Some(dict) = self.obj.downcast_ref_if_exact::(vm) { - PyDictKeys::new(dict.to_owned()).into_pyresult(vm) + PyDictKeys::new(dict.to_owned()).to_pyresult(vm) } else { self.method_output_as_list("keys", vm) } @@ -137,7 +137,7 @@ impl PyMapping<'_> { pub fn values(&self, vm: &VirtualMachine) -> PyResult { if let Some(dict) = self.obj.downcast_ref_if_exact::(vm) { - PyDictValues::new(dict.to_owned()).into_pyresult(vm) + PyDictValues::new(dict.to_owned()).to_pyresult(vm) } else { self.method_output_as_list("values", vm) } @@ -145,7 +145,7 @@ impl PyMapping<'_> { pub fn items(&self, vm: &VirtualMachine) -> PyResult { if let Some(dict) = self.obj.downcast_ref_if_exact::(vm) { - PyDictItems::new(dict.to_owned()).into_pyresult(vm) + PyDictItems::new(dict.to_owned()).to_pyresult(vm) } else { self.method_output_as_list("items", vm) } @@ -168,6 +168,6 @@ impl PyMapping<'_> { // TODO // PySequence::from(&iter).list(vm).map(|x| x.into()) - vm.ctx.new_list(iter.try_to_value(vm)?).into_pyresult(vm) + vm.ctx.new_list(iter.try_to_value(vm)?).to_pyresult(vm) } } diff --git a/vm/src/protocol/object.rs b/vm/src/protocol/object.rs index fbfc62ff1a..8ada3ccc49 100644 --- a/vm/src/protocol/object.rs +++ b/vm/src/protocol/object.rs @@ -9,7 +9,7 @@ use crate::{ bytesinner::ByteInnerNewOptions, common::{hash::PyHash, str::to_ascii}, dictdatatype::DictKey, - function::{IntoPyObject, IntoPyResult, OptionalArg, PyArithmeticValue}, + function::{OptionalArg, PyArithmeticValue, ToPyObject, ToPyResult}, protocol::{PyIter, PyMapping, PySequence}, types::{Constructor, PyComparisonOp}, utils::Either, @@ -46,7 +46,7 @@ impl PyObjectRef { // int PyObject_GenericSetDict(PyObject *o, PyObject *value, void *context) pub fn rich_compare(self, other: Self, opid: PyComparisonOp, vm: &VirtualMachine) -> PyResult { - self._cmp(&other, opid, vm).map(|res| res.into_pyobject(vm)) + self._cmp(&other, opid, vm).map(|res| res.to_pyobject(vm)) } pub fn bytes(self, vm: &VirtualMachine) -> PyResult { @@ -411,7 +411,7 @@ impl PyObject { .ok_or_else(|| vm.new_type_error(format!("object of type '{}' has no len()", &self)))? } - pub fn get_item( + pub fn get_item( &self, needle: K, vm: &VirtualMachine, @@ -420,7 +420,7 @@ impl PyObject { return dict.get_item(needle, vm); } - let needle = needle.into_pyobject(vm); + let needle = needle.to_pyobject(vm); if let Ok(mapping) = PyMapping::try_protocol(self, vm) { mapping.subscript(&needle, vm) @@ -430,7 +430,7 @@ impl PyObject { } else { if self.class().fast_issubclass(&vm.ctx.types.type_type) { if self.is(&vm.ctx.types.type_type) { - return PyGenericAlias::new(self.class().clone(), needle, vm).into_pyresult(vm); + return PyGenericAlias::new(self.class().clone(), needle, vm).to_pyresult(vm); } if let Some(class_getitem) = @@ -443,7 +443,7 @@ impl PyObject { } } - pub fn set_item( + pub fn set_item( &self, needle: K, value: PyObjectRef, @@ -457,7 +457,7 @@ impl PyObject { let seq = PySequence::from(self); if let Some(f) = mapping.methods(vm).ass_subscript { - let needle = needle.into_pyobject(vm); + let needle = needle.to_pyobject(vm); f(&mapping, &needle, Some(value), vm) } else if let Some(f) = seq.methods(vm).ass_item { let i = needle.key_as_isize(vm)?; @@ -470,7 +470,7 @@ impl PyObject { } } - pub fn del_item( + pub fn del_item( &self, needle: K, vm: &VirtualMachine, @@ -483,7 +483,7 @@ impl PyObject { let seq = PySequence::from(self); if let Some(f) = mapping.methods(vm).ass_subscript { - let needle = needle.into_pyobject(vm); + let needle = needle.to_pyobject(vm); f(&mapping, &needle, None, vm) } else if let Some(f) = seq.methods(vm).ass_item { let i = needle.key_as_isize(vm)?; diff --git a/vm/src/protocol/sequence.rs b/vm/src/protocol/sequence.rs index 031084160b..badbb1c220 100644 --- a/vm/src/protocol/sequence.rs +++ b/vm/src/protocol/sequence.rs @@ -1,7 +1,7 @@ use crate::{ builtins::{PyList, PyListRef, PySlice, PyTuple, PyTupleRef}, common::lock::OnceCell, - function::{IntoPyObject, PyArithmeticValue}, + function::{PyArithmeticValue, ToPyObject}, protocol::PyMapping, AsPyObject, PyObject, PyObjectRef, PyResult, PyValue, VirtualMachine, }; @@ -142,7 +142,7 @@ impl PySequence<'_> { // try fallback to __mul__ if self.check(vm) { - let ret = vm._mul(self.obj, &n.into_pyobject(vm))?; + let ret = vm._mul(self.obj, &n.to_pyobject(vm))?; if let PyArithmeticValue::Implemented(ret) = PyArithmeticValue::from_object(vm, ret) { return Ok(ret); } @@ -180,7 +180,7 @@ impl PySequence<'_> { } if self.check(vm) { - let ret = vm._imul(self.obj, &n.into_pyobject(vm))?; + let ret = vm._imul(self.obj, &n.to_pyobject(vm))?; if let PyArithmeticValue::Implemented(ret) = PyArithmeticValue::from_object(vm, ret) { return Ok(ret); } @@ -224,8 +224,8 @@ impl PySequence<'_> { pub fn get_slice(&self, start: isize, stop: isize, vm: &VirtualMachine) -> PyResult { if let Ok(mapping) = PyMapping::try_protocol(self.obj, vm) { let slice = PySlice { - start: Some(start.into_pyobject(vm)), - stop: stop.into_pyobject(vm), + start: Some(start.to_pyobject(vm)), + stop: stop.to_pyobject(vm), step: None, }; mapping.subscript(&slice.into_object(vm), vm) @@ -244,8 +244,8 @@ impl PySequence<'_> { let mapping = PyMapping::from(self.obj); if let Some(f) = mapping.methods(vm).ass_subscript { let slice = PySlice { - start: Some(start.into_pyobject(vm)), - stop: stop.into_pyobject(vm), + start: Some(start.to_pyobject(vm)), + stop: stop.to_pyobject(vm), step: None, }; f(&mapping, &slice.into_object(vm), value, vm) diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index 102823e26d..2b73226bda 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -13,7 +13,7 @@ use crate::{ convert::TryFromObject, dictdatatype::Dict, exceptions, - function::{IntoFuncArgs, IntoPyNativeFunc, IntoPyObject, IntoPyRef, IntoPyResult}, + function::{IntoFuncArgs, IntoPyNativeFunc, IntoPyRef, ToPyObject, ToPyResult}, pyclass::{PyClassImpl, StaticType}, types::{PyTypeFlags, PyTypeSlots, TypeZoo}, VirtualMachine, @@ -390,9 +390,9 @@ impl Deref for PyRefExact { &self.obj } } -impl IntoPyObject for PyRefExact { +impl ToPyObject for PyRefExact { #[inline(always)] - fn into_pyobject(self, _vm: &VirtualMachine) -> PyObjectRef { + fn to_pyobject(self, _vm: &VirtualMachine) -> PyObjectRef { self.obj.into() } } @@ -500,7 +500,7 @@ where impl IntoPyRef

for T where - P: PyValue + IntoPyObject + From, + P: PyValue + ToPyObject + From, { #[inline(always)] fn into_pyref(self, vm: &VirtualMachine) -> PyRef

{ @@ -508,56 +508,56 @@ where } } -impl IntoPyObject for PyRef { +impl ToPyObject for PyRef { #[inline(always)] - fn into_pyobject(self, _vm: &VirtualMachine) -> PyObjectRef { + fn to_pyobject(self, _vm: &VirtualMachine) -> PyObjectRef { self.into() } } -impl IntoPyObject for PyObjectRef { +impl ToPyObject for PyObjectRef { #[inline(always)] - fn into_pyobject(self, _vm: &VirtualMachine) -> PyObjectRef { + fn to_pyobject(self, _vm: &VirtualMachine) -> PyObjectRef { self } } -impl IntoPyObject for &PyObject { +impl ToPyObject for &PyObject { #[inline(always)] - fn into_pyobject(self, _vm: &VirtualMachine) -> PyObjectRef { + fn to_pyobject(self, _vm: &VirtualMachine) -> PyObjectRef { self.to_owned() } } // Allows a built-in function to return any built-in object payload without -// explicitly implementing `IntoPyObject`. -impl IntoPyObject for T +// explicitly implementing `ToPyObject`. +impl ToPyObject for T where T: PyValue + Sized, { #[inline(always)] - fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { + fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { PyValue::into_object(self, vm) } } -impl IntoPyResult for T +impl ToPyResult for T where - T: IntoPyObject, + T: ToPyObject, { #[inline(always)] - fn into_pyresult(self, vm: &VirtualMachine) -> PyResult { - Ok(self.into_pyobject(vm)) + fn to_pyresult(self, vm: &VirtualMachine) -> PyResult { + Ok(self.to_pyobject(vm)) } } -impl IntoPyResult for PyResult +impl ToPyResult for PyResult where - T: IntoPyObject, + T: ToPyObject, { #[inline(always)] - fn into_pyresult(self, vm: &VirtualMachine) -> PyResult { - self.map(|res| T::into_pyobject(res, vm)) + fn to_pyresult(self, vm: &VirtualMachine) -> PyResult { + self.map(|res| T::to_pyobject(res, vm)) } } @@ -625,7 +625,7 @@ pub trait PyValue: fmt::Debug + PyThreadingConstraint + Sized + 'static { #[inline] fn into_pyresult_with_type(self, vm: &VirtualMachine, cls: PyTypeRef) -> PyResult { - self.into_ref_with_type(vm, cls).into_pyresult(vm) + self.into_ref_with_type(vm, cls).to_pyresult(vm) } } diff --git a/vm/src/scope.rs b/vm/src/scope.rs index f9113ba36d..694f7c7b3e 100644 --- a/vm/src/scope.rs +++ b/vm/src/scope.rs @@ -1,6 +1,6 @@ use crate::{ builtins::{pystr::IntoPyStrRef, PyDictRef, PyStrRef}, - function::{ArgMapping, IntoPyObject}, + function::{ArgMapping, ToPyObject}, VirtualMachine, }; use std::fmt; @@ -145,7 +145,7 @@ mod sealed { impl Sealed for super::PyStrRef {} } pub trait PyName: - sealed::Sealed + crate::dictdatatype::DictKey + Clone + IntoPyObject + IntoPyStrRef + sealed::Sealed + crate::dictdatatype::DictKey + Clone + ToPyObject + IntoPyStrRef { } impl PyName for &str {} diff --git a/vm/src/stdlib/io.rs b/vm/src/stdlib/io.rs index 69333715ca..e7271559d8 100644 --- a/vm/src/stdlib/io.rs +++ b/vm/src/stdlib/io.rs @@ -78,8 +78,8 @@ mod _io { PyThreadMutex, PyThreadMutexGuard, }, function::{ - ArgBytesLike, ArgIterable, ArgMemoryBuffer, FuncArgs, IntoPyObject, OptionalArg, - OptionalOption, + ArgBytesLike, ArgIterable, ArgMemoryBuffer, FuncArgs, OptionalArg, OptionalOption, + ToPyObject, }, protocol::{ BufferDescriptor, BufferMethods, BufferResizeGuard, PyBuffer, PyIterReturn, VecBuffer, @@ -160,8 +160,8 @@ mod _io { fn os_err(vm: &VirtualMachine, err: io::Error) -> PyBaseExceptionRef { #[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))] { - use crate::function::IntoPyException; - err.into_pyexception(vm) + use crate::function::ToPyException; + err.to_pyexception(vm) } #[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))] { @@ -571,7 +571,7 @@ mod _io { // FIXME: try to use Arc::unwrap on the bytearray to get at the inner buffer bytes.clone() }) - .into_pyobject(vm)) + .to_pyobject(vm)) } else { vm.call_method(&instance, "readall", ()) } @@ -873,7 +873,7 @@ mod _io { ) -> PyResult> { let len = buf_range.len(); let res = if let Some(buf) = buf { - let memobj = PyMemoryView::from_buffer_range(buf, buf_range, vm)?.into_pyobject(vm); + let memobj = PyMemoryView::from_buffer_range(buf, buf_range, vm)?.to_pyobject(vm); // TODO: loop if write() raises an interrupt vm.call_method(self.raw.as_ref().unwrap(), "write", (memobj,))? @@ -1422,7 +1422,7 @@ mod _io { pos: OptionalOption, vm: &VirtualMachine, ) -> PyResult { - let pos = pos.flatten().into_pyobject(vm); + let pos = pos.flatten().to_pyobject(vm); let mut data = zelf.lock(vm)?; data.check_init(vm)?; if data.writable() { @@ -2420,7 +2420,7 @@ mod _io { ..Default::default() }; if textio.decoded_chars_used.bytes == 0 { - return Ok(cookie.build().into_pyobject(vm)); + return Ok(cookie.build().to_pyobject(vm)); } let decoder_getstate = || { let state = vm.call_method(decoder, "getstate", ())?; @@ -2499,7 +2499,7 @@ mod _io { } vm.call_method(decoder, "setstate", (saved_state,))?; cookie.set_num_to_skip(num_to_skip); - Ok(cookie.build().into_pyobject(vm)) + Ok(cookie.build().to_pyobject(vm)) } #[pyproperty] @@ -3658,7 +3658,7 @@ mod fileio { builtins::{PyStr, PyStrRef, PyTypeRef}, crt_fd::Fd, function::{ - ArgBytesLike, ArgMemoryBuffer, FuncArgs, IntoPyException, OptionalArg, OptionalOption, + ArgBytesLike, ArgMemoryBuffer, FuncArgs, OptionalArg, OptionalOption, ToPyException, }, stdlib::os, AsPyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, VirtualMachine, @@ -3933,14 +3933,14 @@ mod fileio { let mut bytes = vec![0; read_byte as usize]; let n = handle .read(&mut bytes) - .map_err(|err| err.into_pyexception(vm))?; + .map_err(|err| err.to_pyexception(vm))?; bytes.truncate(n); bytes } else { let mut bytes = vec![]; handle .read_to_end(&mut bytes) - .map_err(|err| err.into_pyexception(vm))?; + .map_err(|err| err.to_pyexception(vm))?; bytes }; @@ -3960,7 +3960,7 @@ mod fileio { let mut buf = obj.borrow_buf_mut(); let mut f = handle.take(buf.len() as _); - let ret = f.read(&mut buf).map_err(|e| e.into_pyexception(vm))?; + let ret = f.read(&mut buf).map_err(|e| e.to_pyexception(vm))?; Ok(ret) } @@ -3978,7 +3978,7 @@ mod fileio { let len = obj .with_ref(|b| handle.write(b)) - .map_err(|err| err.into_pyexception(vm))?; + .map_err(|err| err.to_pyexception(vm))?; //return number of bytes written Ok(len) @@ -3993,7 +3993,7 @@ mod fileio { } let fd = zelf.fd.swap(-1); if fd >= 0 { - Fd(fd).close().map_err(|e| e.into_pyexception(vm))?; + Fd(fd).close().map_err(|e| e.to_pyexception(vm))?; } res } diff --git a/vm/src/stdlib/itertools.rs b/vm/src/stdlib/itertools.rs index 7d7dae7ed8..1d90c40a22 100644 --- a/vm/src/stdlib/itertools.rs +++ b/vm/src/stdlib/itertools.rs @@ -8,7 +8,7 @@ mod decl { }; use crate::{ builtins::{int, PyGenericAlias, PyInt, PyIntRef, PyTuple, PyTupleRef, PyTypeRef}, - function::{ArgCallable, FuncArgs, IntoPyObject, OptionalArg, OptionalOption, PosArgs}, + function::{ArgCallable, FuncArgs, OptionalArg, OptionalOption, PosArgs, ToPyObject}, protocol::{PyIter, PyIterReturn}, stdlib::sys, types::{Constructor, IterNext, IterNextIterable}, @@ -195,7 +195,7 @@ mod decl { let mut cur = zelf.cur.write(); let result = cur.clone(); *cur += &zelf.step; - Ok(PyIterReturn::Return(result.into_pyobject(vm))) + Ok(PyIterReturn::Return(result.to_pyobject(vm))) } } @@ -640,7 +640,7 @@ mod decl { state.grouper = Some(grouper.downgrade(None, vm).unwrap()); Ok(PyIterReturn::Return( - (state.current_key.as_ref().unwrap().clone(), grouper).into_pyobject(vm), + (state.current_key.as_ref().unwrap().clone(), grouper).to_pyobject(vm), )) } } diff --git a/vm/src/stdlib/marshal.rs b/vm/src/stdlib/marshal.rs index aaad31d797..ba213f1fec 100644 --- a/vm/src/stdlib/marshal.rs +++ b/vm/src/stdlib/marshal.rs @@ -8,7 +8,7 @@ mod decl { PyInt, PyList, PySet, PyStr, PyTuple, }, bytecode, - function::{ArgBytesLike, IntoPyObject}, + function::{ArgBytesLike, ToPyObject}, protocol::PyBuffer, pyobject::AsPyObject, PyObjectRef, PyResult, TryFromObject, VirtualMachine, @@ -114,7 +114,7 @@ mod decl { // Converts list of tuples to PyObjectRefs of tuples let elements: Vec = key_value_pairs .into_iter() - .map(|(k, v)| PyTuple::new_ref(vec![k, v], &vm.ctx).into_pyobject(vm)) + .map(|(k, v)| PyTuple::new_ref(vec![k, v], &vm.ctx).to_pyobject(vm)) .collect(); // Converts list of tuples to list, dump into binary let mut dict_bytes = dump_list(elements.iter(), vm)?; @@ -221,7 +221,7 @@ mod decl { ) })?; match *type_indicator { - BOOL_BYTE => Ok((buf[0] != 0).into_pyobject(vm)), + BOOL_BYTE => Ok((buf[0] != 0).to_pyobject(vm)), INT_BYTE => { let (sign_byte, uint_bytes) = buf .split_first() @@ -237,7 +237,7 @@ mod decl { } }; let pyint = BigInt::from_bytes_le(sign, uint_bytes); - Ok(pyint.into_pyobject(vm)) + Ok(pyint.to_pyobject(vm)) } FLOAT_BYTE => { let number = f64::from_le_bytes(match buf[..].try_into() { @@ -250,7 +250,7 @@ mod decl { } }); let pyfloat = PyFloat::from(number); - Ok(pyfloat.into_pyobject(vm)) + Ok(pyfloat.to_pyobject(vm)) } STR_BYTE => { let pystr = PyStr::from(match AsciiStr::from_ascii(buf) { @@ -261,11 +261,11 @@ mod decl { ) } }); - Ok(pystr.into_pyobject(vm)) + Ok(pystr.to_pyobject(vm)) } LIST_BYTE => { let elements = read_list(buf, vm)?; - Ok(elements.into_pyobject(vm)) + Ok(elements.to_pyobject(vm)) } SET_BYTE => { let elements = read_list(buf, vm)?; @@ -273,16 +273,16 @@ mod decl { for element in elements { set.add(element, vm)?; } - Ok(set.into_pyobject(vm)) + Ok(set.to_pyobject(vm)) } FROZEN_SET_BYTE => { let elements = read_list(buf, vm)?; let set = PyFrozenSet::from_iter(vm, elements.into_iter())?; - Ok(set.into_pyobject(vm)) + Ok(set.to_pyobject(vm)) } TUPLE_BYTE => { let elements = read_list(buf, vm)?; - let pytuple = PyTuple::new_ref(elements, &vm.ctx).into_pyobject(vm); + let pytuple = PyTuple::new_ref(elements, &vm.ctx).to_pyobject(vm); Ok(pytuple) } DICT_BYTE => { @@ -292,12 +292,12 @@ mod decl { _ => return Err(vm.new_value_error("Couldn't unmarshal dicitionary.".to_owned())), }); - Ok(pydict.into_pyobject(vm)) + Ok(pydict.to_pyobject(vm)) } BYTE_ARRAY => { // Following CPython, after marshaling, byte arrays are converted into bytes. let byte_array = PyBytes::from(buf[..].to_vec()); - Ok(byte_array.into_pyobject(vm)) + Ok(byte_array.to_pyobject(vm)) } _ => { // If prefix is not identifiable, assume CodeObject, error out if it doesn't match. @@ -311,7 +311,7 @@ mod decl { Ok(PyCode { code: vm.map_codeobj(code), } - .into_pyobject(vm)) + .to_pyobject(vm)) } } } diff --git a/vm/src/stdlib/nt.rs b/vm/src/stdlib/nt.rs index 5d736c8319..5346c131c8 100644 --- a/vm/src/stdlib/nt.rs +++ b/vm/src/stdlib/nt.rs @@ -13,7 +13,7 @@ pub(crate) mod module { use crate::{ builtins::{PyStrRef, PyTupleRef}, crt_fd::Fd, - function::{IntoPyException, OptionalArg}, + function::{OptionalArg, ToPyException}, stdlib::os::{ errno_err, DirFd, FollowSymlinks, PyPathLike, SupportFunc, TargetIsDirectory, _os, errno, @@ -67,12 +67,12 @@ pub(crate) mod module { } else { win_fs::symlink_file(args.src.path, args.dst.path) }; - res.map_err(|err| err.into_pyexception(vm)) + res.map_err(|err| err.to_pyexception(vm)) } #[pyfunction] fn set_inheritable(fd: i32, inheritable: bool, vm: &VirtualMachine) -> PyResult<()> { - let handle = Fd(fd).to_raw_handle().map_err(|e| e.into_pyexception(vm))?; + let handle = Fd(fd).to_raw_handle().map_err(|e| e.to_pyexception(vm))?; set_handle_inheritable(handle as _, inheritable, vm) } @@ -103,10 +103,10 @@ pub(crate) mod module { } else { fs::symlink_metadata(&path) }; - let meta = metadata.map_err(|err| err.into_pyexception(vm))?; + let meta = metadata.map_err(|err| err.to_pyexception(vm))?; let mut permissions = meta.permissions(); permissions.set_readonly(mode & S_IWRITE == 0); - fs::set_permissions(&path, permissions).map_err(|err| err.into_pyexception(vm)) + fs::set_permissions(&path, permissions).map_err(|err| err.to_pyexception(vm)) } // cwait is available on MSVC only (according to CPython) @@ -232,9 +232,8 @@ pub(crate) mod module { ) -> PyResult<()> { use std::iter::once; - let make_widestring = |s: &str| { - widestring::WideCString::from_os_str(s).map_err(|err| err.into_pyexception(vm)) - }; + let make_widestring = + |s: &str| widestring::WideCString::from_os_str(s).map_err(|err| err.to_pyexception(vm)); let path = make_widestring(path.as_str())?; @@ -271,7 +270,7 @@ pub(crate) mod module { let real = path .as_ref() .canonicalize() - .map_err(|e| e.into_pyexception(vm))?; + .map_err(|e| e.to_pyexception(vm))?; path.mode.process_path(real, vm) } @@ -353,7 +352,7 @@ pub(crate) mod module { }; } } - Err(err.into_pyexception(vm)) + Err(err.to_pyexception(vm)) } #[pyfunction] @@ -384,7 +383,7 @@ pub(crate) mod module { inheritable: bool, vm: &VirtualMachine, ) -> PyResult<()> { - raw_set_handle_inheritable(handle, inheritable).map_err(|e| e.into_pyexception(vm)) + raw_set_handle_inheritable(handle, inheritable).map_err(|e| e.to_pyexception(vm)) } #[pyfunction] diff --git a/vm/src/stdlib/os.rs b/vm/src/stdlib/os.rs index 86fb4c9a2c..5dacdffa61 100644 --- a/vm/src/stdlib/os.rs +++ b/vm/src/stdlib/os.rs @@ -1,7 +1,7 @@ use crate::{ builtins::{PyBaseExceptionRef, PyBytes, PyBytesRef, PyInt, PySet, PyStr, PyStrRef}, crt_fd::Fd, - function::{ArgumentError, FromArgs, FuncArgs, IntoPyException, IntoPyObject}, + function::{ArgumentError, FromArgs, FuncArgs, ToPyException, ToPyObject}, protocol::PyBuffer, AsPyObject, PyObject, PyObjectRef, PyResult, PyValue, TryFromBorrowedObject, TryFromObject, VirtualMachine, @@ -74,12 +74,12 @@ impl PyPathLike { #[cfg(any(unix, target_os = "wasi"))] pub fn into_cstring(self, vm: &VirtualMachine) -> PyResult { - ffi::CString::new(self.into_bytes()).map_err(|err| err.into_pyexception(vm)) + ffi::CString::new(self.into_bytes()).map_err(|err| err.to_pyexception(vm)) } #[cfg(windows)] pub fn to_widecstring(&self, vm: &VirtualMachine) -> PyResult { - widestring::WideCString::from_os_str(&self.path).map_err(|err| err.into_pyexception(vm)) + widestring::WideCString::from_os_str(&self.path).map_err(|err| err.to_pyexception(vm)) } pub fn filename(&self, vm: &VirtualMachine) -> PyResult { @@ -179,8 +179,8 @@ impl FsPath { } } -impl IntoPyObject for FsPath { - fn into_pyobject(self, _vm: &VirtualMachine) -> PyObjectRef { +impl ToPyObject for FsPath { + fn to_pyobject(self, _vm: &VirtualMachine) -> PyObjectRef { match self { Self::Str(s) => s.into(), Self::Bytes(b) => b.into(), @@ -195,7 +195,7 @@ impl TryFromObject for PyPathLike { Ok(buffer) => { let mut bytes = vec![]; buffer.append_to(&mut bytes); - PyBytes::from(bytes).into_pyobject(vm) + PyBytes::from(bytes).to_pyobject(vm) } Err(_) => obj, }; @@ -235,13 +235,13 @@ impl PathOrFd { } } -impl IntoPyException for io::Error { - fn into_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef { - (&self).into_pyexception(vm) +impl ToPyException for io::Error { + fn to_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef { + (&self).to_pyexception(vm) } } -impl IntoPyException for &'_ io::Error { - fn into_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef { +impl ToPyException for &'_ io::Error { + fn to_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef { let excs = &vm.ctx.exceptions; #[allow(unreachable_patterns)] // some errors are just aliases of each other let exc_type = match self.kind() { @@ -254,16 +254,16 @@ impl IntoPyException for &'_ io::Error { .and_then(|errno| crate::exceptions::raw_os_error_to_exc_type(errno, vm)) .unwrap_or_else(|| excs.os_error.clone()), }; - let errno = self.raw_os_error().into_pyobject(vm); + let errno = self.raw_os_error().to_pyobject(vm); let msg = vm.ctx.new_str(self.to_string()).into(); vm.new_exception(exc_type, vec![errno, msg]) } } #[cfg(unix)] -impl IntoPyException for nix::Error { - fn into_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef { - io::Error::from(self).into_pyexception(vm) +impl ToPyException for nix::Error { + fn to_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef { + io::Error::from(self).to_pyexception(vm) } } @@ -292,9 +292,9 @@ impl IOErrorBuilder { } } -impl IntoPyException for IOErrorBuilder { - fn into_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef { - let excp = self.error.into_pyexception(vm); +impl ToPyException for IOErrorBuilder { + fn to_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef { + let excp = self.error.to_pyexception(vm); if let Some(filename) = self.filename { excp.as_object() @@ -313,7 +313,7 @@ impl IntoPyException for IOErrorBuilder { /// Convert the error stored in the `errno` variable into an Exception #[inline] pub fn errno_err(vm: &VirtualMachine) -> PyBaseExceptionRef { - errno().into_pyexception(vm) + errno().to_pyexception(vm) } #[cfg(windows)] @@ -434,7 +434,7 @@ pub(super) mod _os { PyBytesRef, PyGenericAlias, PyIntRef, PyStrRef, PyTuple, PyTupleRef, PyTypeRef, }, crt_fd::{Fd, Offset}, - function::{ArgBytesLike, FuncArgs, IntoPyException, IntoPyObject, IntoPyRef, OptionalArg}, + function::{ArgBytesLike, FuncArgs, IntoPyRef, OptionalArg, ToPyException, ToPyObject}, protocol::PyIterReturn, suppress_iph, types::{IterNext, IterNextIterable}, @@ -475,7 +475,7 @@ pub(super) mod _os { #[pyfunction] fn close(fileno: i32, vm: &VirtualMachine) -> PyResult<()> { - Fd(fileno).close().map_err(|e| e.into_pyexception(vm)) + Fd(fileno).close().map_err(|e| e.to_pyexception(vm)) } #[pyfunction] @@ -535,12 +535,12 @@ pub(super) mod _os { } }; fd.map(|fd| fd.0) - .map_err(|e| IOErrorBuilder::new(e).filename(name).into_pyexception(vm)) + .map_err(|e| IOErrorBuilder::new(e).filename(name).to_pyexception(vm)) } #[pyfunction] fn fsync(fd: i32, vm: &VirtualMachine) -> PyResult<()> { - Fd(fd).fsync().map_err(|err| err.into_pyexception(vm)) + Fd(fd).fsync().map_err(|err| err.to_pyexception(vm)) } #[pyfunction] @@ -549,7 +549,7 @@ pub(super) mod _os { let mut file = Fd(fd); let n = file .read(&mut buffer) - .map_err(|err| err.into_pyexception(vm))?; + .map_err(|err| err.to_pyexception(vm))?; buffer.truncate(n); Ok(vm.ctx.new_bytes(buffer)) @@ -560,7 +560,7 @@ pub(super) mod _os { let mut file = Fd(fd); let written = data .with_ref(|b| file.write(b)) - .map_err(|err| err.into_pyexception(vm))?; + .map_err(|err| err.to_pyexception(vm))?; Ok(vm.ctx.new_int(written).into()) } @@ -577,7 +577,7 @@ pub(super) mod _os { } else { fs::remove_file(&path) }; - res.map_err(|e| IOErrorBuilder::new(e).filename(path).into_pyexception(vm)) + res.map_err(|e| IOErrorBuilder::new(e).filename(path).to_pyexception(vm)) } #[cfg(not(windows))] @@ -608,14 +608,13 @@ pub(super) mod _os { #[pyfunction] fn mkdirs(path: PyStrRef, vm: &VirtualMachine) -> PyResult<()> { - fs::create_dir_all(path.as_str()).map_err(|err| err.into_pyexception(vm)) + fs::create_dir_all(path.as_str()).map_err(|err| err.to_pyexception(vm)) } #[pyfunction] fn rmdir(path: PyPathLike, dir_fd: DirFd<0>, vm: &VirtualMachine) -> PyResult<()> { let [] = dir_fd.0; - fs::remove_dir(&path) - .map_err(|e| IOErrorBuilder::new(e).filename(path).into_pyexception(vm)) + fs::remove_dir(&path).map_err(|e| IOErrorBuilder::new(e).filename(path).to_pyexception(vm)) } const LISTDIR_FD: bool = cfg!(all(unix, not(target_os = "redox"))); @@ -625,13 +624,13 @@ pub(super) mod _os { let path = path.unwrap_or_else(|| PathOrFd::Path(PyPathLike::new_str("."))); let list = match path { PathOrFd::Path(path) => { - let dir_iter = fs::read_dir(&path).map_err(|err| err.into_pyexception(vm))?; + let dir_iter = fs::read_dir(&path).map_err(|err| err.to_pyexception(vm))?; dir_iter .map(|entry| match entry { Ok(entry_path) => path.mode.process_path(entry_path.file_name(), vm), Err(e) => Err(IOErrorBuilder::new(e) .filename(path.clone()) - .into_pyexception(vm)), + .to_pyexception(vm)), }) .collect::>()? } @@ -646,13 +645,13 @@ pub(super) mod _os { #[cfg(all(unix, not(target_os = "redox")))] { use super::ffi_ext::OsStrExt; - let new_fd = nix::unistd::dup(fno).map_err(|e| e.into_pyexception(vm))?; + let new_fd = nix::unistd::dup(fno).map_err(|e| e.to_pyexception(vm))?; let mut dir = - nix::dir::Dir::from_fd(new_fd).map_err(|e| e.into_pyexception(vm))?; + nix::dir::Dir::from_fd(new_fd).map_err(|e| e.to_pyexception(vm))?; dir.iter() .filter_map(|entry| { entry - .map_err(|e| e.into_pyexception(vm)) + .map_err(|e| e.to_pyexception(vm)) .and_then(|entry| { let fname = entry.file_name().to_bytes(); Ok(match fname { @@ -722,7 +721,7 @@ pub(super) mod _os { let mode = path.mode; let [] = dir_fd.0; let path = fs::read_link(&path) - .map_err(|err| IOErrorBuilder::new(err).filename(path).into_pyexception(vm))?; + .map_err(|err| IOErrorBuilder::new(err).filename(path).to_pyexception(vm))?; mode.process_path(path, vm) } @@ -763,7 +762,7 @@ pub(super) mod _os { if e.kind() == io::ErrorKind::NotFound { Ok(false) } else { - Err(e.into_pyexception(vm)) + Err(e.to_pyexception(vm)) } } } @@ -792,7 +791,7 @@ pub(super) mod _os { Ok(self .entry .file_type() - .map_err(|err| err.into_pyexception(vm))? + .map_err(|err| err.to_pyexception(vm))? .is_symlink()) } @@ -844,7 +843,7 @@ pub(super) mod _os { DirFd::default(), FollowSymlinks(false), ) - .map_err(|e| e.into_pyexception(vm))? + .map_err(|e| e.to_pyexception(vm))? .ok_or_else(|| crate::exceptions::cstring_error(vm))?; // Err(T) means other thread set `ino` at the mean time which is safe to ignore let _ = self.ino.compare_exchange(None, Some(stat.st_ino)); @@ -945,7 +944,7 @@ pub(super) mod _os { .into_ref(vm) .into(), )), - Err(err) => Err(err.into_pyexception(vm)), + Err(err) => Err(err.to_pyexception(vm)), }, None => { zelf.exhausted.store(true); @@ -962,7 +961,7 @@ pub(super) mod _os { OptionalArg::Missing => PyPathLike::new_str("."), }; - let entries = fs::read_dir(path.path).map_err(|err| err.into_pyexception(vm))?; + let entries = fs::read_dir(path.path).map_err(|err| err.to_pyexception(vm))?; Ok(ScandirIterator { entries: PyRwLock::new(entries), exhausted: AtomicCell::new(false), @@ -1074,7 +1073,7 @@ pub(super) mod _os { let args: FuncArgs = flatten_args(args.args.as_slice()).into(); let stat: StatResult = args.bind(vm)?; - Ok(stat.into_pyobject(vm)) + Ok(stat.to_pyobject(vm)) } } @@ -1209,9 +1208,9 @@ pub(super) mod _os { vm: &VirtualMachine, ) -> PyResult { let stat = stat_inner(file.clone(), dir_fd, follow_symlinks) - .map_err(|e| IOErrorBuilder::new(e).filename(file).into_pyexception(vm))? + .map_err(|e| IOErrorBuilder::new(e).filename(file).to_pyexception(vm))? .ok_or_else(|| crate::exceptions::cstring_error(vm))?; - Ok(StatResult::from_stat(&stat, vm).into_pyobject(vm)) + Ok(StatResult::from_stat(&stat, vm).to_pyobject(vm)) } #[pyfunction] @@ -1232,7 +1231,7 @@ pub(super) mod _os { env::current_dir() }; - res.map_err(|err| err.into_pyexception(vm)) + res.map_err(|err| err.to_pyexception(vm)) } #[pyfunction] @@ -1248,7 +1247,7 @@ pub(super) mod _os { #[pyfunction] fn chdir(path: PyPathLike, vm: &VirtualMachine) -> PyResult<()> { env::set_current_dir(&path.path) - .map_err(|err| IOErrorBuilder::new(err).filename(path).into_pyexception(vm)) + .map_err(|err| IOErrorBuilder::new(err).filename(path).to_pyexception(vm)) } #[pyfunction] @@ -1263,7 +1262,7 @@ pub(super) mod _os { IOErrorBuilder::new(err) .filename(src) .filename2(dst) - .into_pyexception(vm) + .to_pyexception(vm) }) } @@ -1299,7 +1298,7 @@ pub(super) mod _os { } let mut buf = vec![0u8; size as usize]; getrandom::getrandom(&mut buf).map_err(|e| match e.raw_os_error() { - Some(errno) => io::Error::from_raw_os_error(errno).into_pyexception(vm), + Some(errno) => io::Error::from_raw_os_error(errno).to_pyexception(vm), None => vm.new_os_error("Getting random failed".to_owned()), })?; Ok(buf) @@ -1317,7 +1316,7 @@ pub(super) mod _os { #[cfg(windows)] let res = unsafe { use winapi::um::{fileapi, winnt}; - let handle = Fd(fd).to_raw_handle().map_err(|e| e.into_pyexception(vm))?; + let handle = Fd(fd).to_raw_handle().map_err(|e| e.to_pyexception(vm))?; let mut li = winnt::LARGE_INTEGER::default(); *li.QuadPart_mut() = position; let ret = fileapi::SetFilePointer( @@ -1346,7 +1345,7 @@ pub(super) mod _os { IOErrorBuilder::new(err) .filename(src) .filename2(dst) - .into_pyexception(vm) + .to_pyexception(vm) }) } @@ -1468,7 +1467,7 @@ pub(super) mod _os { tv_usec: d.as_micros() as _, }; nix::sys::stat::utimes(path.as_ref(), &tv(acc).into(), &tv(modif).into()) - .map_err(|err| err.into_pyexception(vm)) + .map_err(|err| err.to_pyexception(vm)) } } #[cfg(windows)] @@ -1497,13 +1496,13 @@ pub(super) mod _os { .write(true) .custom_flags(winapi::um::winbase::FILE_FLAG_BACKUP_SEMANTICS) .open(path) - .map_err(|err| err.into_pyexception(vm))?; + .map_err(|err| err.to_pyexception(vm))?; let ret = unsafe { SetFileTime(f.as_raw_handle() as _, std::ptr::null(), &acc, &modif) }; if ret == 0 { - Err(io::Error::last_os_error().into_pyexception(vm)) + Err(io::Error::last_os_error().to_pyexception(vm)) } else { Ok(()) } @@ -1553,7 +1552,7 @@ pub(super) mod _os { elapsed: 0.0, }; - Ok(times_result.into_pyobject(vm)) + Ok(times_result.to_pyobject(vm)) } #[cfg(unix)] { @@ -1579,7 +1578,7 @@ pub(super) mod _os { elapsed: c as f64 / tick_for_second, }; - Ok(times_result.into_pyobject(vm)) + Ok(times_result.to_pyobject(vm)) } } @@ -1641,7 +1640,7 @@ pub(super) mod _os { #[pyfunction] pub fn ftruncate(fd: i32, length: Offset, vm: &VirtualMachine) -> PyResult<()> { - Fd(fd).ftruncate(length).map_err(|e| e.into_pyexception(vm)) + Fd(fd).ftruncate(length).map_err(|e| e.to_pyexception(vm)) } #[pyfunction] @@ -1654,9 +1653,8 @@ pub(super) mod _os { let f = OpenOptions::new() .write(true) .open(&path) - .map_err(|e| e.into_pyexception(vm))?; - f.set_len(length as u64) - .map_err(|e| e.into_pyexception(vm))?; + .map_err(|e| e.to_pyexception(vm))?; + f.set_len(length as u64).map_err(|e| e.to_pyexception(vm))?; drop(f); Ok(()) } diff --git a/vm/src/stdlib/posix.rs b/vm/src/stdlib/posix.rs index 0e72263dad..50a5958527 100644 --- a/vm/src/stdlib/posix.rs +++ b/vm/src/stdlib/posix.rs @@ -30,7 +30,7 @@ pub(crate) fn make_module(vm: &VirtualMachine) -> PyObjectRef { pub mod module { use crate::{ builtins::{PyDictRef, PyInt, PyIntRef, PyListRef, PyStrRef, PyTupleRef, PyTypeRef}, - function::{IntoPyException, IntoPyObject, OptionalArg}, + function::{OptionalArg, ToPyException, ToPyObject}, stdlib::os::{ errno_err, DirFd, FollowSymlinks, PathOrFd, PyPathLike, SupportFunc, TargetIsDirectory, _os, fs_metadata, IOErrorBuilder, @@ -241,7 +241,7 @@ pub mod module { #[pyfunction] fn getgroups(vm: &VirtualMachine) -> PyResult> { - let group_ids = getgroups_impl().map_err(|e| e.into_pyexception(vm))?; + let group_ids = getgroups_impl().map_err(|e| e.to_pyexception(vm))?; Ok(group_ids .into_iter() .map(|gid| vm.ctx.new_int(gid.as_raw()).into()) @@ -266,14 +266,14 @@ pub mod module { return Ok(metadata.is_ok()); } - let metadata = metadata.map_err(|err| err.into_pyexception(vm))?; + let metadata = metadata.map_err(|err| err.to_pyexception(vm))?; let user_id = metadata.uid(); let group_id = metadata.gid(); let mode = metadata.mode(); let perm = get_right_permission(mode, Uid::from_raw(user_id), Gid::from_raw(group_id)) - .map_err(|err| err.into_pyexception(vm))?; + .map_err(|err| err.to_pyexception(vm))?; let r_ok = !flags.contains(AccessFlags::R_OK) || perm.is_readable; let w_ok = !flags.contains(AccessFlags::W_OK) || perm.is_writable; @@ -313,7 +313,7 @@ pub mod module { #[cfg(not(target_os = "redox"))] { nix::unistd::symlinkat(&*src, args.dir_fd.get_opt(), &*dst) - .map_err(|err| err.into_pyexception(vm)) + .map_err(|err| err.to_pyexception(vm)) } #[cfg(target_os = "redox")] { @@ -330,7 +330,7 @@ pub mod module { #[cfg(not(target_os = "redox"))] #[pyfunction] fn fchdir(fd: RawFd, vm: &VirtualMachine) -> PyResult<()> { - nix::unistd::fchdir(fd).map_err(|err| err.into_pyexception(vm)) + nix::unistd::fchdir(fd).map_err(|err| err.to_pyexception(vm)) } #[cfg(not(target_os = "redox"))] @@ -342,7 +342,7 @@ pub mod module { // Use `From for io::Error` when it is available IOErrorBuilder::new(io::Error::from_raw_os_error(err as i32)) .filename(path) - .into_pyexception(vm) + .to_pyexception(vm) }) } @@ -390,7 +390,7 @@ pub mod module { // Use `From for io::Error` when it is available IOErrorBuilder::new(io::Error::from_raw_os_error(err as i32)) .filename(path) - .into_pyexception(vm) + .to_pyexception(vm) }) } @@ -515,7 +515,7 @@ pub mod module { #[pyfunction] fn sched_yield(vm: &VirtualMachine) -> PyResult<()> { - let _ = nix::sched::sched_yield().map_err(|e| e.into_pyexception(vm))?; + let _ = nix::sched::sched_yield().map_err(|e| e.to_pyexception(vm))?; Ok(()) } @@ -538,7 +538,7 @@ pub mod module { impl SchedParam { #[pyproperty] fn sched_priority(&self, vm: &VirtualMachine) -> PyObjectRef { - self.sched_priority.clone().into_pyobject(vm) + self.sched_priority.clone().to_pyobject(vm) } #[pymethod(magic)] @@ -650,7 +650,7 @@ pub mod module { param.assume_init() }; Ok(SchedParam { - sched_priority: param.sched_priority.into_pyobject(vm), + sched_priority: param.sched_priority.to_pyobject(vm), }) } @@ -690,13 +690,13 @@ pub mod module { let flags = fcntl::fcntl(fd, fcntl::FcntlArg::F_GETFD); match flags { Ok(ret) => Ok((ret & libc::FD_CLOEXEC) == 0), - Err(err) => Err(err.into_pyexception(vm)), + Err(err) => Err(err.to_pyexception(vm)), } } #[pyfunction] fn set_inheritable(fd: i32, inheritable: bool, vm: &VirtualMachine) -> PyResult<()> { - super::raw_set_inheritable(fd, inheritable).map_err(|err| err.into_pyexception(vm)) + super::raw_set_inheritable(fd, inheritable).map_err(|err| err.to_pyexception(vm)) } #[pyfunction] @@ -704,7 +704,7 @@ pub mod module { let flags = fcntl::fcntl(fd, fcntl::FcntlArg::F_GETFL); match flags { Ok(ret) => Ok((ret & libc::O_NONBLOCK) == 0), - Err(err) => Err(err.into_pyexception(vm)), + Err(err) => Err(err.to_pyexception(vm)), } } @@ -721,14 +721,14 @@ pub mod module { } Ok(()) }; - _set_flag().map_err(|err: nix::Error| err.into_pyexception(vm)) + _set_flag().map_err(|err: nix::Error| err.to_pyexception(vm)) } #[pyfunction] fn pipe(vm: &VirtualMachine) -> PyResult<(RawFd, RawFd)> { use nix::unistd::close; use nix::unistd::pipe; - let (rfd, wfd) = pipe().map_err(|err| err.into_pyexception(vm))?; + let (rfd, wfd) = pipe().map_err(|err| err.to_pyexception(vm))?; set_inheritable(rfd, false, vm) .and_then(|_| set_inheritable(wfd, false, vm)) .map_err(|err| { @@ -752,7 +752,7 @@ pub mod module { #[pyfunction] fn pipe2(flags: libc::c_int, vm: &VirtualMachine) -> PyResult<(RawFd, RawFd)> { let oflags = fcntl::OFlag::from_bits_truncate(flags); - nix::unistd::pipe2(oflags).map_err(|err| err.into_pyexception(vm)) + nix::unistd::pipe2(oflags).map_err(|err| err.to_pyexception(vm)) } #[pyfunction] @@ -781,7 +781,7 @@ pub mod module { body().map_err(|err| { IOErrorBuilder::new(err) .filename(err_path) - .into_pyexception(vm) + .to_pyexception(vm) }) } @@ -791,7 +791,7 @@ pub mod module { fd, nix::sys::stat::Mode::from_bits(mode as libc::mode_t).unwrap(), ) - .map_err(|err| err.into_pyexception(vm)) + .map_err(|err| err.to_pyexception(vm)) } #[cfg(not(target_os = "redox"))] @@ -857,7 +857,7 @@ pub mod module { unistd::execv(&path, &argv) .map(|_ok| ()) - .map_err(|err| err.into_pyexception(vm)) + .map_err(|err| err.to_pyexception(vm)) } #[pyfunction] @@ -900,13 +900,13 @@ pub mod module { entry.push(b'='); entry.extend_from_slice(&value); - CString::new(entry).map_err(|err| err.into_pyexception(vm)) + CString::new(entry).map_err(|err| err.to_pyexception(vm)) }) .collect::, _>>()?; let env: Vec<&CStr> = env.iter().map(|entry| entry.as_c_str()).collect(); - unistd::execve(&path, &argv, &env).map_err(|err| err.into_pyexception(vm))?; + unistd::execve(&path, &argv, &env).map_err(|err| err.to_pyexception(vm))?; Ok(()) } @@ -931,7 +931,7 @@ pub mod module { #[pyfunction] fn getpgid(pid: u32, vm: &VirtualMachine) -> PyResult { let pgid = - unistd::getpgid(Some(Pid::from_raw(pid as i32))).map_err(|e| e.into_pyexception(vm))?; + unistd::getpgid(Some(Pid::from_raw(pid as i32))).map_err(|e| e.to_pyexception(vm))?; Ok(vm.new_pyobj(pgid.as_raw())) } @@ -944,7 +944,7 @@ pub mod module { #[pyfunction] fn getsid(pid: u32, vm: &VirtualMachine) -> PyResult { let sid = - unistd::getsid(Some(Pid::from_raw(pid as i32))).map_err(|e| e.into_pyexception(vm))?; + unistd::getsid(Some(Pid::from_raw(pid as i32))).map_err(|e| e.to_pyexception(vm))?; Ok(vm.new_pyobj(sid.as_raw())) } @@ -963,20 +963,20 @@ pub mod module { #[pyfunction] fn setgid(gid: Option, vm: &VirtualMachine) -> PyResult<()> { let gid = gid.ok_or_else(|| vm.new_os_error("Invalid argument".to_string()))?; - unistd::setgid(gid).map_err(|err| err.into_pyexception(vm)) + unistd::setgid(gid).map_err(|err| err.to_pyexception(vm)) } #[cfg(not(target_os = "redox"))] #[pyfunction] fn setegid(egid: Option, vm: &VirtualMachine) -> PyResult<()> { let egid = egid.ok_or_else(|| vm.new_os_error("Invalid argument".to_string()))?; - unistd::setegid(egid).map_err(|err| err.into_pyexception(vm)) + unistd::setegid(egid).map_err(|err| err.to_pyexception(vm)) } #[pyfunction] fn setpgid(pid: u32, pgid: u32, vm: &VirtualMachine) -> PyResult<()> { unistd::setpgid(Pid::from_raw(pid as i32), Pid::from_raw(pgid as i32)) - .map_err(|err| err.into_pyexception(vm)) + .map_err(|err| err.to_pyexception(vm)) } #[cfg(not(target_os = "redox"))] @@ -984,7 +984,7 @@ pub mod module { fn setsid(vm: &VirtualMachine) -> PyResult<()> { unistd::setsid() .map(|_ok| ()) - .map_err(|err| err.into_pyexception(vm)) + .map_err(|err| err.to_pyexception(vm)) } fn try_from_id(vm: &VirtualMachine, obj: PyObjectRef, typ_name: &str) -> PyResult> { @@ -1024,24 +1024,24 @@ pub mod module { #[pyfunction] fn setuid(uid: Option, vm: &VirtualMachine) -> PyResult<()> { let uid = uid.ok_or_else(|| vm.new_os_error("Invalid argument".to_string()))?; - unistd::setuid(uid).map_err(|err| err.into_pyexception(vm)) + unistd::setuid(uid).map_err(|err| err.to_pyexception(vm)) } #[cfg(not(target_os = "redox"))] #[pyfunction] fn seteuid(euid: Option, vm: &VirtualMachine) -> PyResult<()> { let euid = euid.ok_or_else(|| vm.new_os_error("Invalid argument".to_string()))?; - unistd::seteuid(euid).map_err(|err| err.into_pyexception(vm)) + unistd::seteuid(euid).map_err(|err| err.to_pyexception(vm)) } #[cfg(not(target_os = "redox"))] #[pyfunction] fn setreuid(ruid: Option, euid: Option, vm: &VirtualMachine) -> PyResult<()> { if let Some(ruid) = ruid { - unistd::setuid(ruid).map_err(|err| err.into_pyexception(vm))?; + unistd::setuid(ruid).map_err(|err| err.to_pyexception(vm))?; } if let Some(euid) = euid { - unistd::seteuid(euid).map_err(|err| err.into_pyexception(vm))?; + unistd::seteuid(euid).map_err(|err| err.to_pyexception(vm))?; } Ok(()) } @@ -1067,15 +1067,15 @@ pub mod module { unwrap_or_unchanged(euid), unwrap_or_unchanged(suid), ) - .map_err(|err| err.into_pyexception(vm)) + .map_err(|err| err.to_pyexception(vm)) } #[cfg(not(target_os = "redox"))] #[pyfunction] fn openpty(vm: &VirtualMachine) -> PyResult<(i32, i32)> { - let r = nix::pty::openpty(None, None).map_err(|err| err.into_pyexception(vm))?; + let r = nix::pty::openpty(None, None).map_err(|err| err.to_pyexception(vm))?; for fd in &[r.master, r.slave] { - super::raw_set_inheritable(*fd, false).map_err(|e| e.into_pyexception(vm))?; + super::raw_set_inheritable(*fd, false).map_err(|e| e.to_pyexception(vm))?; } Ok((r.master, r.slave)) } @@ -1098,7 +1098,7 @@ pub mod module { #[pyfunction] fn uname(vm: &VirtualMachine) -> PyResult<_os::UnameResult> { - let info = uname::uname().map_err(|err| err.into_pyexception(vm))?; + let info = uname::uname().map_err(|err| err.to_pyexception(vm))?; Ok(_os::UnameResult { sysname: info.sysname, nodename: info.nodename, @@ -1167,17 +1167,17 @@ pub mod module { unwrap_or_unchanged(egid), unwrap_or_unchanged(sgid), ) - .map_err(|err| err.into_pyexception(vm)) + .map_err(|err| err.to_pyexception(vm)) } #[cfg(not(target_os = "redox"))] #[pyfunction] fn setregid(rgid: Option, egid: Option, vm: &VirtualMachine) -> PyResult<()> { if let Some(rgid) = rgid { - unistd::setgid(rgid).map_err(|err| err.into_pyexception(vm))?; + unistd::setgid(rgid).map_err(|err| err.to_pyexception(vm))?; } if let Some(egid) = egid { - unistd::setegid(egid).map_err(|err| err.into_pyexception(vm))?; + unistd::setegid(egid).map_err(|err| err.to_pyexception(vm))?; } Ok(()) } @@ -1193,7 +1193,7 @@ pub mod module { fn initgroups(user_name: PyStrRef, gid: Option, vm: &VirtualMachine) -> PyResult<()> { let user = CString::new(user_name.as_str()).unwrap(); let gid = gid.ok_or_else(|| vm.new_os_error("Invalid argument".to_string()))?; - unistd::initgroups(&user, gid).map_err(|err| err.into_pyexception(vm)) + unistd::initgroups(&user, gid).map_err(|err| err.to_pyexception(vm)) } // cfg from nix @@ -1208,7 +1208,7 @@ pub mod module { .collect::>, _>>()? .ok_or_else(|| vm.new_os_error("Invalid argument".to_string()))?; let ret = unistd::setgroups(&gids); - ret.map_err(|err| err.into_pyexception(vm)) + ret.map_err(|err| err.to_pyexception(vm)) } #[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "macos"))] @@ -1343,7 +1343,7 @@ pub mod module { if ret != 0 { return Err(IOErrorBuilder::new(std::io::Error::from_raw_os_error(ret)) .filename(self.path) - .into_pyexception(vm)); + .to_pyexception(vm)); } } } @@ -1417,7 +1417,7 @@ pub mod module { } else { Err(IOErrorBuilder::new(std::io::Error::from_raw_os_error(ret)) .filename(self.path) - .into_pyexception(vm)) + .to_pyexception(vm)) } } } @@ -1462,7 +1462,7 @@ pub mod module { fn waitpid(pid: libc::pid_t, opt: i32, vm: &VirtualMachine) -> PyResult<(libc::pid_t, i32)> { let mut status = 0; let pid = unsafe { libc::waitpid(pid, &mut status, opt) }; - let pid = nix::Error::result(pid).map_err(|err| err.into_pyexception(vm))?; + let pid = nix::Error::result(pid).map_err(|err| err.to_pyexception(vm))?; Ok((pid, status)) } #[pyfunction] @@ -1496,7 +1496,7 @@ pub mod module { ws_ypixel: 0, }; unsafe { winsz(fd.unwrap_or(libc::STDOUT_FILENO), &mut w) } - .map_err(|err| err.into_pyexception(vm))?; + .map_err(|err| err.to_pyexception(vm))?; (w.ws_col.into(), w.ws_row.into()) }; Ok(_os::PyTerminalSize { columns, lines }) @@ -1527,12 +1527,12 @@ pub mod module { #[pyfunction] fn dup(fd: i32, vm: &VirtualMachine) -> PyResult { - let fd = nix::unistd::dup(fd).map_err(|e| e.into_pyexception(vm))?; + let fd = nix::unistd::dup(fd).map_err(|e| e.to_pyexception(vm))?; super::raw_set_inheritable(fd, false) .map(|()| fd) .map_err(|e| { let _ = nix::unistd::close(fd); - e.into_pyexception(vm) + e.to_pyexception(vm) }) } @@ -1548,11 +1548,11 @@ pub mod module { #[pyfunction] fn dup2(args: Dup2Args, vm: &VirtualMachine) -> PyResult { - let fd = nix::unistd::dup2(args.fd, args.fd2).map_err(|e| e.into_pyexception(vm))?; + let fd = nix::unistd::dup2(args.fd, args.fd2).map_err(|e| e.to_pyexception(vm))?; if !args.inheritable { super::raw_set_inheritable(fd, false).map_err(|e| { let _ = nix::unistd::close(fd); - e.into_pyexception(vm) + e.to_pyexception(vm) })? } Ok(fd) @@ -1614,7 +1614,7 @@ pub mod module { fn getgrouplist(user: PyStrRef, group: u32, vm: &VirtualMachine) -> PyResult> { let user = CString::new(user.as_str()).unwrap(); let gid = Gid::from_raw(group); - let group_ids = unistd::getgrouplist(&user, gid).map_err(|err| err.into_pyexception(vm))?; + let group_ids = unistd::getgrouplist(&user, gid).map_err(|err| err.to_pyexception(vm))?; Ok(group_ids .into_iter() .map(|gid| vm.new_pyobj(gid.as_raw())) @@ -1878,7 +1878,7 @@ pub mod module { if errno::errno() == 0 { Ok(None) } else { - Err(io::Error::from(Errno::last()).into_pyexception(vm)) + Err(io::Error::from(Errno::last()).to_pyexception(vm)) } } else { Ok(Some(raw)) @@ -1921,7 +1921,7 @@ pub mod module { Some(&mut file_offset), args.count as usize, ) - .map_err(|err| err.into_pyexception(vm))?; + .map_err(|err| err.to_pyexception(vm))?; Ok(vm.ctx.new_int(res as u64).into()) } @@ -1974,7 +1974,7 @@ pub mod module { headers, trailers, ); - res.map_err(|err| err.into_pyexception(vm))?; + res.map_err(|err| err.to_pyexception(vm))?; Ok(vm.ctx.new_int(written as u64).into()) } diff --git a/vm/src/stdlib/pwd.rs b/vm/src/stdlib/pwd.rs index be32213fda..e51950296a 100644 --- a/vm/src/stdlib/pwd.rs +++ b/vm/src/stdlib/pwd.rs @@ -4,7 +4,7 @@ pub(crate) use pwd::make_module; mod pwd { use crate::{ builtins::{PyIntRef, PyStrRef}, - function::{IntoPyException, IntoPyObject}, + function::{ToPyException, ToPyObject}, AsPyObject, PyObjectRef, PyResult, PyStructSequence, VirtualMachine, }; use nix::unistd::{self, User}; @@ -51,7 +51,7 @@ mod pwd { #[pyfunction] fn getpwnam(name: PyStrRef, vm: &VirtualMachine) -> PyResult { - match User::from_name(name.as_str()).map_err(|err| err.into_pyexception(vm))? { + match User::from_name(name.as_str()).map_err(|err| err.to_pyexception(vm))? { Some(user) => Ok(Passwd::from(user)), None => { let name_repr = name.as_object().repr(vm)?; @@ -68,7 +68,7 @@ mod pwd { fn getpwuid(uid: PyIntRef, vm: &VirtualMachine) -> PyResult { let uid_t = libc::uid_t::try_from(uid.as_bigint()).map(unistd::Uid::from_raw); let user = match uid_t { - Ok(uid) => User::from_uid(uid).map_err(|err| err.into_pyexception(vm))?, + Ok(uid) => User::from_uid(uid).map_err(|err| err.to_pyexception(vm))?, Err(_) => None, }; match user { @@ -94,7 +94,7 @@ mod pwd { unsafe { libc::setpwent() }; while let Some(ptr) = NonNull::new(unsafe { libc::getpwent() }) { let user = User::from(unsafe { ptr.as_ref() }); - let passwd = Passwd::from(user).into_pyobject(vm); + let passwd = Passwd::from(user).to_pyobject(vm); list.push(passwd); } unsafe { libc::endpwent() }; diff --git a/vm/src/stdlib/signal.rs b/vm/src/stdlib/signal.rs index e4798f8be5..a55a92a1b2 100644 --- a/vm/src/stdlib/signal.rs +++ b/vm/src/stdlib/signal.rs @@ -11,7 +11,7 @@ pub(crate) fn make_module(vm: &VirtualMachine) -> PyObjectRef { #[pymodule] pub(crate) mod _signal { use crate::{ - convert::TryFromBorrowedObject, function::IntoPyException, signal, PyObjectRef, PyResult, + convert::TryFromBorrowedObject, function::ToPyException, signal, PyObjectRef, PyResult, VirtualMachine, }; use std::sync::atomic::{self, Ordering}; @@ -216,7 +216,7 @@ pub(crate) mod _signal { let err = std::io::Error::last_os_error(); // if getsockopt failed for some other reason, throw if err.raw_os_error() != Some(winsock2::WSAENOTSOCK) { - return Err(err.into_pyexception(vm)); + return Err(err.to_pyexception(vm)); } } is_socket @@ -226,7 +226,7 @@ pub(crate) mod _signal { #[cfg(not(windows))] if fd != INVALID_WAKEUP { use nix::fcntl; - let oflags = fcntl::fcntl(fd, fcntl::F_GETFL).map_err(|e| e.into_pyexception(vm))?; + let oflags = fcntl::fcntl(fd, fcntl::F_GETFL).map_err(|e| e.to_pyexception(vm))?; let nonblock = fcntl::OFlag::from_bits_truncate(oflags).contains(fcntl::OFlag::O_NONBLOCK); if !nonblock { diff --git a/vm/src/stdlib/sre.rs b/vm/src/stdlib/sre.rs index 9c41cfcb28..a44d82ea1c 100644 --- a/vm/src/stdlib/sre.rs +++ b/vm/src/stdlib/sre.rs @@ -8,7 +8,7 @@ mod _sre { PyTupleRef, PyTypeRef, }, common::{ascii, hash::PyHash}, - function::{ArgCallable, IntoPyObject, OptionalArg, PosArgs, PyComparisonValue}, + function::{ArgCallable, OptionalArg, PosArgs, PyComparisonValue, ToPyObject}, protocol::{PyBuffer, PyMappingMethods}, stdlib::sys, types::{AsMapping, Comparable, Hashable}, @@ -492,11 +492,7 @@ mod _sre { }; let ret = vm.call_method(&join_type, "join", (list,))?; - Ok(if subn { - (ret, n).into_pyobject(vm) - } else { - ret - }) + Ok(if subn { (ret, n).to_pyobject(vm) } else { ret }) }) } @@ -613,7 +609,7 @@ mod _sre { #[pyproperty] fn regs(&self, vm: &VirtualMachine) -> PyTupleRef { PyTuple::new_ref( - self.regs.iter().map(|&x| x.into_pyobject(vm)).collect(), + self.regs.iter().map(|&x| x.to_pyobject(vm)).collect(), &vm.ctx, ) } @@ -652,7 +648,7 @@ mod _sre { .with_str_drive(self.string.clone(), vm, |str_drive| { let args = args.into_vec(); if args.is_empty() { - return Ok(self.get_slice(0, str_drive, vm).unwrap().into_pyobject(vm)); + return Ok(self.get_slice(0, str_drive, vm).unwrap().to_pyobject(vm)); } let mut v: Vec = args .into_iter() @@ -661,7 +657,7 @@ mod _sre { .ok_or_else(|| vm.new_index_error("no such group".to_owned())) .map(|index| { self.get_slice(index, str_drive, vm) - .map(|x| x.into_pyobject(vm)) + .map(|x| x.to_pyobject(vm)) .unwrap_or_else(|| vm.ctx.none()) }) }) @@ -702,7 +698,7 @@ mod _sre { let v: Vec = (1..self.regs.len()) .map(|i| { self.get_slice(i, str_drive, vm) - .map(|s| s.into_pyobject(vm)) + .map(|s| s.to_pyobject(vm)) .unwrap_or_else(|| default.clone()) }) .collect(); @@ -726,7 +722,7 @@ mod _sre { let value = self .get_index(index, vm) .and_then(|x| self.get_slice(x, str_drive, vm)) - .map(|x| x.into_pyobject(vm)) + .map(|x| x.to_pyobject(vm)) .unwrap_or_else(|| default.clone()); dict.set_item(key, value, vm)?; } @@ -789,7 +785,7 @@ mod _sre { subscript: Some(|mapping, needle, vm| { Self::mapping_downcast(mapping) .getitem(needle.to_owned(), vm) - .map(|x| x.into_pyobject(vm)) + .map(|x| x.to_pyobject(vm)) }), ass_subscript: None, }; diff --git a/vm/src/stdlib/string.rs b/vm/src/stdlib/string.rs index 94425b8f9b..9d03e9c58c 100644 --- a/vm/src/stdlib/string.rs +++ b/vm/src/stdlib/string.rs @@ -9,8 +9,8 @@ mod _string { use crate::{ builtins::{PyList, PyStrRef}, format::{FieldName, FieldNamePart, FieldType, FormatPart, FormatString, FromTemplate}, - function::IntoPyException, - function::IntoPyObject, + function::ToPyException, + function::ToPyObject, PyObjectRef, PyResult, VirtualMachine, }; use std::mem; @@ -28,13 +28,13 @@ mod _string { format_spec, preconversion_spec.map(|c| c.to_string()), ); - tuple.into_pyobject(vm) + tuple.to_pyobject(vm) } #[pyfunction] fn formatter_parser(text: PyStrRef, vm: &VirtualMachine) -> PyResult { let format_string = - FormatString::from_str(text.as_str()).map_err(|e| e.into_pyexception(vm))?; + FormatString::from_str(text.as_str()).map_err(|e| e.to_pyexception(vm))?; let mut result = Vec::new(); let mut literal = String::new(); @@ -73,21 +73,21 @@ mod _string { text: PyStrRef, vm: &VirtualMachine, ) -> PyResult<(PyObjectRef, PyList)> { - let field_name = FieldName::parse(text.as_str()).map_err(|e| e.into_pyexception(vm))?; + let field_name = FieldName::parse(text.as_str()).map_err(|e| e.to_pyexception(vm))?; let first = match field_name.field_type { FieldType::Auto => vm.ctx.new_str(ascii!("")).into(), - FieldType::Index(index) => index.into_pyobject(vm), - FieldType::Keyword(attribute) => attribute.into_pyobject(vm), + FieldType::Index(index) => index.to_pyobject(vm), + FieldType::Keyword(attribute) => attribute.to_pyobject(vm), }; let rest = field_name .parts .iter() .map(|p| match p { - FieldNamePart::Attribute(attribute) => (true, attribute).into_pyobject(vm), - FieldNamePart::StringIndex(index) => (false, index).into_pyobject(vm), - FieldNamePart::Index(index) => (false, *index).into_pyobject(vm), + FieldNamePart::Attribute(attribute) => (true, attribute).to_pyobject(vm), + FieldNamePart::StringIndex(index) => (false, index).to_pyobject(vm), + FieldNamePart::Index(index) => (false, *index).to_pyobject(vm), }) .collect(); diff --git a/vm/src/stdlib/sys.rs b/vm/src/stdlib/sys.rs index a6d1bbffcf..d8373557a7 100644 --- a/vm/src/stdlib/sys.rs +++ b/vm/src/stdlib/sys.rs @@ -1,4 +1,4 @@ -use crate::{function::IntoPyObject, pyclass::PyClassImpl, PyObject, PyResult, VirtualMachine}; +use crate::{function::ToPyObject, pyclass::PyClassImpl, PyObject, PyResult, VirtualMachine}; pub(crate) use sys::{MAXSIZE, MULTIARCH}; @@ -689,7 +689,7 @@ pub(crate) fn init_module(vm: &VirtualMachine, module: &PyObject, builtins: &PyO .set_item("builtins", builtins.to_owned(), vm) .unwrap(); extend_module!(vm, module, { - "__doc__" => sys::DOC.to_owned().into_pyobject(vm), + "__doc__" => sys::DOC.to_owned().to_pyobject(vm), "modules" => modules, }); } diff --git a/vm/src/stdlib/sysconfigdata.rs b/vm/src/stdlib/sysconfigdata.rs index c3fb39b16c..d3bb64bd85 100644 --- a/vm/src/stdlib/sysconfigdata.rs +++ b/vm/src/stdlib/sysconfigdata.rs @@ -3,7 +3,7 @@ pub(crate) use _sysconfigdata::make_module; #[pymodule] pub(crate) mod _sysconfigdata { use crate::{ - builtins::PyDictRef, function::IntoPyObject, stdlib::sys::MULTIARCH, VirtualMachine, + builtins::PyDictRef, function::ToPyObject, stdlib::sys::MULTIARCH, VirtualMachine, }; #[pyattr] @@ -11,7 +11,7 @@ pub(crate) mod _sysconfigdata { let vars = vm.ctx.new_dict(); macro_rules! sysvars { ($($key:literal => $value:expr),*$(,)?) => {{ - $(vars.set_item($key, $value.into_pyobject(vm), vm).unwrap();)* + $(vars.set_item($key, $value.to_pyobject(vm), vm).unwrap();)* }}; } sysvars! { diff --git a/vm/src/stdlib/thread.rs b/vm/src/stdlib/thread.rs index 9e61284935..89b7c6abbb 100644 --- a/vm/src/stdlib/thread.rs +++ b/vm/src/stdlib/thread.rs @@ -6,7 +6,7 @@ pub(crate) use _thread::{make_module, RawRMutex}; pub(crate) mod _thread { use crate::{ builtins::{PyDictRef, PyStrRef, PyTupleRef, PyTypeRef}, - function::{ArgCallable, FuncArgs, IntoPyException, KwArgs, OptionalArg}, + function::{ArgCallable, FuncArgs, KwArgs, OptionalArg, ToPyException}, py_io, types::{Constructor, GetAttr, SetAttr}, utils::Either, @@ -246,7 +246,7 @@ pub(crate) mod _thread { vm.state.thread_count.fetch_add(1); thread_to_id(handle.thread()) }) - .map_err(|err| err.into_pyexception(vm)) + .map_err(|err| err.to_pyexception(vm)) } fn run_thread(func: ArgCallable, args: FuncArgs, vm: &VirtualMachine) { diff --git a/vm/src/stdlib/winapi.rs b/vm/src/stdlib/winapi.rs index aae0c76366..b1b72ed5a7 100644 --- a/vm/src/stdlib/winapi.rs +++ b/vm/src/stdlib/winapi.rs @@ -5,7 +5,7 @@ pub(crate) use _winapi::make_module; mod _winapi { use crate::{ builtins::PyStrRef, - function::{ArgMapping, ArgSequence, IntoPyException, OptionalArg}, + function::{ArgMapping, ArgSequence, OptionalArg, ToPyException}, stdlib::os::errno_err, PyObjectRef, PyResult, TryFromObject, VirtualMachine, }; @@ -197,7 +197,7 @@ mod _winapi { let wstr = |s: PyStrRef| { let ws = widestring::WideCString::from_str(s.as_str()) - .map_err(|err| err.into_pyexception(vm))?; + .map_err(|err| err.to_pyexception(vm))?; Ok(ws.into_vec_with_nul()) }; diff --git a/vm/src/stdlib/winreg.rs b/vm/src/stdlib/winreg.rs index a90d0707c7..37668083c8 100644 --- a/vm/src/stdlib/winreg.rs +++ b/vm/src/stdlib/winreg.rs @@ -29,7 +29,7 @@ pub(crate) fn make_module(vm: &VirtualMachine) -> PyObjectRef { mod winreg { use crate::common::lock::{PyRwLock, PyRwLockReadGuard, PyRwLockWriteGuard}; use crate::{ - builtins::PyStrRef, function::IntoPyException, PyObjectRef, PyRef, PyResult, PyValue, + builtins::PyStrRef, function::ToPyException, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, VirtualMachine, }; use ::winreg::{enums::RegType, RegKey, RegValue}; @@ -169,7 +169,7 @@ mod winreg { let sub_key = sub_key.as_ref().map_or("", |s| s.as_str()); let key = key .with_key(|k| k.open_subkey_with_flags(sub_key, access)) - .map_err(|e| e.into_pyexception(vm))?; + .map_err(|e| e.to_pyexception(vm))?; Ok(PyHkey::new(key)) } @@ -178,7 +178,7 @@ mod winreg { fn QueryValue(key: Hkey, subkey: Option, vm: &VirtualMachine) -> PyResult { let subkey = subkey.as_ref().map_or("", |s| s.as_str()); key.with_key(|k| k.get_value(subkey)) - .map_err(|e| e.into_pyexception(vm)) + .map_err(|e| e.to_pyexception(vm)) } #[pyfunction] @@ -189,7 +189,7 @@ mod winreg { ) -> PyResult<(PyObjectRef, usize)> { let subkey = subkey.as_ref().map_or("", |s| s.as_str()); key.with_key(|k| k.get_raw_value(subkey)) - .map_err(|e| e.into_pyexception(vm)) + .map_err(|e| e.to_pyexception(vm)) .and_then(|regval| { let ty = regval.vtype.clone() as usize; Ok((reg_to_py(regval, vm)?, ty)) @@ -204,7 +204,7 @@ mod winreg { winerror::ERROR_NO_MORE_ITEMS as i32, )) }) - .map_err(|e| e.into_pyexception(vm)) + .map_err(|e| e.to_pyexception(vm)) } #[pyfunction] @@ -219,7 +219,7 @@ mod winreg { winerror::ERROR_NO_MORE_ITEMS as i32, )) }) - .map_err(|e| e.into_pyexception(vm)) + .map_err(|e| e.to_pyexception(vm)) .and_then(|(name, value)| { let ty = value.vtype.clone() as usize; Ok((name, reg_to_py(value, vm)?, ty)) @@ -240,7 +240,7 @@ mod winreg { Some(subkey) => { let (k, _disp) = key .with_key(|k| k.create_subkey(subkey.as_str())) - .map_err(|e| e.into_pyexception(vm))?; + .map_err(|e| e.to_pyexception(vm))?; k } None => key.into_key(), @@ -261,13 +261,13 @@ mod winreg { } let subkey = subkey.as_ref().map_or("", |s| s.as_str()); key.with_key(|k| k.set_value(subkey, &OsStr::new(value.as_str()))) - .map_err(|e| e.into_pyexception(vm)) + .map_err(|e| e.to_pyexception(vm)) } #[pyfunction] fn DeleteKey(key: Hkey, subkey: PyStrRef, vm: &VirtualMachine) -> PyResult<()> { key.with_key(|k| k.delete_subkey(subkey.as_str())) - .map_err(|e| e.into_pyexception(vm)) + .map_err(|e| e.to_pyexception(vm)) } fn reg_to_py(value: RegValue, vm: &VirtualMachine) -> PyResult { diff --git a/vm/src/types/slot.rs b/vm/src/types/slot.rs index d578b16ab3..2d2c2d436f 100644 --- a/vm/src/types/slot.rs +++ b/vm/src/types/slot.rs @@ -2,7 +2,7 @@ pub use crate::builtins::object::{generic_getattr, generic_setattr}; use crate::common::{hash::PyHash, lock::PyRwLock}; use crate::{ builtins::{PyInt, PyStrRef, PyType, PyTypeRef}, - function::{FromArgs, FuncArgs, IntoPyObject, IntoPyResult, OptionalArg, PyComparisonValue}, + function::{FromArgs, FuncArgs, OptionalArg, PyComparisonValue, ToPyObject, ToPyResult}, protocol::{PyBuffer, PyIterReturn, PyMapping, PyMappingMethods}, protocol::{PySequence, PySequenceMethods}, utils::Either, @@ -228,7 +228,7 @@ fn as_sequence_wrapper(zelf: &PyObject, _vm: &VirtualMachine) -> Cow<'static, Py length_wrapper(seq.obj.to_owned(), vm) }), item: Some(|seq, i, vm| { - vm.call_special_method(seq.obj.to_owned(), "__getitem__", (i.into_pyobject(vm),)) + vm.call_special_method(seq.obj.to_owned(), "__getitem__", (i.to_pyobject(vm),)) }), ass_item: then_some_closure!( zelf.class().has_attr("__setitem__") | zelf.class().has_attr("__delitem__"), @@ -237,11 +237,11 @@ fn as_sequence_wrapper(zelf: &PyObject, _vm: &VirtualMachine) -> Cow<'static, Py .call_special_method( seq.obj.to_owned(), "__setitem__", - (i.into_pyobject(vm), value), + (i.to_pyobject(vm), value), ) .map(|_| Ok(()))?, None => vm - .call_special_method(seq.obj.to_owned(), "__delitem__", (i.into_pyobject(vm),)) + .call_special_method(seq.obj.to_owned(), "__delitem__", (i.to_pyobject(vm),)) .map(|_| Ok(()))?, } ), @@ -890,7 +890,7 @@ pub trait IterNext: PyValue + Iterable { #[inline] #[pymethod] fn __next__(zelf: PyObjectRef, vm: &VirtualMachine) -> PyResult { - Self::slot_iternext(&zelf, vm).into_pyresult(vm) + Self::slot_iternext(&zelf, vm).to_pyresult(vm) } } diff --git a/vm/src/utils.rs b/vm/src/utils.rs index da08aeb524..fcc87605ab 100644 --- a/vm/src/utils.rs +++ b/vm/src/utils.rs @@ -1,6 +1,6 @@ use crate::{ builtins::{PyFloat, PyStr}, - function::{IntoPyException, IntoPyObject}, + function::{ToPyException, ToPyObject}, AsPyObject, PyObject, PyObjectRef, PyObjectWrap, PyResult, TryFromObject, VirtualMachine, }; use num_traits::ToPrimitive; @@ -41,12 +41,12 @@ impl PyObjectWrap for Either { } } -impl IntoPyObject for Either { +impl ToPyObject for Either { #[inline(always)] - fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { + fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { match self { - Self::A(a) => a.into_pyobject(vm), - Self::B(b) => b.into_pyobject(vm), + Self::A(a) => a.to_pyobject(vm), + Self::B(b) => b.to_pyobject(vm), } } } @@ -120,8 +120,8 @@ impl TryFromObject for std::time::Duration { } } -impl IntoPyObject for std::convert::Infallible { - fn into_pyobject(self, _vm: &VirtualMachine) -> PyObjectRef { +impl ToPyObject for std::convert::Infallible { + fn to_pyobject(self, _vm: &VirtualMachine) -> PyObjectRef { match self {} } } @@ -132,13 +132,13 @@ pub trait ToCString { impl ToCString for &str { fn to_cstring(&self, vm: &VirtualMachine) -> PyResult { - std::ffi::CString::new(*self).map_err(|err| err.into_pyexception(vm)) + std::ffi::CString::new(*self).map_err(|err| err.to_pyexception(vm)) } } impl ToCString for PyStr { fn to_cstring(&self, vm: &VirtualMachine) -> PyResult { - std::ffi::CString::new(self.as_ref()).map_err(|err| err.into_pyexception(vm)) + std::ffi::CString::new(self.as_ref()).map_err(|err| err.to_pyexception(vm)) } } diff --git a/vm/src/vm.rs b/vm/src/vm.rs index 3cb6dfd2a2..7c10da4ac4 100644 --- a/vm/src/vm.rs +++ b/vm/src/vm.rs @@ -19,7 +19,7 @@ use crate::{ common::{ascii, hash::HashSecret, lock::PyMutex, rc::PyRc}, frame::{ExecutionResult, Frame, FrameRef}, frozen, - function::{ArgMapping, FuncArgs, IntoPyObject}, + function::{ArgMapping, FuncArgs, ToPyObject}, import, protocol::PyIterIter, pyobject::PyLease, @@ -621,7 +621,7 @@ impl VirtualMachine { (None, None) }; let from_list = match from_list { - Some(tup) => tup.into_pyobject(self), + Some(tup) => tup.to_pyobject(self), None => self.new_tuple(()).into(), }; self.invoke(&import_func, (module, globals, locals, from_list, level)) @@ -965,10 +965,7 @@ mod sealed { } /// A sealed marker trait for `DictKey` types that always become an exact instance of `str` -pub trait Internable: - sealed::SealedInternable + crate::dictdatatype::DictKey + IntoPyObject -{ -} +pub trait Internable: sealed::SealedInternable + crate::dictdatatype::DictKey + ToPyObject {} impl Internable for String {} diff --git a/vm/src/vm_new.rs b/vm/src/vm_new.rs index 8b907d44de..acf546eaba 100644 --- a/vm/src/vm_new.rs +++ b/vm/src/vm_new.rs @@ -7,7 +7,7 @@ use crate::{ tuple::{IntoPyTuple, PyTupleRef}, PyBaseException, PyBaseExceptionRef, PyDictRef, PyModule, PyStrRef, PyTypeRef, }, - function::IntoPyObject, + function::ToPyObject, scope::Scope, vm::VirtualMachine, AsPyObject, PyObject, PyObjectRef, PyObjectWrap, PyRef, @@ -16,8 +16,8 @@ use crate::{ /// Collection of object creation helpers impl VirtualMachine { /// Create a new python object - pub fn new_pyobj(&self, value: impl IntoPyObject) -> PyObjectRef { - value.into_pyobject(self) + pub fn new_pyobj(&self, value: impl ToPyObject) -> PyObjectRef { + value.to_pyobject(self) } pub fn new_tuple(&self, value: impl IntoPyTuple) -> PyTupleRef { @@ -222,7 +222,7 @@ impl VirtualMachine { .unwrap(); syntax_error .as_object() - .set_attr("text", error.statement.clone().into_pyobject(self), self) + .set_attr("text", error.statement.clone().to_pyobject(self), self) .unwrap(); syntax_error .as_object() diff --git a/wasm/lib/src/browser_module.rs b/wasm/lib/src/browser_module.rs index f619242486..2c460f54ca 100644 --- a/wasm/lib/src/browser_module.rs +++ b/wasm/lib/src/browser_module.rs @@ -8,7 +8,7 @@ mod _browser { use js_sys::Promise; use rustpython_vm::{ builtins::{PyDictRef, PyStrRef}, - function::{ArgCallable, IntoPyObject, OptionalArg}, + function::{ArgCallable, OptionalArg, ToPyObject}, import::import_file, pyclass::PyClassImpl, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine, @@ -174,7 +174,7 @@ mod _browser { .query_selector(query.as_str()) .map_err(|err| convert::js_py_typeerror(vm, err))? .map(|elem| Element { elem }) - .into_pyobject(vm); + .to_pyobject(vm); Ok(elem) } } diff --git a/wasm/lib/src/js_module.rs b/wasm/lib/src/js_module.rs index a3273c4f14..73f1ff9aef 100644 --- a/wasm/lib/src/js_module.rs +++ b/wasm/lib/src/js_module.rs @@ -14,7 +14,7 @@ mod _js { use js_sys::{Array, Object, Promise, Reflect}; use rustpython_vm::{ builtins::{PyBaseExceptionRef, PyFloat, PyStrRef, PyTypeRef}, - function::{ArgCallable, IntoPyObject, OptionalArg, OptionalOption, PosArgs}, + function::{ArgCallable, OptionalArg, OptionalOption, PosArgs, ToPyObject}, protocol::PyIterReturn, types::{IterNext, IterNextIterable}, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult, PyValue, TryFromObject, @@ -614,7 +614,7 @@ mod _js { fn new_js_error(vm: &VirtualMachine, err: JsValue) -> PyBaseExceptionRef { vm.new_exception( vm.class("_js", "JSError"), - vec![PyJsValue::new(err).into_pyobject(vm)], + vec![PyJsValue::new(err).to_pyobject(vm)], ) } } From a07fcd2b44fdb62c70418adef9b6612a7268e6ce Mon Sep 17 00:00:00 2001 From: Jeong Yunwon Date: Mon, 18 Apr 2022 05:13:23 +0900 Subject: [PATCH 3/8] split convert module into sub modules --- vm/src/convert/mod.rs | 5 ++++ vm/src/convert/transmute_from.rs | 29 ++++++++++++++++++++++ vm/src/{convert.rs => convert/try_from.rs} | 25 ------------------- 3 files changed, 34 insertions(+), 25 deletions(-) create mode 100644 vm/src/convert/mod.rs create mode 100644 vm/src/convert/transmute_from.rs rename vm/src/{convert.rs => convert/try_from.rs} (78%) diff --git a/vm/src/convert/mod.rs b/vm/src/convert/mod.rs new file mode 100644 index 0000000000..4c5aedc48a --- /dev/null +++ b/vm/src/convert/mod.rs @@ -0,0 +1,5 @@ +mod transmute_from; +mod try_from; + +pub use transmute_from::TransmuteFromObject; +pub use try_from::{TryFromBorrowedObject, TryFromObject}; \ No newline at end of file diff --git a/vm/src/convert/transmute_from.rs b/vm/src/convert/transmute_from.rs new file mode 100644 index 0000000000..e6c1eff7a9 --- /dev/null +++ b/vm/src/convert/transmute_from.rs @@ -0,0 +1,29 @@ +use crate::{ + pyobject::{AsPyObject, PyObject, PyRef, PyResult, PyValue}, + vm::VirtualMachine, +}; + +/// Marks a type that has the exact same layout as PyObjectRef, e.g. a type that is +/// `repr(transparent)` over PyObjectRef. +/// +/// # Safety +/// Can only be implemented for types that are `repr(transparent)` over a PyObjectRef `obj`, +/// and logically valid so long as `check(vm, obj)` returns `Ok(())` +pub unsafe trait TransmuteFromObject: Sized { + fn check(vm: &VirtualMachine, obj: &PyObject) -> PyResult<()>; +} + +unsafe impl TransmuteFromObject for PyRef { + fn check(vm: &VirtualMachine, obj: &PyObject) -> PyResult<()> { + let class = T::class(vm); + if obj.fast_isinstance(class) { + if obj.payload_is::() { + Ok(()) + } else { + Err(vm.new_downcast_runtime_error(class, obj)) + } + } else { + Err(vm.new_downcast_type_error(class, obj)) + } + } +} diff --git a/vm/src/convert.rs b/vm/src/convert/try_from.rs similarity index 78% rename from vm/src/convert.rs rename to vm/src/convert/try_from.rs index d8a9f19634..13b0f587f2 100644 --- a/vm/src/convert.rs +++ b/vm/src/convert/try_from.rs @@ -3,31 +3,6 @@ use crate::{ vm::VirtualMachine, }; -/// Marks a type that has the exact same layout as PyObjectRef, e.g. a type that is -/// `repr(transparent)` over PyObjectRef. -/// -/// # Safety -/// Can only be implemented for types that are `repr(transparent)` over a PyObjectRef `obj`, -/// and logically valid so long as `check(vm, obj)` returns `Ok(())` -pub unsafe trait TransmuteFromObject: Sized { - fn check(vm: &VirtualMachine, obj: &PyObject) -> PyResult<()>; -} - -unsafe impl TransmuteFromObject for PyRef { - fn check(vm: &VirtualMachine, obj: &PyObject) -> PyResult<()> { - let class = T::class(vm); - if obj.fast_isinstance(class) { - if obj.payload_is::() { - Ok(()) - } else { - Err(vm.new_downcast_runtime_error(class, obj)) - } - } else { - Err(vm.new_downcast_type_error(class, obj)) - } - } -} - /// Implemented by any type that can be created from a Python object. /// /// Any type that implements `TryFromObject` is automatically `FromArgs`, and From e356d51af12f49924bf6871e2ac417d2a2d29d70 Mon Sep 17 00:00:00 2001 From: Jeong Yunwon Date: Mon, 18 Apr 2022 05:36:38 +0900 Subject: [PATCH 4/8] move ToPy* to convert --- derive/src/pystructseq.rs | 4 ++-- stdlib/src/array.rs | 8 +++----- stdlib/src/json.rs | 3 ++- stdlib/src/posixsubprocess.rs | 2 +- stdlib/src/re.rs | 27 ++++++++++++++++----------- stdlib/src/resource.rs | 2 +- stdlib/src/scproxy.rs | 2 +- stdlib/src/select.rs | 15 +++++---------- stdlib/src/socket.rs | 9 +++------ stdlib/src/ssl.rs | 4 ++-- stdlib/src/termios.rs | 2 +- vm/src/buffer.rs | 3 ++- vm/src/builtins/bytearray.rs | 2 +- vm/src/builtins/bytes.rs | 6 ++---- vm/src/builtins/complex.rs | 3 ++- vm/src/builtins/dict.rs | 2 +- vm/src/builtins/enumerate.rs | 3 ++- vm/src/builtins/float.rs | 3 ++- vm/src/builtins/function.rs | 2 +- vm/src/builtins/function/jitfunc.rs | 3 ++- vm/src/builtins/genericalias.rs | 3 ++- vm/src/builtins/getset.rs | 3 ++- vm/src/builtins/int.rs | 6 ++---- vm/src/builtins/list.rs | 3 ++- vm/src/builtins/mappingproxy.rs | 3 ++- vm/src/builtins/memory.rs | 3 ++- vm/src/builtins/module.rs | 6 ++---- vm/src/builtins/pybool.rs | 4 +--- vm/src/builtins/pystr.rs | 6 ++---- vm/src/builtins/pyunion.rs | 3 ++- vm/src/builtins/singletons.rs | 2 +- vm/src/builtins/slice.rs | 3 ++- vm/src/builtins/tuple.rs | 4 ++-- vm/src/codecs.rs | 2 +- vm/src/convert/mod.rs | 4 +++- vm/src/convert/to_pyobject.rs | 18 ++++++++++++++++++ vm/src/dictdatatype.rs | 2 +- vm/src/exceptions.rs | 6 ++++-- vm/src/format.rs | 3 ++- vm/src/frame.rs | 3 ++- vm/src/function.rs | 18 +----------------- vm/src/function/argument.rs | 6 +++--- vm/src/function/arithmetic.rs | 3 +-- vm/src/protocol/iter.rs | 2 +- vm/src/protocol/mapping.rs | 2 +- vm/src/protocol/object.rs | 3 ++- vm/src/protocol/sequence.rs | 3 ++- vm/src/pyobject.rs | 3 ++- vm/src/scope.rs | 3 ++- vm/src/stdlib/io.rs | 9 ++++----- vm/src/stdlib/itertools.rs | 3 ++- vm/src/stdlib/marshal.rs | 3 ++- vm/src/stdlib/nt.rs | 3 ++- vm/src/stdlib/os.rs | 6 ++++-- vm/src/stdlib/posix.rs | 5 +++-- vm/src/stdlib/pwd.rs | 2 +- vm/src/stdlib/signal.rs | 2 +- vm/src/stdlib/sre.rs | 3 ++- vm/src/stdlib/string.rs | 4 ++-- vm/src/stdlib/sys.rs | 2 +- vm/src/stdlib/sysconfigdata.rs | 4 +--- vm/src/stdlib/thread.rs | 3 ++- vm/src/stdlib/winapi.rs | 3 ++- vm/src/stdlib/winreg.rs | 2 +- vm/src/types/slot.rs | 8 +++++--- vm/src/utils.rs | 2 +- vm/src/vm.rs | 3 ++- vm/src/vm_new.rs | 2 +- wasm/lib/src/browser_module.rs | 3 ++- wasm/lib/src/js_module.rs | 3 ++- 70 files changed, 165 insertions(+), 145 deletions(-) create mode 100644 vm/src/convert/to_pyobject.rs diff --git a/derive/src/pystructseq.rs b/derive/src/pystructseq.rs index 6ec2352cfb..08cbe69808 100644 --- a/derive/src/pystructseq.rs +++ b/derive/src/pystructseq.rs @@ -29,14 +29,14 @@ pub(crate) fn impl_pystruct_sequence(input: DeriveInput) -> Result impl ::rustpython_vm::PyStructSequence for #ty { const FIELD_NAMES: &'static [&'static str] = &[#(stringify!(#field_names)),*]; fn into_tuple(self, vm: &::rustpython_vm::VirtualMachine) -> ::rustpython_vm::builtins::PyTuple { - let items = vec![#(::rustpython_vm::function::ToPyObject::to_pyobject( + let items = vec![#(::rustpython_vm::convert::ToPyObject::to_pyobject( self.#field_names, vm, )),*]; ::rustpython_vm::builtins::PyTuple::new_unchecked(items.into_boxed_slice()) } } - impl ::rustpython_vm::function::ToPyObject for #ty { + impl ::rustpython_vm::convert::ToPyObject for #ty { fn to_pyobject(self, vm: &::rustpython_vm::VirtualMachine) -> ::rustpython_vm::PyObjectRef { ::rustpython_vm::PyStructSequence::into_struct_sequence(self, vm).into() } diff --git a/stdlib/src/array.rs b/stdlib/src/array.rs index 243fbbaace..3a69bf0816 100644 --- a/stdlib/src/array.rs +++ b/stdlib/src/array.rs @@ -17,10 +17,8 @@ mod array { PyListRef, PyStr, PyStrRef, PyTupleRef, PyTypeRef, }, class_or_notimplemented, - function::{ - ArgBytesLike, ArgIntoFloat, ArgIterable, OptionalArg, PyComparisonValue, - ToPyObject, ToPyResult, - }, + convert::{ToPyObject, ToPyResult, TryFromBorrowedObject, TryFromObject}, + function::{ArgBytesLike, ArgIntoFloat, ArgIterable, OptionalArg, PyComparisonValue}, protocol::{ BufferDescriptor, BufferMethods, BufferResizeGuard, PyBuffer, PyIterReturn, PyMappingMethods, @@ -34,7 +32,7 @@ mod array { PyComparisonOp, }, AsPyObject, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, - TryFromBorrowedObject, TryFromObject, VirtualMachine, + VirtualMachine, }, }; use itertools::Itertools; diff --git a/stdlib/src/json.rs b/stdlib/src/json.rs index 658fd4edae..bb663eb93e 100644 --- a/stdlib/src/json.rs +++ b/stdlib/src/json.rs @@ -6,7 +6,8 @@ mod _json { use super::machinery; use crate::vm::{ builtins::{PyBaseExceptionRef, PyStrRef, PyTypeRef}, - function::{OptionalArg, ToPyObject, ToPyResult}, + convert::{ToPyObject, ToPyResult}, + function::OptionalArg, protocol::PyIterReturn, types::{Callable, Constructor}, AsPyObject, PyObjectRef, PyObjectView, PyResult, PyValue, VirtualMachine, diff --git a/stdlib/src/posixsubprocess.rs b/stdlib/src/posixsubprocess.rs index 563b978487..5f753c0061 100644 --- a/stdlib/src/posixsubprocess.rs +++ b/stdlib/src/posixsubprocess.rs @@ -19,7 +19,7 @@ pub(crate) use _posixsubprocess::make_module; #[pymodule] mod _posixsubprocess { use super::{exec, CStrPathLike, ForkExecArgs, ProcArgs}; - use crate::vm::{function::ToPyException, PyResult, VirtualMachine}; + use crate::vm::{convert::ToPyException, PyResult, VirtualMachine}; #[pyfunction] fn fork_exec(args: ForkExecArgs, vm: &VirtualMachine) -> PyResult { diff --git a/stdlib/src/re.rs b/stdlib/src/re.rs index 7b341b596e..9c4dbb5d69 100644 --- a/stdlib/src/re.rs +++ b/stdlib/src/re.rs @@ -10,8 +10,9 @@ mod re { */ use crate::vm::{ builtins::{PyInt, PyIntRef, PyStr, PyStrRef}, - function::{ToPyObject, OptionalArg, PosArgs}, - match_class, PyObjectRef, PyResult, PyValue, TryFromObject, VirtualMachine, + convert::{ToPyObject, TryFromObject}, + function::{OptionalArg, PosArgs}, + match_class, PyObjectRef, PyResult, PyValue, VirtualMachine, }; use num_traits::Signed; use regex::bytes::{Captures, Regex, RegexBuilder}; @@ -252,9 +253,11 @@ mod re { let split = output .into_iter() .map(|v| { - vm.unwrap_or_none( - v.map(|v| vm.ctx.new_str(String::from_utf8_lossy(v).into_owned()).into()), - ) + vm.unwrap_or_none(v.map(|v| { + vm.ctx + .new_str(String::from_utf8_lossy(v).into_owned()) + .into() + })) }) .collect(); Ok(vm.ctx.new_list(split).into()) @@ -369,18 +372,20 @@ mod re { #[pymethod] fn start(&self, group: OptionalArg, vm: &VirtualMachine) -> PyResult { let group = group.unwrap_or_else(|| vm.ctx.new_int(0).into()); - let start = self - .get_bounds(group, vm)? - .map_or_else(|| vm.ctx.new_int(-1).into(), |r| vm.ctx.new_int(r.start).into()); + let start = self.get_bounds(group, vm)?.map_or_else( + || vm.ctx.new_int(-1).into(), + |r| vm.ctx.new_int(r.start).into(), + ); Ok(start) } #[pymethod] fn end(&self, group: OptionalArg, vm: &VirtualMachine) -> PyResult { let group = group.unwrap_or_else(|| vm.ctx.new_int(0).into()); - let end = self - .get_bounds(group, vm)? - .map_or_else(|| vm.ctx.new_int(-1).into(), |r| vm.ctx.new_int(r.end).into()); + let end = self.get_bounds(group, vm)?.map_or_else( + || vm.ctx.new_int(-1).into(), + |r| vm.ctx.new_int(r.end).into(), + ); Ok(end) } diff --git a/stdlib/src/resource.rs b/stdlib/src/resource.rs index a3abbe223f..046d86e5f3 100644 --- a/stdlib/src/resource.rs +++ b/stdlib/src/resource.rs @@ -3,7 +3,7 @@ pub(crate) use resource::make_module; #[pymodule] mod resource { use crate::vm::{ - function::{ToPyException, ToPyObject}, + convert::{ToPyException, ToPyObject}, stdlib::os, PyObject, PyObjectRef, PyResult, PyStructSequence, TryFromBorrowedObject, VirtualMachine, }; diff --git a/stdlib/src/scproxy.rs b/stdlib/src/scproxy.rs index 3b9bbf67d8..e1e0fce736 100644 --- a/stdlib/src/scproxy.rs +++ b/stdlib/src/scproxy.rs @@ -6,7 +6,7 @@ mod _scproxy { use crate::vm::{ builtins::{PyDictRef, PyStr}, - function::ToPyObject, + convert::ToPyObject, PyResult, VirtualMachine, }; use system_configuration::core_foundation::{ diff --git a/stdlib/src/select.rs b/stdlib/src/select.rs index ba24e45c91..360f0d6d4b 100644 --- a/stdlib/src/select.rs +++ b/stdlib/src/select.rs @@ -154,12 +154,10 @@ fn sec_to_timeval(sec: f64) -> timeval { mod decl { use super::*; use crate::vm::{ - builtins::PyTypeRef, - function::{OptionalOption, ToPyException}, - stdlib::time, - utils::Either, - PyObjectRef, PyResult, VirtualMachine, + builtins::PyTypeRef, convert::ToPyException, function::OptionalOption, stdlib::time, + utils::Either, PyObjectRef, PyResult, VirtualMachine, }; + #[pyattr] fn error(vm: &VirtualMachine) -> PyTypeRef { vm.ctx.exceptions.os_error.clone() @@ -262,11 +260,8 @@ mod decl { pub(super) mod poll { use super::*; use crate::vm::{ - builtins::PyFloat, - common::lock::PyMutex, - function::{OptionalArg, ToPyObject}, - stdlib::io::Fildes, - AsPyObject, PyValue, + builtins::PyFloat, common::lock::PyMutex, convert::ToPyObject, function::OptionalArg, + stdlib::io::Fildes, AsPyObject, PyValue, }; use libc::pollfd; use num_traits::ToPrimitive; diff --git a/stdlib/src/socket.rs b/stdlib/src/socket.rs index bb1b6a2538..c8b3f873c0 100644 --- a/stdlib/src/socket.rs +++ b/stdlib/src/socket.rs @@ -13,13 +13,10 @@ mod _socket { use crate::common::lock::{PyMappedRwLockReadGuard, PyRwLock, PyRwLockReadGuard}; use crate::vm::{ builtins::{PyBaseExceptionRef, PyListRef, PyStrRef, PyTupleRef, PyTypeRef}, - function::{ - ArgBytesLike, ArgMemoryBuffer, FuncArgs, OptionalArg, OptionalOption, ToPyException, - ToPyObject, - }, + convert::{ToPyException, ToPyObject, TryFromBorrowedObject, TryFromObject}, + function::{ArgBytesLike, ArgMemoryBuffer, FuncArgs, OptionalArg, OptionalOption}, utils::{Either, ToCString}, - AsPyObject, PyObjectRef, PyResult, PyValue, TryFromBorrowedObject, TryFromObject, - VirtualMachine, + AsPyObject, PyObjectRef, PyResult, PyValue, VirtualMachine, }; use crossbeam_utils::atomic::AtomicCell; use num_traits::ToPrimitive; diff --git a/stdlib/src/ssl.rs b/stdlib/src/ssl.rs index 14c527f7d6..bac1b8bfde 100644 --- a/stdlib/src/ssl.rs +++ b/stdlib/src/ssl.rs @@ -31,10 +31,10 @@ mod _ssl { socket::{self, PySocket}, vm::{ builtins::{PyBaseExceptionRef, PyStrRef, PyType, PyTypeRef}, + convert::{ToPyException, ToPyObject}, exceptions, function::{ ArgBytesLike, ArgCallable, ArgMemoryBuffer, ArgStrOrBytesLike, OptionalArg, - ToPyException, ToPyObject, }, stdlib::os::PyPathLike, types::Constructor, @@ -1399,7 +1399,7 @@ mod windows { common::ascii, vm::{ builtins::{PyFrozenSet, PyStrRef}, - function::ToPyException, + convert::ToPyException, PyObjectRef, PyResult, PyValue, VirtualMachine, }, }; diff --git a/stdlib/src/termios.rs b/stdlib/src/termios.rs index b1673479ec..173df78718 100644 --- a/stdlib/src/termios.rs +++ b/stdlib/src/termios.rs @@ -4,7 +4,7 @@ pub(crate) use self::termios::make_module; mod termios { use crate::vm::{ builtins::{PyBaseExceptionRef, PyBytes, PyInt, PyListRef, PyTypeRef}, - function::ToPyObject, + convert::ToPyObject, PyObjectRef, PyResult, TryFromObject, VirtualMachine, }; use termios::Termios; diff --git a/vm/src/buffer.rs b/vm/src/buffer.rs index 4a80bb0139..a97c75ab67 100644 --- a/vm/src/buffer.rs +++ b/vm/src/buffer.rs @@ -1,7 +1,8 @@ use crate::{ builtins::{float, PyBaseExceptionRef, PyBytesRef, PyTuple, PyTupleRef, PyTypeRef}, common::{static_cell, str::wchar_t}, - function::{ArgBytesLike, ArgIntoBool, ToPyObject}, + convert::ToPyObject, + function::{ArgBytesLike, ArgIntoBool}, PyObjectRef, PyResult, TryFromObject, VirtualMachine, }; use half::f16; diff --git a/vm/src/builtins/bytearray.rs b/vm/src/builtins/bytearray.rs index a297b83b11..92e9ba80da 100644 --- a/vm/src/builtins/bytearray.rs +++ b/vm/src/builtins/bytearray.rs @@ -18,9 +18,9 @@ use crate::{ PyRwLockReadGuard, PyRwLockWriteGuard, }, }, + convert::ToPyObject, function::{ ArgBytesLike, ArgIterable, FuncArgs, OptionalArg, OptionalOption, PyComparisonValue, - ToPyObject, }, protocol::{ BufferDescriptor, BufferMethods, BufferResizeGuard, PyBuffer, PyIterReturn, diff --git a/vm/src/builtins/bytes.rs b/vm/src/builtins/bytes.rs index 6c3329246c..80467bea79 100644 --- a/vm/src/builtins/bytes.rs +++ b/vm/src/builtins/bytes.rs @@ -7,10 +7,8 @@ use crate::{ ByteInnerSplitOptions, ByteInnerTranslateOptions, DecodeArgs, PyBytesInner, }, common::{hash::PyHash, lock::PyMutex}, - function::{ - ArgBytesLike, ArgIterable, OptionalArg, OptionalOption, PyComparisonValue, ToPyObject, - ToPyResult, - }, + convert::{ToPyObject, ToPyResult}, + function::{ArgBytesLike, ArgIterable, OptionalArg, OptionalOption, PyComparisonValue}, protocol::{ BufferDescriptor, BufferMethods, PyBuffer, PyIterReturn, PyMappingMethods, PySequenceMethods, diff --git a/vm/src/builtins/complex.rs b/vm/src/builtins/complex.rs index f0767808ec..c49684edf6 100644 --- a/vm/src/builtins/complex.rs +++ b/vm/src/builtins/complex.rs @@ -1,9 +1,10 @@ use super::{float, PyStr, PyTypeRef}; use crate::{ + convert::ToPyObject, function::{ OptionalArg, OptionalOption, PyArithmeticValue::{self, *}, - PyComparisonValue, ToPyObject, + PyComparisonValue, }, pyclass::PyClassImpl, types::{Comparable, Constructor, Hashable, PyComparisonOp}, diff --git a/vm/src/builtins/dict.rs b/vm/src/builtins/dict.rs index c4f79d67d5..347983d5c1 100644 --- a/vm/src/builtins/dict.rs +++ b/vm/src/builtins/dict.rs @@ -9,10 +9,10 @@ use crate::{ PyTuple, }, common::ascii, + convert::ToPyObject, dictdatatype::{self, DictKey}, function::{ ArgIterable, FuncArgs, KwArgs, OptionalArg, PyArithmeticValue::*, PyComparisonValue, - ToPyObject, }, protocol::{PyIterIter, PyIterReturn, PyMappingMethods, PySequenceMethods}, pyclass::{PyClassDef, PyClassImpl}, diff --git a/vm/src/builtins/enumerate.rs b/vm/src/builtins/enumerate.rs index 76ac172a78..f9e2abe53b 100644 --- a/vm/src/builtins/enumerate.rs +++ b/vm/src/builtins/enumerate.rs @@ -1,7 +1,8 @@ use super::{IterStatus, PositionIterInternal, PyGenericAlias, PyIntRef, PyTupleRef, PyTypeRef}; use crate::common::lock::{PyMutex, PyRwLock}; use crate::{ - function::{OptionalArg, ToPyObject}, + convert::ToPyObject, + function::OptionalArg, protocol::{PyIter, PyIterReturn}, pyclass::PyClassImpl, types::{Constructor, IterNext, IterNextIterable}, diff --git a/vm/src/builtins/float.rs b/vm/src/builtins/float.rs index c312cd9104..34785412b9 100644 --- a/vm/src/builtins/float.rs +++ b/vm/src/builtins/float.rs @@ -1,11 +1,12 @@ use super::{try_bigint_to_f64, PyByteArray, PyBytes, PyInt, PyIntRef, PyStr, PyStrRef, PyTypeRef}; use crate::common::{float_ops, hash}; use crate::{ + convert::ToPyObject, format::FormatSpec, function::{ ArgBytesLike, OptionalArg, OptionalOption, PyArithmeticValue::{self, *}, - PyComparisonValue, ToPyObject, + PyComparisonValue, }, pyclass::PyClassImpl, types::{Comparable, Constructor, Hashable, PyComparisonOp}, diff --git a/vm/src/builtins/function.rs b/vm/src/builtins/function.rs index ded2851b54..934aa78add 100644 --- a/vm/src/builtins/function.rs +++ b/vm/src/builtins/function.rs @@ -17,7 +17,7 @@ use crate::{ AsPyObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine, }; #[cfg(feature = "jit")] -use crate::{common::lock::OnceCell, function::ToPyObject}; +use crate::{common::lock::OnceCell, convert::ToPyObject}; use itertools::Itertools; #[cfg(feature = "jit")] use rustpython_jit::CompiledCode; diff --git a/vm/src/builtins/function/jitfunc.rs b/vm/src/builtins/function/jitfunc.rs index e3fdc4bedd..1c299e519c 100644 --- a/vm/src/builtins/function/jitfunc.rs +++ b/vm/src/builtins/function/jitfunc.rs @@ -1,7 +1,8 @@ use crate::{ builtins::{float, int, pybool, PyBaseExceptionRef, PyDictRef, PyFunction, PyStrRef}, bytecode::CodeFlags, - function::{FuncArgs, ToPyObject}, + convert::ToPyObject, + function::FuncArgs, AsPyObject, PyObject, PyObjectRef, PyResult, TryFromObject, VirtualMachine, }; use num_traits::ToPrimitive; diff --git a/vm/src/builtins/genericalias.rs b/vm/src/builtins/genericalias.rs index 89f168694a..1e937013ae 100644 --- a/vm/src/builtins/genericalias.rs +++ b/vm/src/builtins/genericalias.rs @@ -1,7 +1,8 @@ use crate::{ builtins::{PyList, PyStr, PyStrRef, PyTuple, PyTupleRef, PyType, PyTypeRef}, common::hash, - function::{FuncArgs, PyComparisonValue, ToPyObject}, + convert::ToPyObject, + function::{FuncArgs, PyComparisonValue}, protocol::PyMappingMethods, pyclass::PyClassImpl, types::{AsMapping, Callable, Comparable, Constructor, GetAttr, Hashable, PyComparisonOp}, diff --git a/vm/src/builtins/getset.rs b/vm/src/builtins/getset.rs index e8b2ccc4f9..a781519048 100644 --- a/vm/src/builtins/getset.rs +++ b/vm/src/builtins/getset.rs @@ -3,7 +3,8 @@ */ use super::PyTypeRef; use crate::{ - function::{OwnedParam, RefParam, ToPyResult}, + convert::ToPyResult, + function::{OwnedParam, RefParam}, pyclass::PyClassImpl, pyobject::PyThreadingConstraint, types::{Constructor, GetDescriptor, Unconstructible}, diff --git a/vm/src/builtins/int.rs b/vm/src/builtins/int.rs index 63a332370d..d32ddd03e7 100644 --- a/vm/src/builtins/int.rs +++ b/vm/src/builtins/int.rs @@ -2,11 +2,9 @@ use super::{float, PyByteArray, PyBytes, PyStr, PyStrRef, PyTypeRef}; use crate::{ bytesinner::PyBytesInner, common::hash, + convert::{ToPyObject, ToPyResult}, format::FormatSpec, - function::{ - ArgIntoBool, OptionalArg, OptionalOption, PyArithmeticValue, PyComparisonValue, ToPyObject, - ToPyResult, - }, + function::{ArgIntoBool, OptionalArg, OptionalOption, PyArithmeticValue, PyComparisonValue}, pyclass::PyClassImpl, types::{Comparable, Constructor, Hashable, PyComparisonOp}, AsPyObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromBorrowedObject, diff --git a/vm/src/builtins/list.rs b/vm/src/builtins/list.rs index d40f22dede..6977fe3042 100644 --- a/vm/src/builtins/list.rs +++ b/vm/src/builtins/list.rs @@ -4,7 +4,8 @@ use crate::common::lock::{ }; use crate::TryFromBorrowedObject; use crate::{ - function::{FuncArgs, OptionalArg, PyComparisonValue, ToPyObject}, + convert::ToPyObject, + function::{FuncArgs, OptionalArg, PyComparisonValue}, protocol::{PyIterReturn, PyMappingMethods, PySequence, PySequenceMethods}, pyclass::PyClassImpl, sequence::{MutObjectSequenceOp, ObjectSequenceOp, SequenceMutOp, SequenceOp}, diff --git a/vm/src/builtins/mappingproxy.rs b/vm/src/builtins/mappingproxy.rs index a1effa0f3a..1eb7d46849 100644 --- a/vm/src/builtins/mappingproxy.rs +++ b/vm/src/builtins/mappingproxy.rs @@ -2,7 +2,8 @@ use std::borrow::Cow; use super::{PyDict, PyGenericAlias, PyList, PyStr, PyStrRef, PyTuple, PyTypeRef}; use crate::{ - function::{OptionalArg, ToPyObject}, + convert::ToPyObject, + function::OptionalArg, protocol::{PyMapping, PyMappingMethods, PySequence, PySequenceMethods}, pyclass::PyClassImpl, types::{AsMapping, AsSequence, Constructor, Iterable}, diff --git a/vm/src/builtins/memory.rs b/vm/src/builtins/memory.rs index 2b95262b4d..b8228c6219 100644 --- a/vm/src/builtins/memory.rs +++ b/vm/src/builtins/memory.rs @@ -9,7 +9,8 @@ use crate::{ hash::PyHash, lock::OnceCell, }, - function::{FuncArgs, OptionalArg, PyComparisonValue, ToPyObject}, + convert::ToPyObject, + function::{FuncArgs, OptionalArg, PyComparisonValue}, protocol::{ BufferDescriptor, BufferMethods, PyBuffer, PyMappingMethods, PySequenceMethods, VecBuffer, }, diff --git a/vm/src/builtins/module.rs b/vm/src/builtins/module.rs index 8fe6705509..c199cdc72f 100644 --- a/vm/src/builtins/module.rs +++ b/vm/src/builtins/module.rs @@ -1,10 +1,8 @@ use super::pystr::IntoPyStrRef; use super::{PyDictRef, PyStr, PyStrRef, PyTypeRef}; use crate::{ - function::{FuncArgs, ToPyObject}, - pyclass::PyClassImpl, - types::GetAttr, - AsPyObject, PyContext, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, VirtualMachine, + convert::ToPyObject, function::FuncArgs, pyclass::PyClassImpl, types::GetAttr, AsPyObject, + PyContext, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, VirtualMachine, }; #[pyclass(module = false, name = "module")] diff --git a/vm/src/builtins/pybool.rs b/vm/src/builtins/pybool.rs index 4449243844..a4e0850287 100644 --- a/vm/src/builtins/pybool.rs +++ b/vm/src/builtins/pybool.rs @@ -1,8 +1,6 @@ use super::{PyInt, PyStrRef, PyTypeRef}; use crate::{ - function::{OptionalArg, ToPyObject}, - pyclass::PyClassImpl, - types::Constructor, + convert::ToPyObject, function::OptionalArg, pyclass::PyClassImpl, types::Constructor, AsPyObject, PyContext, PyObject, PyObjectRef, PyResult, PyValue, TryFromBorrowedObject, VirtualMachine, }; diff --git a/vm/src/builtins/pystr.rs b/vm/src/builtins/pystr.rs index 3bc791162f..70935116e1 100644 --- a/vm/src/builtins/pystr.rs +++ b/vm/src/builtins/pystr.rs @@ -5,11 +5,9 @@ use super::{ }; use crate::{ anystr::{self, adjust_indices, AnyStr, AnyStrContainer, AnyStrWrapper}, + convert::{ToPyException, ToPyObject}, format::{FormatSpec, FormatString, FromTemplate}, - function::{ - ArgIterable, FuncArgs, OptionalArg, OptionalOption, PyComparisonValue, ToPyException, - ToPyObject, - }, + function::{ArgIterable, FuncArgs, OptionalArg, OptionalOption, PyComparisonValue}, protocol::{PyIterReturn, PyMappingMethods, PySequenceMethods}, pyclass::PyClassImpl, sequence::SequenceOp, diff --git a/vm/src/builtins/pyunion.rs b/vm/src/builtins/pyunion.rs index f8cdfbecaf..248a1415e8 100644 --- a/vm/src/builtins/pyunion.rs +++ b/vm/src/builtins/pyunion.rs @@ -2,7 +2,8 @@ use super::genericalias; use crate::{ builtins::{PyFrozenSet, PyStr, PyStrRef, PyTuple, PyTupleRef, PyTypeRef}, common::hash, - function::{PyComparisonValue, ToPyObject}, + convert::ToPyObject, + function::PyComparisonValue, protocol::PyMappingMethods, pyclass::PyClassImpl, types::{AsMapping, Comparable, GetAttr, Hashable, Iterable, PyComparisonOp}, diff --git a/vm/src/builtins/singletons.rs b/vm/src/builtins/singletons.rs index 2544039dcc..c7d4a53c36 100644 --- a/vm/src/builtins/singletons.rs +++ b/vm/src/builtins/singletons.rs @@ -1,6 +1,6 @@ use super::PyTypeRef; use crate::{ - function::ToPyObject, pyclass::PyClassImpl, types::Constructor, AsPyObject, PyContext, + convert::ToPyObject, pyclass::PyClassImpl, types::Constructor, AsPyObject, PyContext, PyObjectRef, PyResult, PyValue, VirtualMachine, }; diff --git a/vm/src/builtins/slice.rs b/vm/src/builtins/slice.rs index aad2e90f37..c229805741 100644 --- a/vm/src/builtins/slice.rs +++ b/vm/src/builtins/slice.rs @@ -1,7 +1,8 @@ // sliceobject.{h,c} in CPython use super::{PyInt, PyIntRef, PyTupleRef, PyTypeRef}; use crate::{ - function::{FuncArgs, OptionalArg, PyComparisonValue, ToPyObject}, + convert::ToPyObject, + function::{FuncArgs, OptionalArg, PyComparisonValue}, pyclass::PyClassImpl, types::{Comparable, Constructor, Hashable, PyComparisonOp, Unhashable}, AsPyObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine, diff --git a/vm/src/builtins/tuple.rs b/vm/src/builtins/tuple.rs index 36fbf4e034..d52ff2faa9 100644 --- a/vm/src/builtins/tuple.rs +++ b/vm/src/builtins/tuple.rs @@ -1,8 +1,8 @@ use super::{PositionIterInternal, PyGenericAlias, PyTypeRef}; use crate::common::{hash::PyHash, lock::PyMutex}; use crate::{ - convert::{TransmuteFromObject, TryFromBorrowedObject}, - function::{OptionalArg, PyArithmeticValue, PyComparisonValue, ToPyObject}, + convert::{ToPyObject, TransmuteFromObject, TryFromBorrowedObject}, + function::{OptionalArg, PyArithmeticValue, PyComparisonValue}, protocol::{PyIterReturn, PyMappingMethods, PySequenceMethods}, pyclass::PyClassImpl, sequence::{ObjectSequenceOp, SequenceOp}, diff --git a/vm/src/codecs.rs b/vm/src/codecs.rs index 996c0e233a..9b8b0f5354 100644 --- a/vm/src/codecs.rs +++ b/vm/src/codecs.rs @@ -1,7 +1,7 @@ use crate::{ builtins::{PyBaseExceptionRef, PyBytesRef, PyStr, PyStrRef, PyTuple, PyTupleRef}, common::{ascii, lock::PyRwLock}, - function::ToPyObject, + convert::ToPyObject, AsPyObject, PyContext, PyObject, PyObjectRef, PyResult, PyValue, TryFromObject, VirtualMachine, }; use std::{borrow::Cow, collections::HashMap, fmt::Write, ops::Range}; diff --git a/vm/src/convert/mod.rs b/vm/src/convert/mod.rs index 4c5aedc48a..a381d0c855 100644 --- a/vm/src/convert/mod.rs +++ b/vm/src/convert/mod.rs @@ -1,5 +1,7 @@ +mod to_pyobject; mod transmute_from; mod try_from; +pub use to_pyobject::{ToPyException, ToPyObject, ToPyResult}; pub use transmute_from::TransmuteFromObject; -pub use try_from::{TryFromBorrowedObject, TryFromObject}; \ No newline at end of file +pub use try_from::{TryFromBorrowedObject, TryFromObject}; diff --git a/vm/src/convert/to_pyobject.rs b/vm/src/convert/to_pyobject.rs new file mode 100644 index 0000000000..6f8f9931de --- /dev/null +++ b/vm/src/convert/to_pyobject.rs @@ -0,0 +1,18 @@ +use crate::{builtins::PyBaseExceptionRef, PyObjectRef, PyResult, VirtualMachine}; + +/// Implemented by any type that can be returned from a built-in Python function. +/// +/// `ToPyObject` has a blanket implementation for any built-in object payload, +/// and should be implemented by many primitive Rust types, allowing a built-in +/// function to simply return a `bool` or a `usize` for example. +pub trait ToPyObject { + fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef; +} + +pub trait ToPyResult { + fn to_pyresult(self, vm: &VirtualMachine) -> PyResult; +} + +pub trait ToPyException { + fn to_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef; +} diff --git a/vm/src/dictdatatype.rs b/vm/src/dictdatatype.rs index ec52e8707c..994cf8161e 100644 --- a/vm/src/dictdatatype.rs +++ b/vm/src/dictdatatype.rs @@ -9,7 +9,7 @@ use crate::common::{ }; use crate::{ builtins::{PyInt, PyStr, PyStrRef}, - function::ToPyObject, + convert::ToPyObject, AsPyObject, PyObject, PyObjectRef, PyRefExact, PyResult, VirtualMachine, }; use num_traits::ToPrimitive; diff --git a/vm/src/exceptions.rs b/vm/src/exceptions.rs index 70e50f27b8..40d3ab2785 100644 --- a/vm/src/exceptions.rs +++ b/vm/src/exceptions.rs @@ -4,7 +4,8 @@ use crate::{ builtins::{ traceback::PyTracebackRef, PyNone, PyStr, PyStrRef, PyTuple, PyTupleRef, PyType, PyTypeRef, }, - function::{ArgIterable, FuncArgs, ToPyException, ToPyObject}, + convert::{ToPyException, ToPyObject}, + function::{ArgIterable, FuncArgs}, py_io::{self, Write}, pyclass::{PyClassImpl, StaticType}, stdlib::sys, @@ -1027,7 +1028,8 @@ pub(super) mod types { #[cfg_attr(target_os = "wasi", allow(unused_imports))] use crate::{ builtins::{traceback::PyTracebackRef, PyInt, PyTupleRef, PyTypeRef}, - function::{FuncArgs, ToPyResult}, + convert::ToPyResult, + function::FuncArgs, PyObjectRef, PyRef, PyResult, VirtualMachine, }; use crossbeam_utils::atomic::AtomicCell; diff --git a/vm/src/format.rs b/vm/src/format.rs index ad31dbbf52..38f4ebfa2f 100644 --- a/vm/src/format.rs +++ b/vm/src/format.rs @@ -1,7 +1,8 @@ use crate::{ builtins::{PyBaseExceptionRef, PyStrRef}, common::float_ops, - function::{FuncArgs, ToPyException}, + convert::ToPyException, + function::FuncArgs, stdlib::builtins, AsPyObject, PyObject, PyObjectRef, PyResult, VirtualMachine, }; diff --git a/vm/src/frame.rs b/vm/src/frame.rs index 6889ce4296..29d20fceea 100644 --- a/vm/src/frame.rs +++ b/vm/src/frame.rs @@ -8,9 +8,10 @@ use crate::{ PySlice, PyStr, PyStrRef, PyTraceback, PyTypeRef, }, bytecode, + convert::ToPyResult, coroutine::Coro, exceptions::ExceptionCtor, - function::{ArgMapping, FuncArgs, ToPyResult}, + function::{ArgMapping, FuncArgs}, protocol::{PyIter, PyIterReturn}, scope::Scope, stdlib::builtins, diff --git a/vm/src/function.rs b/vm/src/function.rs index eface4b2ee..80617b1526 100644 --- a/vm/src/function.rs +++ b/vm/src/function.rs @@ -5,6 +5,7 @@ mod number; use crate::{ builtins::{PyBaseExceptionRef, PyTupleRef, PyTypeRef}, + convert::{ToPyObject, ToPyResult}, pyobject::{PyObjectPayload, PyThreadingConstraint}, AsPyObject, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, VirtualMachine, }; @@ -17,27 +18,10 @@ pub use arithmetic::{PyArithmeticValue, PyComparisonValue}; pub use buffer::{ArgAsciiBuffer, ArgBytesLike, ArgMemoryBuffer, ArgStrOrBytesLike}; pub use number::{ArgIntoBool, ArgIntoComplex, ArgIntoFloat}; -/// Implemented by any type that can be returned from a built-in Python function. -/// -/// `ToPyObject` has a blanket implementation for any built-in object payload, -/// and should be implemented by many primitive Rust types, allowing a built-in -/// function to simply return a `bool` or a `usize` for example. -pub trait ToPyObject { - fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef; -} - -pub trait ToPyResult { - fn to_pyresult(self, vm: &VirtualMachine) -> PyResult; -} - pub trait IntoPyRef { fn into_pyref(self, vm: &VirtualMachine) -> PyRef; } -pub trait ToPyException { - fn to_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef; -} - pub trait IntoFuncArgs: Sized { fn into_args(self, vm: &VirtualMachine) -> FuncArgs; fn into_method_args(self, obj: PyObjectRef, vm: &VirtualMachine) -> FuncArgs { diff --git a/vm/src/function/argument.rs b/vm/src/function/argument.rs index 5eec8ba621..0decb6f474 100644 --- a/vm/src/function/argument.rs +++ b/vm/src/function/argument.rs @@ -1,8 +1,8 @@ -use super::{IntoFuncArgs, ToPyObject}; +use super::IntoFuncArgs; use crate::{ builtins::{iter::PySequenceIterator, PyDict, PyDictRef}, - protocol::PyIter, - protocol::{PyIterIter, PyMapping, PyMappingMethods}, + convert::ToPyObject, + protocol::{PyIter, PyIterIter, PyMapping, PyMappingMethods}, AsPyObject, PyObject, PyObjectRef, PyObjectWrap, PyResult, PyValue, TryFromObject, VirtualMachine, }; diff --git a/vm/src/function/arithmetic.rs b/vm/src/function/arithmetic.rs index dc4788930e..4229328c0a 100644 --- a/vm/src/function/arithmetic.rs +++ b/vm/src/function/arithmetic.rs @@ -1,6 +1,5 @@ use crate::{ - convert::TryFromObject, - function::ToPyObject, + convert::{ToPyObject, TryFromObject}, pyobject::{AsPyObject, PyObjectRef, PyResult}, VirtualMachine, }; diff --git a/vm/src/protocol/iter.rs b/vm/src/protocol/iter.rs index 85c60976fe..7559fbead7 100644 --- a/vm/src/protocol/iter.rs +++ b/vm/src/protocol/iter.rs @@ -1,6 +1,6 @@ use crate::{ builtins::iter::PySequenceIterator, - function::{ToPyObject, ToPyResult}, + convert::{ToPyObject, ToPyResult}, AsPyObject, PyObject, PyObjectRef, PyObjectWrap, PyResult, PyValue, TryFromObject, VirtualMachine, }; diff --git a/vm/src/protocol/mapping.rs b/vm/src/protocol/mapping.rs index 336a80b563..fec685ddad 100644 --- a/vm/src/protocol/mapping.rs +++ b/vm/src/protocol/mapping.rs @@ -4,7 +4,7 @@ use crate::{ PyDict, }, common::lock::OnceCell, - function::ToPyResult, + convert::ToPyResult, AsPyObject, PyObject, PyObjectRef, PyResult, VirtualMachine, }; diff --git a/vm/src/protocol/object.rs b/vm/src/protocol/object.rs index 8ada3ccc49..eca1109aa7 100644 --- a/vm/src/protocol/object.rs +++ b/vm/src/protocol/object.rs @@ -8,8 +8,9 @@ use crate::{ }, bytesinner::ByteInnerNewOptions, common::{hash::PyHash, str::to_ascii}, + convert::{ToPyObject, ToPyResult}, dictdatatype::DictKey, - function::{OptionalArg, PyArithmeticValue, ToPyObject, ToPyResult}, + function::{OptionalArg, PyArithmeticValue}, protocol::{PyIter, PyMapping, PySequence}, types::{Constructor, PyComparisonOp}, utils::Either, diff --git a/vm/src/protocol/sequence.rs b/vm/src/protocol/sequence.rs index badbb1c220..063ccb9d08 100644 --- a/vm/src/protocol/sequence.rs +++ b/vm/src/protocol/sequence.rs @@ -1,7 +1,8 @@ use crate::{ builtins::{PyList, PyListRef, PySlice, PyTuple, PyTupleRef}, common::lock::OnceCell, - function::{PyArithmeticValue, ToPyObject}, + convert::ToPyObject, + function::PyArithmeticValue, protocol::PyMapping, AsPyObject, PyObject, PyObjectRef, PyResult, PyValue, VirtualMachine, }; diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index 2b73226bda..aba49d466b 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -11,9 +11,10 @@ use crate::{ PyType, PyTypeRef, }, convert::TryFromObject, + convert::{ToPyObject, ToPyResult}, dictdatatype::Dict, exceptions, - function::{IntoFuncArgs, IntoPyNativeFunc, IntoPyRef, ToPyObject, ToPyResult}, + function::{IntoFuncArgs, IntoPyNativeFunc, IntoPyRef}, pyclass::{PyClassImpl, StaticType}, types::{PyTypeFlags, PyTypeSlots, TypeZoo}, VirtualMachine, diff --git a/vm/src/scope.rs b/vm/src/scope.rs index 694f7c7b3e..27c443c2c5 100644 --- a/vm/src/scope.rs +++ b/vm/src/scope.rs @@ -1,6 +1,7 @@ use crate::{ builtins::{pystr::IntoPyStrRef, PyDictRef, PyStrRef}, - function::{ArgMapping, ToPyObject}, + convert::ToPyObject, + function::ArgMapping, VirtualMachine, }; use std::fmt; diff --git a/vm/src/stdlib/io.rs b/vm/src/stdlib/io.rs index e7271559d8..2875c96ff8 100644 --- a/vm/src/stdlib/io.rs +++ b/vm/src/stdlib/io.rs @@ -77,9 +77,9 @@ mod _io { PyMappedThreadMutexGuard, PyRwLock, PyRwLockReadGuard, PyRwLockWriteGuard, PyThreadMutex, PyThreadMutexGuard, }, + convert::ToPyObject, function::{ ArgBytesLike, ArgIterable, ArgMemoryBuffer, FuncArgs, OptionalArg, OptionalOption, - ToPyObject, }, protocol::{ BufferDescriptor, BufferMethods, BufferResizeGuard, PyBuffer, PyIterReturn, VecBuffer, @@ -160,7 +160,7 @@ mod _io { fn os_err(vm: &VirtualMachine, err: io::Error) -> PyBaseExceptionRef { #[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))] { - use crate::function::ToPyException; + use crate::convert::ToPyException; err.to_pyexception(vm) } #[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))] @@ -3656,10 +3656,9 @@ mod fileio { use super::{Offset, _io::*}; use crate::{ builtins::{PyStr, PyStrRef, PyTypeRef}, + convert::ToPyException, crt_fd::Fd, - function::{ - ArgBytesLike, ArgMemoryBuffer, FuncArgs, OptionalArg, OptionalOption, ToPyException, - }, + function::{ArgBytesLike, ArgMemoryBuffer, FuncArgs, OptionalArg, OptionalOption}, stdlib::os, AsPyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, VirtualMachine, }; diff --git a/vm/src/stdlib/itertools.rs b/vm/src/stdlib/itertools.rs index 1d90c40a22..1049d036b8 100644 --- a/vm/src/stdlib/itertools.rs +++ b/vm/src/stdlib/itertools.rs @@ -8,7 +8,8 @@ mod decl { }; use crate::{ builtins::{int, PyGenericAlias, PyInt, PyIntRef, PyTuple, PyTupleRef, PyTypeRef}, - function::{ArgCallable, FuncArgs, OptionalArg, OptionalOption, PosArgs, ToPyObject}, + convert::ToPyObject, + function::{ArgCallable, FuncArgs, OptionalArg, OptionalOption, PosArgs}, protocol::{PyIter, PyIterReturn}, stdlib::sys, types::{Constructor, IterNext, IterNextIterable}, diff --git a/vm/src/stdlib/marshal.rs b/vm/src/stdlib/marshal.rs index ba213f1fec..c5e7497fe4 100644 --- a/vm/src/stdlib/marshal.rs +++ b/vm/src/stdlib/marshal.rs @@ -8,7 +8,8 @@ mod decl { PyInt, PyList, PySet, PyStr, PyTuple, }, bytecode, - function::{ArgBytesLike, ToPyObject}, + convert::ToPyObject, + function::ArgBytesLike, protocol::PyBuffer, pyobject::AsPyObject, PyObjectRef, PyResult, TryFromObject, VirtualMachine, diff --git a/vm/src/stdlib/nt.rs b/vm/src/stdlib/nt.rs index 5346c131c8..c00808cdb9 100644 --- a/vm/src/stdlib/nt.rs +++ b/vm/src/stdlib/nt.rs @@ -12,8 +12,9 @@ pub(crate) fn make_module(vm: &VirtualMachine) -> PyObjectRef { pub(crate) mod module { use crate::{ builtins::{PyStrRef, PyTupleRef}, + convert::ToPyException, crt_fd::Fd, - function::{OptionalArg, ToPyException}, + function::OptionalArg, stdlib::os::{ errno_err, DirFd, FollowSymlinks, PyPathLike, SupportFunc, TargetIsDirectory, _os, errno, diff --git a/vm/src/stdlib/os.rs b/vm/src/stdlib/os.rs index 5dacdffa61..ee8740e145 100644 --- a/vm/src/stdlib/os.rs +++ b/vm/src/stdlib/os.rs @@ -1,7 +1,8 @@ use crate::{ builtins::{PyBaseExceptionRef, PyBytes, PyBytesRef, PyInt, PySet, PyStr, PyStrRef}, + convert::{ToPyException, ToPyObject}, crt_fd::Fd, - function::{ArgumentError, FromArgs, FuncArgs, ToPyException, ToPyObject}, + function::{ArgumentError, FromArgs, FuncArgs}, protocol::PyBuffer, AsPyObject, PyObject, PyObjectRef, PyResult, PyValue, TryFromBorrowedObject, TryFromObject, VirtualMachine, @@ -433,8 +434,9 @@ pub(super) mod _os { builtins::{ PyBytesRef, PyGenericAlias, PyIntRef, PyStrRef, PyTuple, PyTupleRef, PyTypeRef, }, + convert::{ToPyException, ToPyObject}, crt_fd::{Fd, Offset}, - function::{ArgBytesLike, FuncArgs, IntoPyRef, OptionalArg, ToPyException, ToPyObject}, + function::{ArgBytesLike, FuncArgs, IntoPyRef, OptionalArg}, protocol::PyIterReturn, suppress_iph, types::{IterNext, IterNextIterable}, diff --git a/vm/src/stdlib/posix.rs b/vm/src/stdlib/posix.rs index 50a5958527..99f7c5836a 100644 --- a/vm/src/stdlib/posix.rs +++ b/vm/src/stdlib/posix.rs @@ -30,14 +30,15 @@ pub(crate) fn make_module(vm: &VirtualMachine) -> PyObjectRef { pub mod module { use crate::{ builtins::{PyDictRef, PyInt, PyIntRef, PyListRef, PyStrRef, PyTupleRef, PyTypeRef}, - function::{OptionalArg, ToPyException, ToPyObject}, + convert::{ToPyException, ToPyObject, TryFromObject}, + function::OptionalArg, stdlib::os::{ errno_err, DirFd, FollowSymlinks, PathOrFd, PyPathLike, SupportFunc, TargetIsDirectory, _os, fs_metadata, IOErrorBuilder, }, types::Constructor, utils::{Either, ToCString}, - AsPyObject, PyObjectRef, PyResult, PyValue, TryFromObject, VirtualMachine, + AsPyObject, PyObjectRef, PyResult, PyValue, VirtualMachine, }; use bitflags::bitflags; use nix::{ diff --git a/vm/src/stdlib/pwd.rs b/vm/src/stdlib/pwd.rs index e51950296a..125ae5a532 100644 --- a/vm/src/stdlib/pwd.rs +++ b/vm/src/stdlib/pwd.rs @@ -4,7 +4,7 @@ pub(crate) use pwd::make_module; mod pwd { use crate::{ builtins::{PyIntRef, PyStrRef}, - function::{ToPyException, ToPyObject}, + convert::{ToPyException, ToPyObject}, AsPyObject, PyObjectRef, PyResult, PyStructSequence, VirtualMachine, }; use nix::unistd::{self, User}; diff --git a/vm/src/stdlib/signal.rs b/vm/src/stdlib/signal.rs index a55a92a1b2..5ed656f0d4 100644 --- a/vm/src/stdlib/signal.rs +++ b/vm/src/stdlib/signal.rs @@ -11,7 +11,7 @@ pub(crate) fn make_module(vm: &VirtualMachine) -> PyObjectRef { #[pymodule] pub(crate) mod _signal { use crate::{ - convert::TryFromBorrowedObject, function::ToPyException, signal, PyObjectRef, PyResult, + convert::ToPyException, convert::TryFromBorrowedObject, signal, PyObjectRef, PyResult, VirtualMachine, }; use std::sync::atomic::{self, Ordering}; diff --git a/vm/src/stdlib/sre.rs b/vm/src/stdlib/sre.rs index a44d82ea1c..63f17f27d4 100644 --- a/vm/src/stdlib/sre.rs +++ b/vm/src/stdlib/sre.rs @@ -8,7 +8,8 @@ mod _sre { PyTupleRef, PyTypeRef, }, common::{ascii, hash::PyHash}, - function::{ArgCallable, OptionalArg, PosArgs, PyComparisonValue, ToPyObject}, + convert::ToPyObject, + function::{ArgCallable, OptionalArg, PosArgs, PyComparisonValue}, protocol::{PyBuffer, PyMappingMethods}, stdlib::sys, types::{AsMapping, Comparable, Hashable}, diff --git a/vm/src/stdlib/string.rs b/vm/src/stdlib/string.rs index 9d03e9c58c..abba6dbfad 100644 --- a/vm/src/stdlib/string.rs +++ b/vm/src/stdlib/string.rs @@ -8,9 +8,9 @@ mod _string { use crate::common::ascii; use crate::{ builtins::{PyList, PyStrRef}, + convert::ToPyException, + convert::ToPyObject, format::{FieldName, FieldNamePart, FieldType, FormatPart, FormatString, FromTemplate}, - function::ToPyException, - function::ToPyObject, PyObjectRef, PyResult, VirtualMachine, }; use std::mem; diff --git a/vm/src/stdlib/sys.rs b/vm/src/stdlib/sys.rs index d8373557a7..938fa77f38 100644 --- a/vm/src/stdlib/sys.rs +++ b/vm/src/stdlib/sys.rs @@ -1,4 +1,4 @@ -use crate::{function::ToPyObject, pyclass::PyClassImpl, PyObject, PyResult, VirtualMachine}; +use crate::{convert::ToPyObject, pyclass::PyClassImpl, PyObject, PyResult, VirtualMachine}; pub(crate) use sys::{MAXSIZE, MULTIARCH}; diff --git a/vm/src/stdlib/sysconfigdata.rs b/vm/src/stdlib/sysconfigdata.rs index d3bb64bd85..4ed2c6123c 100644 --- a/vm/src/stdlib/sysconfigdata.rs +++ b/vm/src/stdlib/sysconfigdata.rs @@ -2,9 +2,7 @@ pub(crate) use _sysconfigdata::make_module; #[pymodule] pub(crate) mod _sysconfigdata { - use crate::{ - builtins::PyDictRef, function::ToPyObject, stdlib::sys::MULTIARCH, VirtualMachine, - }; + use crate::{builtins::PyDictRef, convert::ToPyObject, stdlib::sys::MULTIARCH, VirtualMachine}; #[pyattr] fn build_time_vars(vm: &VirtualMachine) -> PyDictRef { diff --git a/vm/src/stdlib/thread.rs b/vm/src/stdlib/thread.rs index 89b7c6abbb..55b454edfa 100644 --- a/vm/src/stdlib/thread.rs +++ b/vm/src/stdlib/thread.rs @@ -6,7 +6,8 @@ pub(crate) use _thread::{make_module, RawRMutex}; pub(crate) mod _thread { use crate::{ builtins::{PyDictRef, PyStrRef, PyTupleRef, PyTypeRef}, - function::{ArgCallable, FuncArgs, KwArgs, OptionalArg, ToPyException}, + convert::ToPyException, + function::{ArgCallable, FuncArgs, KwArgs, OptionalArg}, py_io, types::{Constructor, GetAttr, SetAttr}, utils::Either, diff --git a/vm/src/stdlib/winapi.rs b/vm/src/stdlib/winapi.rs index b1b72ed5a7..4522a13409 100644 --- a/vm/src/stdlib/winapi.rs +++ b/vm/src/stdlib/winapi.rs @@ -5,7 +5,8 @@ pub(crate) use _winapi::make_module; mod _winapi { use crate::{ builtins::PyStrRef, - function::{ArgMapping, ArgSequence, OptionalArg, ToPyException}, + convert::ToPyException, + function::{ArgMapping, ArgSequence, OptionalArg}, stdlib::os::errno_err, PyObjectRef, PyResult, TryFromObject, VirtualMachine, }; diff --git a/vm/src/stdlib/winreg.rs b/vm/src/stdlib/winreg.rs index 37668083c8..59c46fa1f2 100644 --- a/vm/src/stdlib/winreg.rs +++ b/vm/src/stdlib/winreg.rs @@ -29,7 +29,7 @@ pub(crate) fn make_module(vm: &VirtualMachine) -> PyObjectRef { mod winreg { use crate::common::lock::{PyRwLock, PyRwLockReadGuard, PyRwLockWriteGuard}; use crate::{ - builtins::PyStrRef, function::ToPyException, PyObjectRef, PyRef, PyResult, PyValue, + builtins::PyStrRef, convert::ToPyException, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, VirtualMachine, }; use ::winreg::{enums::RegType, RegKey, RegValue}; diff --git a/vm/src/types/slot.rs b/vm/src/types/slot.rs index 2d2c2d436f..4dae0ccaa4 100644 --- a/vm/src/types/slot.rs +++ b/vm/src/types/slot.rs @@ -2,9 +2,11 @@ pub use crate::builtins::object::{generic_getattr, generic_setattr}; use crate::common::{hash::PyHash, lock::PyRwLock}; use crate::{ builtins::{PyInt, PyStrRef, PyType, PyTypeRef}, - function::{FromArgs, FuncArgs, OptionalArg, PyComparisonValue, ToPyObject, ToPyResult}, - protocol::{PyBuffer, PyIterReturn, PyMapping, PyMappingMethods}, - protocol::{PySequence, PySequenceMethods}, + convert::{ToPyObject, ToPyResult}, + function::{FromArgs, FuncArgs, OptionalArg, PyComparisonValue}, + protocol::{ + PyBuffer, PyIterReturn, PyMapping, PyMappingMethods, PySequence, PySequenceMethods, + }, utils::Either, AsPyObject, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, VirtualMachine, }; diff --git a/vm/src/utils.rs b/vm/src/utils.rs index fcc87605ab..043b9de910 100644 --- a/vm/src/utils.rs +++ b/vm/src/utils.rs @@ -1,6 +1,6 @@ use crate::{ builtins::{PyFloat, PyStr}, - function::{ToPyException, ToPyObject}, + convert::{ToPyException, ToPyObject}, AsPyObject, PyObject, PyObjectRef, PyObjectWrap, PyResult, TryFromObject, VirtualMachine, }; use num_traits::ToPrimitive; diff --git a/vm/src/vm.rs b/vm/src/vm.rs index 7c10da4ac4..14c3d1eeba 100644 --- a/vm/src/vm.rs +++ b/vm/src/vm.rs @@ -17,9 +17,10 @@ use crate::{ bytecode, codecs::CodecsRegistry, common::{ascii, hash::HashSecret, lock::PyMutex, rc::PyRc}, + convert::ToPyObject, frame::{ExecutionResult, Frame, FrameRef}, frozen, - function::{ArgMapping, FuncArgs, ToPyObject}, + function::{ArgMapping, FuncArgs}, import, protocol::PyIterIter, pyobject::PyLease, diff --git a/vm/src/vm_new.rs b/vm/src/vm_new.rs index acf546eaba..109c101d6b 100644 --- a/vm/src/vm_new.rs +++ b/vm/src/vm_new.rs @@ -7,7 +7,7 @@ use crate::{ tuple::{IntoPyTuple, PyTupleRef}, PyBaseException, PyBaseExceptionRef, PyDictRef, PyModule, PyStrRef, PyTypeRef, }, - function::ToPyObject, + convert::ToPyObject, scope::Scope, vm::VirtualMachine, AsPyObject, PyObject, PyObjectRef, PyObjectWrap, PyRef, diff --git a/wasm/lib/src/browser_module.rs b/wasm/lib/src/browser_module.rs index 2c460f54ca..2cb35bcc47 100644 --- a/wasm/lib/src/browser_module.rs +++ b/wasm/lib/src/browser_module.rs @@ -8,7 +8,8 @@ mod _browser { use js_sys::Promise; use rustpython_vm::{ builtins::{PyDictRef, PyStrRef}, - function::{ArgCallable, OptionalArg, ToPyObject}, + convert::ToPyObject, + function::{ArgCallable, OptionalArg}, import::import_file, pyclass::PyClassImpl, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine, diff --git a/wasm/lib/src/js_module.rs b/wasm/lib/src/js_module.rs index 73f1ff9aef..0f908af28c 100644 --- a/wasm/lib/src/js_module.rs +++ b/wasm/lib/src/js_module.rs @@ -14,7 +14,8 @@ mod _js { use js_sys::{Array, Object, Promise, Reflect}; use rustpython_vm::{ builtins::{PyBaseExceptionRef, PyFloat, PyStrRef, PyTypeRef}, - function::{ArgCallable, OptionalArg, OptionalOption, PosArgs, ToPyObject}, + convert::ToPyObject, + function::{ArgCallable, OptionalArg, OptionalOption, PosArgs}, protocol::PyIterReturn, types::{IterNext, IterNextIterable}, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult, PyValue, TryFromObject, From 659c1b82541c531c023888d18943681cab4bb9ac Mon Sep 17 00:00:00 2001 From: Jeong Yunwon Date: Mon, 18 Apr 2022 06:00:14 +0900 Subject: [PATCH 5/8] PyLease::{into_pyref -> into_owned} --- vm/src/builtins/pytype.rs | 4 ++-- vm/src/pyobject.rs | 12 +++++------- vm/src/vm.rs | 4 ++-- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/vm/src/builtins/pytype.rs b/vm/src/builtins/pytype.rs index 02ab97f406..78944bdf20 100644 --- a/vm/src/builtins/pytype.rs +++ b/vm/src/builtins/pytype.rs @@ -647,7 +647,7 @@ impl GetAttr for PyType { .is_some() { if let Some(descr_get) = attr_class.mro_find_map(|cls| cls.slots.descr_get.load()) { - let mcl = PyLease::into_pyref(mcl).into(); + let mcl = PyLease::into_owned(mcl).into(); return descr_get(attr.clone(), Some(zelf.into()), Some(mcl), vm); } } @@ -874,7 +874,7 @@ fn calculate_meta_class( if winner.fast_issubclass(&base_type) { continue; } else if base_type.fast_issubclass(&winner) { - winner = PyLease::into_pyref(base_type); + winner = PyLease::into_owned(base_type); continue; } diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index aba49d466b..bf8943604a 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -467,11 +467,9 @@ pub struct PyLease<'a, T: PyObjectPayload> { } impl<'a, T: PyObjectPayload + PyValue> PyLease<'a, T> { - // Associated function on purpose, because of deref - #[allow(clippy::wrong_self_convention)] #[inline(always)] - pub fn into_pyref(zelf: Self) -> PyRef { - zelf.inner.clone() + pub fn into_owned(self) -> PyRef { + self.inner.clone() } } @@ -748,7 +746,7 @@ impl PyMethod { .is_some() { drop(descr_cls); - let cls = PyLease::into_pyref(cls).into(); + let cls = PyLease::into_owned(cls).into(); return descr_get(descr, Some(obj), Some(cls), vm).map(Self::Attribute); } } @@ -776,7 +774,7 @@ impl PyMethod { }) } Some(descr_get) => { - let cls = PyLease::into_pyref(cls).into(); + let cls = PyLease::into_owned(cls).into(); descr_get(attr, Some(obj), Some(cls), vm).map(Self::Attribute) } None => Ok(Self::Attribute(attr)), @@ -817,7 +815,7 @@ impl PyMethod { drop(obj_cls); Self::Function { target: obj, func } } else { - let obj_cls = PyLease::into_pyref(obj_cls).into(); + let obj_cls = PyLease::into_owned(obj_cls).into(); let attr = vm .call_get_descriptor_specific(func, Some(obj), Some(obj_cls)) .unwrap_or_else(Ok)?; diff --git a/vm/src/vm.rs b/vm/src/vm.rs index 14c3d1eeba..a4f5f42b23 100644 --- a/vm/src/vm.rs +++ b/vm/src/vm.rs @@ -785,7 +785,7 @@ impl VirtualMachine { .is_some() { drop(descr_cls); - let cls = PyLease::into_pyref(obj_cls).into(); + let cls = PyLease::into_owned(obj_cls).into(); return descr_get(descr, Some(obj), Some(cls), self).map(Some); } } @@ -808,7 +808,7 @@ impl VirtualMachine { } else if let Some((attr, descr_get)) = cls_attr { match descr_get { Some(descr_get) => { - let cls = PyLease::into_pyref(obj_cls).into(); + let cls = PyLease::into_owned(obj_cls).into(); descr_get(attr, Some(obj), Some(cls), self).map(Some) } None => Ok(Some(attr)), From d88fb363089956a229b5ff1697364ace6e33660a Mon Sep 17 00:00:00 2001 From: Jeong Yunwon Date: Mon, 18 Apr 2022 06:02:35 +0900 Subject: [PATCH 6/8] call PyLease::into_owned as method --- vm/src/builtins/pytype.rs | 5 ++--- vm/src/pyobject.rs | 6 +++--- vm/src/vm.rs | 5 ++--- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/vm/src/builtins/pytype.rs b/vm/src/builtins/pytype.rs index 78944bdf20..bb6f11c6d7 100644 --- a/vm/src/builtins/pytype.rs +++ b/vm/src/builtins/pytype.rs @@ -10,7 +10,6 @@ use crate::common::{ use crate::{ function::{FuncArgs, KwArgs, OptionalArg}, pyclass::{PyClassImpl, StaticType}, - pyobject::PyLease, types::{Callable, GetAttr, PyTypeFlags, PyTypeSlots, SetAttr}, AsPyObject, PyContext, PyObjectRef, PyObjectWeak, PyRef, PyResult, PyValue, VirtualMachine, }; @@ -647,7 +646,7 @@ impl GetAttr for PyType { .is_some() { if let Some(descr_get) = attr_class.mro_find_map(|cls| cls.slots.descr_get.load()) { - let mcl = PyLease::into_owned(mcl).into(); + let mcl = mcl.into_owned().into(); return descr_get(attr.clone(), Some(zelf.into()), Some(mcl), vm); } } @@ -874,7 +873,7 @@ fn calculate_meta_class( if winner.fast_issubclass(&base_type) { continue; } else if base_type.fast_issubclass(&winner) { - winner = PyLease::into_owned(base_type); + winner = base_type.into_owned(); continue; } diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index bf8943604a..2cc44873a2 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -746,7 +746,7 @@ impl PyMethod { .is_some() { drop(descr_cls); - let cls = PyLease::into_owned(cls).into(); + let cls = cls.into_owned().into(); return descr_get(descr, Some(obj), Some(cls), vm).map(Self::Attribute); } } @@ -774,7 +774,7 @@ impl PyMethod { }) } Some(descr_get) => { - let cls = PyLease::into_owned(cls).into(); + let cls = cls.into_owned().into(); descr_get(attr, Some(obj), Some(cls), vm).map(Self::Attribute) } None => Ok(Self::Attribute(attr)), @@ -815,7 +815,7 @@ impl PyMethod { drop(obj_cls); Self::Function { target: obj, func } } else { - let obj_cls = PyLease::into_owned(obj_cls).into(); + let obj_cls = obj_cls.into_owned().into(); let attr = vm .call_get_descriptor_specific(func, Some(obj), Some(obj_cls)) .unwrap_or_else(Ok)?; diff --git a/vm/src/vm.rs b/vm/src/vm.rs index a4f5f42b23..b5c3b5ec0a 100644 --- a/vm/src/vm.rs +++ b/vm/src/vm.rs @@ -23,7 +23,6 @@ use crate::{ function::{ArgMapping, FuncArgs}, import, protocol::PyIterIter, - pyobject::PyLease, scope::Scope, signal, stdlib, AsPyObject, PyContext, PyObject, PyObjectRef, PyRef, PyRefExact, PyResult, PyValue, @@ -785,7 +784,7 @@ impl VirtualMachine { .is_some() { drop(descr_cls); - let cls = PyLease::into_owned(obj_cls).into(); + let cls = obj_cls.into_owned().into(); return descr_get(descr, Some(obj), Some(cls), self).map(Some); } } @@ -808,7 +807,7 @@ impl VirtualMachine { } else if let Some((attr, descr_get)) = cls_attr { match descr_get { Some(descr_get) => { - let cls = PyLease::into_owned(obj_cls).into(); + let cls = obj_cls.into_owned().into(); descr_get(attr, Some(obj), Some(cls), self).map(Some) } None => Ok(Some(attr)), From 6121195548bcf86487354f31d6aaa3210630c118 Mon Sep 17 00:00:00 2001 From: Jeong Yunwon Date: Mon, 18 Apr 2022 06:22:09 +0900 Subject: [PATCH 7/8] Replace IntoPyRef with VirtualMachine::new_pyref --- stdlib/src/zlib.rs | 4 ++-- vm/src/builtins/range.rs | 14 +++++++------- vm/src/function.rs | 6 +----- vm/src/pyobject.rs | 12 +----------- vm/src/stdlib/os.rs | 16 ++++++++-------- vm/src/vm_new.rs | 10 +++++++++- 6 files changed, 28 insertions(+), 34 deletions(-) diff --git a/stdlib/src/zlib.rs b/stdlib/src/zlib.rs index 9ab0b23c21..781120d479 100644 --- a/stdlib/src/zlib.rs +++ b/stdlib/src/zlib.rs @@ -5,7 +5,7 @@ mod zlib { use crate::common::lock::PyMutex; use crate::vm::{ builtins::{PyBaseExceptionRef, PyBytes, PyBytesRef, PyIntRef, PyTypeRef}, - function::{ArgBytesLike, IntoPyRef, OptionalArg, OptionalOption}, + function::{ArgBytesLike, OptionalArg, OptionalOption}, PyResult, PyValue, VirtualMachine, }; use adler32::RollingAdler32 as Adler32; @@ -321,7 +321,7 @@ mod zlib { .chain(leftover) .copied() .collect(); - *unused_data = unused.into_pyref(vm); + *unused_data = vm.new_pyref(unused); } } diff --git a/vm/src/builtins/range.rs b/vm/src/builtins/range.rs index cd8e13cb5f..79cb88b603 100644 --- a/vm/src/builtins/range.rs +++ b/vm/src/builtins/range.rs @@ -2,7 +2,7 @@ use super::{PyInt, PyIntRef, PySlice, PyTupleRef, PyTypeRef}; use crate::common::hash::PyHash; use crate::{ builtins::builtins_iter, - function::{FuncArgs, IntoPyRef, OptionalArg, PyComparisonValue}, + function::{FuncArgs, OptionalArg, PyComparisonValue}, protocol::{PyIterReturn, PyMappingMethods, PySequenceMethods}, pyclass::PyClassImpl, types::{ @@ -183,9 +183,9 @@ pub fn init(context: &PyContext) { impl PyRange { fn new(cls: PyTypeRef, stop: PyIntRef, vm: &VirtualMachine) -> PyResult> { PyRange { - start: (0).into_pyref(vm), + start: vm.new_pyref(0), stop, - step: (1).into_pyref(vm), + step: vm.new_pyref(1), } .into_ref_with_type(vm, cls) } @@ -197,7 +197,7 @@ impl PyRange { step: OptionalArg, vm: &VirtualMachine, ) -> PyResult> { - let step = step.unwrap_or_else(|| (1).into_pyref(vm)); + let step = step.unwrap_or_else(|| vm.new_pyref(1)); if step.as_bigint().is_zero() { return Err(vm.new_value_error("range() arg 3 must not be zero".to_owned())); } @@ -350,9 +350,9 @@ impl PyRange { substop = (substop * range_step.as_bigint()) + range_start.as_bigint(); Ok(PyRange { - start: substart.into_pyref(vm), - stop: substop.into_pyref(vm), - step: substep.into_pyref(vm), + start: vm.new_pyref(substart), + stop: vm.new_pyref(substop), + step: vm.new_pyref(substep), } .into_ref(vm) .into()) diff --git a/vm/src/function.rs b/vm/src/function.rs index 80617b1526..19ca84036a 100644 --- a/vm/src/function.rs +++ b/vm/src/function.rs @@ -6,7 +6,7 @@ mod number; use crate::{ builtins::{PyBaseExceptionRef, PyTupleRef, PyTypeRef}, convert::{ToPyObject, ToPyResult}, - pyobject::{PyObjectPayload, PyThreadingConstraint}, + pyobject::PyThreadingConstraint, AsPyObject, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, VirtualMachine, }; use indexmap::IndexMap; @@ -18,10 +18,6 @@ pub use arithmetic::{PyArithmeticValue, PyComparisonValue}; pub use buffer::{ArgAsciiBuffer, ArgBytesLike, ArgMemoryBuffer, ArgStrOrBytesLike}; pub use number::{ArgIntoBool, ArgIntoComplex, ArgIntoFloat}; -pub trait IntoPyRef { - fn into_pyref(self, vm: &VirtualMachine) -> PyRef; -} - pub trait IntoFuncArgs: Sized { fn into_args(self, vm: &VirtualMachine) -> FuncArgs; fn into_method_args(self, obj: PyObjectRef, vm: &VirtualMachine) -> FuncArgs { diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index 2cc44873a2..e916842447 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -14,7 +14,7 @@ use crate::{ convert::{ToPyObject, ToPyResult}, dictdatatype::Dict, exceptions, - function::{IntoFuncArgs, IntoPyNativeFunc, IntoPyRef}, + function::{IntoFuncArgs, IntoPyNativeFunc}, pyclass::{PyClassImpl, StaticType}, types::{PyTypeFlags, PyTypeSlots, TypeZoo}, VirtualMachine, @@ -497,16 +497,6 @@ where } } -impl IntoPyRef

for T -where - P: PyValue + ToPyObject + From, -{ - #[inline(always)] - fn into_pyref(self, vm: &VirtualMachine) -> PyRef

{ - P::from(self).into_ref(vm) - } -} - impl ToPyObject for PyRef { #[inline(always)] fn to_pyobject(self, _vm: &VirtualMachine) -> PyObjectRef { diff --git a/vm/src/stdlib/os.rs b/vm/src/stdlib/os.rs index ee8740e145..87bcb18066 100644 --- a/vm/src/stdlib/os.rs +++ b/vm/src/stdlib/os.rs @@ -436,7 +436,7 @@ pub(super) mod _os { }, convert::{ToPyException, ToPyObject}, crt_fd::{Fd, Offset}, - function::{ArgBytesLike, FuncArgs, IntoPyRef, OptionalArg}, + function::{ArgBytesLike, FuncArgs, OptionalArg}, protocol::PyIterReturn, suppress_iph, types::{IterNext, IterNextIterable}, @@ -1033,13 +1033,13 @@ pub(super) mod _os { let to_f64 = |(s, ns)| (s as f64) + (ns as f64) / (NANOS_PER_SEC as f64); let to_ns = |(s, ns)| s as i128 * NANOS_PER_SEC as i128 + ns as i128; StatResult { - st_mode: stat.st_mode.into_pyref(vm), - st_ino: stat.st_ino.into_pyref(vm), - st_dev: stat.st_dev.into_pyref(vm), - st_nlink: stat.st_nlink.into_pyref(vm), - st_uid: stat.st_uid.into_pyref(vm), - st_gid: stat.st_gid.into_pyref(vm), - st_size: stat.st_size.into_pyref(vm), + st_mode: vm.new_pyref(stat.st_mode), + st_ino: vm.new_pyref(stat.st_ino), + st_dev: vm.new_pyref(stat.st_dev), + st_nlink: vm.new_pyref(stat.st_nlink), + st_uid: vm.new_pyref(stat.st_uid), + st_gid: vm.new_pyref(stat.st_gid), + st_size: vm.new_pyref(stat.st_size), __st_atime_int: atime.0, __st_mtime_int: mtime.0, __st_ctime_int: ctime.0, diff --git a/vm/src/vm_new.rs b/vm/src/vm_new.rs index 109c101d6b..d1b6a07a13 100644 --- a/vm/src/vm_new.rs +++ b/vm/src/vm_new.rs @@ -10,7 +10,7 @@ use crate::{ convert::ToPyObject, scope::Scope, vm::VirtualMachine, - AsPyObject, PyObject, PyObjectRef, PyObjectWrap, PyRef, + AsPyObject, PyObject, PyObjectRef, PyObjectWrap, PyRef, PyValue, }; /// Collection of object creation helpers @@ -20,6 +20,14 @@ impl VirtualMachine { value.to_pyobject(self) } + pub fn new_pyref(&self, value: T) -> PyRef

+ where + T: Into

, + P: PyValue, + { + value.into().into_ref(self) + } + pub fn new_tuple(&self, value: impl IntoPyTuple) -> PyTupleRef { value.into_pytuple(self) } From fee3bf272241ef607fc3864b69fa807f58484032 Mon Sep 17 00:00:00 2001 From: Jeong Yunwon Date: Mon, 18 Apr 2022 07:19:35 +0900 Subject: [PATCH 8/8] AsPyObject -> AsObject because fn as_object --- src/lib.rs | 2 +- src/shell.rs | 2 +- stdlib/src/array.rs | 2 +- stdlib/src/csv.rs | 2 +- stdlib/src/json.rs | 2 +- stdlib/src/math.rs | 2 +- stdlib/src/pystruct.rs | 2 +- stdlib/src/select.rs | 2 +- stdlib/src/socket.rs | 2 +- vm/src/anystr.rs | 2 +- vm/src/builtins/asyncgenerator.rs | 2 +- vm/src/builtins/builtinfunc.rs | 2 +- vm/src/builtins/bytearray.rs | 2 +- vm/src/builtins/bytes.rs | 2 +- vm/src/builtins/classmethod.rs | 2 +- vm/src/builtins/code.rs | 2 +- vm/src/builtins/complex.rs | 2 +- vm/src/builtins/coroutine.rs | 2 +- vm/src/builtins/dict.rs | 2 +- vm/src/builtins/enumerate.rs | 2 +- vm/src/builtins/float.rs | 2 +- vm/src/builtins/frame.rs | 2 +- vm/src/builtins/function.rs | 2 +- vm/src/builtins/function/jitfunc.rs | 2 +- vm/src/builtins/generator.rs | 2 +- vm/src/builtins/genericalias.rs | 2 +- vm/src/builtins/getset.rs | 2 +- vm/src/builtins/int.rs | 2 +- vm/src/builtins/list.rs | 2 +- vm/src/builtins/mappingproxy.rs | 2 +- vm/src/builtins/memory.rs | 2 +- vm/src/builtins/module.rs | 2 +- vm/src/builtins/namespace.rs | 2 +- vm/src/builtins/object.rs | 2 +- vm/src/builtins/property.rs | 2 +- vm/src/builtins/pybool.rs | 5 ++--- vm/src/builtins/pystr.rs | 2 +- vm/src/builtins/pysuper.rs | 2 +- vm/src/builtins/pytype.rs | 2 +- vm/src/builtins/pyunion.rs | 2 +- vm/src/builtins/range.rs | 2 +- vm/src/builtins/set.rs | 2 +- vm/src/builtins/singletons.rs | 2 +- vm/src/builtins/slice.rs | 2 +- vm/src/builtins/tuple.rs | 2 +- vm/src/builtins/weakref.rs | 2 +- vm/src/builtins/zip.rs | 2 +- vm/src/bytesinner.rs | 2 +- vm/src/cformat.rs | 2 +- vm/src/codecs.rs | 2 +- vm/src/convert/transmute_from.rs | 2 +- vm/src/convert/try_from.rs | 2 +- vm/src/coroutine.rs | 2 +- vm/src/dictdatatype.rs | 2 +- vm/src/exceptions.rs | 2 +- vm/src/format.rs | 2 +- vm/src/frame.rs | 2 +- vm/src/function.rs | 2 +- vm/src/function/argument.rs | 2 +- vm/src/function/arithmetic.rs | 2 +- vm/src/function/number.rs | 2 +- vm/src/import.rs | 2 +- vm/src/lib.rs | 2 +- vm/src/macros.rs | 2 +- vm/src/protocol/buffer.rs | 2 +- vm/src/protocol/iter.rs | 2 +- vm/src/protocol/mapping.rs | 6 +++--- vm/src/protocol/object.rs | 2 +- vm/src/protocol/sequence.rs | 2 +- vm/src/py_serde.rs | 2 +- vm/src/pyobject.rs | 8 ++++---- vm/src/pyobjectrc.rs | 2 +- vm/src/sequence.rs | 2 +- vm/src/sliceable.rs | 2 +- vm/src/stdlib/ast.rs | 4 ++-- vm/src/stdlib/atexit.rs | 2 +- vm/src/stdlib/builtins.rs | 2 +- vm/src/stdlib/codecs.rs | 2 +- vm/src/stdlib/collections.rs | 2 +- vm/src/stdlib/io.rs | 4 ++-- vm/src/stdlib/itertools.rs | 2 +- vm/src/stdlib/marshal.rs | 2 +- vm/src/stdlib/operator.rs | 2 +- vm/src/stdlib/os.rs | 4 ++-- vm/src/stdlib/posix.rs | 4 ++-- vm/src/stdlib/pwd.rs | 2 +- vm/src/stdlib/thread.rs | 2 +- vm/src/stdlib/warnings.rs | 2 +- vm/src/suggestion.rs | 2 +- vm/src/types/slot.rs | 2 +- vm/src/utils.rs | 2 +- vm/src/vm.rs | 4 ++-- vm/src/vm_new.rs | 6 +++--- vm/src/vm_object.rs | 4 ++-- vm/src/vm_ops.rs | 2 +- wasm/lib/src/convert.rs | 2 +- 96 files changed, 110 insertions(+), 111 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 366df3b301..bc08a47e90 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -51,7 +51,7 @@ use rustpython_vm::{ compile, match_class, scope::Scope, stdlib::{atexit, sys}, - AsPyObject, InitParameter, Interpreter, PyObjectRef, PyResult, PySettings, TryFromObject, + AsObject, InitParameter, Interpreter, PyObjectRef, PyResult, PySettings, TryFromObject, VirtualMachine, }; use std::{env, path::Path, process, str::FromStr}; diff --git a/src/shell.rs b/src/shell.rs index 73288359c5..3edb9434a9 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -6,7 +6,7 @@ use rustpython_vm::{ builtins::PyBaseExceptionRef, compile::{self, CompileError, CompileErrorType}, scope::Scope, - AsPyObject, PyResult, VirtualMachine, + AsObject, PyResult, VirtualMachine, }; enum ShellExecResult { diff --git a/stdlib/src/array.rs b/stdlib/src/array.rs index 3a69bf0816..6542f68f08 100644 --- a/stdlib/src/array.rs +++ b/stdlib/src/array.rs @@ -31,7 +31,7 @@ mod array { AsBuffer, AsMapping, Comparable, Constructor, IterNext, IterNextIterable, Iterable, PyComparisonOp, }, - AsPyObject, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, + AsObject, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, VirtualMachine, }, }; diff --git a/stdlib/src/csv.rs b/stdlib/src/csv.rs index 72954eff87..0a9367b94e 100644 --- a/stdlib/src/csv.rs +++ b/stdlib/src/csv.rs @@ -16,7 +16,7 @@ mod _csv { match_class, protocol::{PyIter, PyIterReturn}, types::{IterNext, IterNextIterable}, - AsPyObject, PyObjectRef, PyObjectView, PyResult, PyValue, TryFromObject, VirtualMachine, + AsObject, PyObjectRef, PyObjectView, PyResult, PyValue, TryFromObject, VirtualMachine, }; use itertools::{self, Itertools}; use std::fmt; diff --git a/stdlib/src/json.rs b/stdlib/src/json.rs index bb663eb93e..3b68d83ce4 100644 --- a/stdlib/src/json.rs +++ b/stdlib/src/json.rs @@ -10,7 +10,7 @@ mod _json { function::OptionalArg, protocol::PyIterReturn, types::{Callable, Constructor}, - AsPyObject, PyObjectRef, PyObjectView, PyResult, PyValue, VirtualMachine, + AsObject, PyObjectRef, PyObjectView, PyResult, PyValue, VirtualMachine, }; use num_bigint::BigInt; use std::str::FromStr; diff --git a/stdlib/src/math.rs b/stdlib/src/math.rs index 17379f27b7..5e2b0dd303 100644 --- a/stdlib/src/math.rs +++ b/stdlib/src/math.rs @@ -6,7 +6,7 @@ mod math { builtins::{try_bigint_to_f64, try_f64_to_bigint, PyFloat, PyInt, PyIntRef}, function::{ArgIntoFloat, ArgIterable, OptionalArg, PosArgs}, utils::Either, - AsPyObject, PyObject, PyObjectRef, PyRef, PyResult, VirtualMachine, + AsObject, PyObject, PyObjectRef, PyRef, PyResult, VirtualMachine, }; use num_bigint::BigInt; use num_traits::{One, Signed, Zero}; diff --git a/stdlib/src/pystruct.rs b/stdlib/src/pystruct.rs index da736f87e8..1594d9c136 100644 --- a/stdlib/src/pystruct.rs +++ b/stdlib/src/pystruct.rs @@ -16,7 +16,7 @@ pub(crate) mod _struct { match_class, protocol::PyIterReturn, types::{Constructor, IterNext, IterNextIterable}, - AsPyObject, PyObjectRef, PyObjectView, PyResult, PyValue, TryFromObject, VirtualMachine, + AsObject, PyObjectRef, PyObjectView, PyResult, PyValue, TryFromObject, VirtualMachine, }; use crossbeam_utils::atomic::AtomicCell; diff --git a/stdlib/src/select.rs b/stdlib/src/select.rs index 360f0d6d4b..058fa242d6 100644 --- a/stdlib/src/select.rs +++ b/stdlib/src/select.rs @@ -261,7 +261,7 @@ mod decl { use super::*; use crate::vm::{ builtins::PyFloat, common::lock::PyMutex, convert::ToPyObject, function::OptionalArg, - stdlib::io::Fildes, AsPyObject, PyValue, + stdlib::io::Fildes, AsObject, PyValue, }; use libc::pollfd; use num_traits::ToPrimitive; diff --git a/stdlib/src/socket.rs b/stdlib/src/socket.rs index c8b3f873c0..52cc33391c 100644 --- a/stdlib/src/socket.rs +++ b/stdlib/src/socket.rs @@ -16,7 +16,7 @@ mod _socket { convert::{ToPyException, ToPyObject, TryFromBorrowedObject, TryFromObject}, function::{ArgBytesLike, ArgMemoryBuffer, FuncArgs, OptionalArg, OptionalOption}, utils::{Either, ToCString}, - AsPyObject, PyObjectRef, PyResult, PyValue, VirtualMachine, + AsObject, PyObjectRef, PyResult, PyValue, VirtualMachine, }; use crossbeam_utils::atomic::AtomicCell; use num_traits::ToPrimitive; diff --git a/vm/src/anystr.rs b/vm/src/anystr.rs index bcf209d691..4cd7a8f760 100644 --- a/vm/src/anystr.rs +++ b/vm/src/anystr.rs @@ -3,7 +3,7 @@ use crate::{ cformat::CFormatString, function::{single_or_tuple_any, OptionalOption}, protocol::PyIterIter, - AsPyObject, PyObjectRef, PyResult, TryFromObject, VirtualMachine, + AsObject, PyObjectRef, PyResult, TryFromObject, VirtualMachine, }; use num_traits::{cast::ToPrimitive, sign::Signed}; use std::str::FromStr; diff --git a/vm/src/builtins/asyncgenerator.rs b/vm/src/builtins/asyncgenerator.rs index 8ffed4ebd0..389070e6cf 100644 --- a/vm/src/builtins/asyncgenerator.rs +++ b/vm/src/builtins/asyncgenerator.rs @@ -7,7 +7,7 @@ use crate::{ protocol::PyIterReturn, pyclass::PyClassImpl, types::{Constructor, IterNext, IterNextIterable, Unconstructible}, - AsPyObject, PyContext, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine, + AsObject, PyContext, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine, }; use crossbeam_utils::atomic::AtomicCell; diff --git a/vm/src/builtins/builtinfunc.rs b/vm/src/builtins/builtinfunc.rs index af05e36193..d534c512f2 100644 --- a/vm/src/builtins/builtinfunc.rs +++ b/vm/src/builtins/builtinfunc.rs @@ -4,7 +4,7 @@ use crate::{ function::{FuncArgs, IntoPyNativeFunc, PyNativeFunc}, pyclass::PyClassImpl, types::{Callable, Constructor, GetDescriptor, Unconstructible}, - AsPyObject, PyContext, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine, + AsObject, PyContext, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine, }; use std::fmt; diff --git a/vm/src/builtins/bytearray.rs b/vm/src/builtins/bytearray.rs index 92e9ba80da..792e5cf6cc 100644 --- a/vm/src/builtins/bytearray.rs +++ b/vm/src/builtins/bytearray.rs @@ -33,7 +33,7 @@ use crate::{ IterNextIterable, Iterable, PyComparisonOp, Unconstructible, Unhashable, }, utils::Either, - AsPyObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult, + AsObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult, PyValue, TryFromBorrowedObject, TryFromObject, VirtualMachine, }; use bstr::ByteSlice; diff --git a/vm/src/builtins/bytes.rs b/vm/src/builtins/bytes.rs index 80467bea79..22b4ccc9d7 100644 --- a/vm/src/builtins/bytes.rs +++ b/vm/src/builtins/bytes.rs @@ -20,7 +20,7 @@ use crate::{ IterNextIterable, Iterable, PyComparisonOp, Unconstructible, }, utils::Either, - AsPyObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult, + AsObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult, PyValue, TryFromBorrowedObject, TryFromObject, VirtualMachine, }; use bstr::ByteSlice; diff --git a/vm/src/builtins/classmethod.rs b/vm/src/builtins/classmethod.rs index 09e6c3a8ed..ecbb4c0d1f 100644 --- a/vm/src/builtins/classmethod.rs +++ b/vm/src/builtins/classmethod.rs @@ -3,7 +3,7 @@ use crate::{ builtins::PyBoundMethod, pyclass::PyClassImpl, types::{Constructor, GetDescriptor}, - AsPyObject, PyContext, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine, + AsObject, PyContext, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine, }; /// classmethod(function) -> method diff --git a/vm/src/builtins/code.rs b/vm/src/builtins/code.rs index 25c0d3a559..a3e46f1acb 100644 --- a/vm/src/builtins/code.rs +++ b/vm/src/builtins/code.rs @@ -7,7 +7,7 @@ use crate::{ bytecode::{self, BorrowedConstant, Constant, ConstantBag}, function::FuncArgs, pyclass::{PyClassImpl, StaticType}, - AsPyObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine, + AsObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine, }; use num_traits::Zero; use std::{fmt, ops::Deref}; diff --git a/vm/src/builtins/complex.rs b/vm/src/builtins/complex.rs index c49684edf6..4032851baf 100644 --- a/vm/src/builtins/complex.rs +++ b/vm/src/builtins/complex.rs @@ -8,7 +8,7 @@ use crate::{ }, pyclass::PyClassImpl, types::{Comparable, Constructor, Hashable, PyComparisonOp}, - AsPyObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine, + AsObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine, }; use num_complex::Complex64; use num_traits::Zero; diff --git a/vm/src/builtins/coroutine.rs b/vm/src/builtins/coroutine.rs index c6859ba7d5..dc46e966fa 100644 --- a/vm/src/builtins/coroutine.rs +++ b/vm/src/builtins/coroutine.rs @@ -6,7 +6,7 @@ use crate::{ protocol::PyIterReturn, pyclass::PyClassImpl, types::{Constructor, IterNext, IterNextIterable, Unconstructible}, - AsPyObject, PyContext, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine, + AsObject, PyContext, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine, }; #[pyclass(module = false, name = "coroutine")] diff --git a/vm/src/builtins/dict.rs b/vm/src/builtins/dict.rs index 347983d5c1..b84e6736b6 100644 --- a/vm/src/builtins/dict.rs +++ b/vm/src/builtins/dict.rs @@ -21,7 +21,7 @@ use crate::{ IterNextIterable, Iterable, PyComparisonOp, Unconstructible, Unhashable, }, vm::{ReprGuard, VirtualMachine}, - AsPyObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, + AsObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, TryFromObject, }; use rustpython_common::lock::PyMutex; diff --git a/vm/src/builtins/enumerate.rs b/vm/src/builtins/enumerate.rs index f9e2abe53b..3c6318c512 100644 --- a/vm/src/builtins/enumerate.rs +++ b/vm/src/builtins/enumerate.rs @@ -6,7 +6,7 @@ use crate::{ protocol::{PyIter, PyIterReturn}, pyclass::PyClassImpl, types::{Constructor, IterNext, IterNextIterable}, - AsPyObject, PyContext, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine, + AsObject, PyContext, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine, }; use num_bigint::BigInt; use num_traits::Zero; diff --git a/vm/src/builtins/float.rs b/vm/src/builtins/float.rs index 34785412b9..c5fc2afde7 100644 --- a/vm/src/builtins/float.rs +++ b/vm/src/builtins/float.rs @@ -10,7 +10,7 @@ use crate::{ }, pyclass::PyClassImpl, types::{Comparable, Constructor, Hashable, PyComparisonOp}, - AsPyObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromBorrowedObject, + AsObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromBorrowedObject, TryFromObject, VirtualMachine, }; use num_bigint::{BigInt, ToBigInt}; diff --git a/vm/src/builtins/frame.rs b/vm/src/builtins/frame.rs index c38b4c9b38..91676c68bd 100644 --- a/vm/src/builtins/frame.rs +++ b/vm/src/builtins/frame.rs @@ -7,7 +7,7 @@ use crate::{ frame::{Frame, FrameRef}, pyclass::PyClassImpl, types::{Constructor, Unconstructible}, - AsPyObject, PyContext, PyObjectRef, PyObjectWrap, PyRef, PyResult, VirtualMachine, + AsObject, PyContext, PyObjectRef, PyObjectWrap, PyRef, PyResult, VirtualMachine, }; pub fn init(context: &PyContext) { diff --git a/vm/src/builtins/function.rs b/vm/src/builtins/function.rs index 934aa78add..cde240ad33 100644 --- a/vm/src/builtins/function.rs +++ b/vm/src/builtins/function.rs @@ -14,7 +14,7 @@ use crate::{ pyclass::PyClassImpl, scope::Scope, types::{Callable, Comparable, Constructor, GetAttr, GetDescriptor, PyComparisonOp}, - AsPyObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine, + AsObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine, }; #[cfg(feature = "jit")] use crate::{common::lock::OnceCell, convert::ToPyObject}; diff --git a/vm/src/builtins/function/jitfunc.rs b/vm/src/builtins/function/jitfunc.rs index 1c299e519c..a41c0e109c 100644 --- a/vm/src/builtins/function/jitfunc.rs +++ b/vm/src/builtins/function/jitfunc.rs @@ -3,7 +3,7 @@ use crate::{ bytecode::CodeFlags, convert::ToPyObject, function::FuncArgs, - AsPyObject, PyObject, PyObjectRef, PyResult, TryFromObject, VirtualMachine, + AsObject, PyObject, PyObjectRef, PyResult, TryFromObject, VirtualMachine, }; use num_traits::ToPrimitive; use rustpython_jit::{AbiValue, Args, CompiledCode, JitArgumentError, JitType}; diff --git a/vm/src/builtins/generator.rs b/vm/src/builtins/generator.rs index 7528981b79..41f3e4ace1 100644 --- a/vm/src/builtins/generator.rs +++ b/vm/src/builtins/generator.rs @@ -10,7 +10,7 @@ use crate::{ protocol::PyIterReturn, pyclass::PyClassImpl, types::{Constructor, IterNext, IterNextIterable, Unconstructible}, - AsPyObject, PyContext, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine, + AsObject, PyContext, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine, }; #[pyclass(module = false, name = "generator")] diff --git a/vm/src/builtins/genericalias.rs b/vm/src/builtins/genericalias.rs index 1e937013ae..e3b81f8fc7 100644 --- a/vm/src/builtins/genericalias.rs +++ b/vm/src/builtins/genericalias.rs @@ -6,7 +6,7 @@ use crate::{ protocol::PyMappingMethods, pyclass::PyClassImpl, types::{AsMapping, Callable, Comparable, Constructor, GetAttr, Hashable, PyComparisonOp}, - AsPyObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, + AsObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, TryFromObject, VirtualMachine, }; use std::fmt; diff --git a/vm/src/builtins/getset.rs b/vm/src/builtins/getset.rs index a781519048..935b4198bf 100644 --- a/vm/src/builtins/getset.rs +++ b/vm/src/builtins/getset.rs @@ -8,7 +8,7 @@ use crate::{ pyclass::PyClassImpl, pyobject::PyThreadingConstraint, types::{Constructor, GetDescriptor, Unconstructible}, - AsPyObject, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, VirtualMachine, + AsObject, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, VirtualMachine, }; pub type PyGetterFunc = Box PyResult)>; diff --git a/vm/src/builtins/int.rs b/vm/src/builtins/int.rs index d32ddd03e7..07c19b85f0 100644 --- a/vm/src/builtins/int.rs +++ b/vm/src/builtins/int.rs @@ -7,7 +7,7 @@ use crate::{ function::{ArgIntoBool, OptionalArg, OptionalOption, PyArithmeticValue, PyComparisonValue}, pyclass::PyClassImpl, types::{Comparable, Constructor, Hashable, PyComparisonOp}, - AsPyObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromBorrowedObject, + AsObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromBorrowedObject, VirtualMachine, }; use bstr::ByteSlice; diff --git a/vm/src/builtins/list.rs b/vm/src/builtins/list.rs index 6977fe3042..7f3f22626b 100644 --- a/vm/src/builtins/list.rs +++ b/vm/src/builtins/list.rs @@ -16,7 +16,7 @@ use crate::{ }, utils::collection_repr, vm::{ReprGuard, VirtualMachine}, - AsPyObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult, + AsObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult, PyValue, }; use std::{borrow::Cow, fmt, ops::DerefMut}; diff --git a/vm/src/builtins/mappingproxy.rs b/vm/src/builtins/mappingproxy.rs index 1eb7d46849..c7404c5762 100644 --- a/vm/src/builtins/mappingproxy.rs +++ b/vm/src/builtins/mappingproxy.rs @@ -7,7 +7,7 @@ use crate::{ protocol::{PyMapping, PyMappingMethods, PySequence, PySequenceMethods}, pyclass::PyClassImpl, types::{AsMapping, AsSequence, Constructor, Iterable}, - AsPyObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, + AsObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, VirtualMachine, }; diff --git a/vm/src/builtins/memory.rs b/vm/src/builtins/memory.rs index b8228c6219..6b4c64f788 100644 --- a/vm/src/builtins/memory.rs +++ b/vm/src/builtins/memory.rs @@ -19,7 +19,7 @@ use crate::{ sliceable::wrap_index, types::{AsBuffer, AsMapping, AsSequence, Comparable, Constructor, Hashable, PyComparisonOp}, utils::Either, - AsPyObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult, + AsObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult, PyValue, TryFromBorrowedObject, TryFromObject, VirtualMachine, }; use crossbeam_utils::atomic::AtomicCell; diff --git a/vm/src/builtins/module.rs b/vm/src/builtins/module.rs index c199cdc72f..15396d7d61 100644 --- a/vm/src/builtins/module.rs +++ b/vm/src/builtins/module.rs @@ -1,7 +1,7 @@ use super::pystr::IntoPyStrRef; use super::{PyDictRef, PyStr, PyStrRef, PyTypeRef}; use crate::{ - convert::ToPyObject, function::FuncArgs, pyclass::PyClassImpl, types::GetAttr, AsPyObject, + convert::ToPyObject, function::FuncArgs, pyclass::PyClassImpl, types::GetAttr, AsObject, PyContext, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, VirtualMachine, }; diff --git a/vm/src/builtins/namespace.rs b/vm/src/builtins/namespace.rs index 441b60d465..85cae053a3 100644 --- a/vm/src/builtins/namespace.rs +++ b/vm/src/builtins/namespace.rs @@ -5,7 +5,7 @@ use crate::{ pyclass::PyClassImpl, types::{Comparable, Constructor, PyComparisonOp}, vm::ReprGuard, - AsPyObject, PyContext, PyObject, PyRef, PyResult, PyValue, VirtualMachine, + AsObject, PyContext, PyObject, PyRef, PyResult, PyValue, VirtualMachine, }; /// A simple attribute-based namespace. diff --git a/vm/src/builtins/object.rs b/vm/src/builtins/object.rs index 71044ef7a2..43d71acf34 100644 --- a/vm/src/builtins/object.rs +++ b/vm/src/builtins/object.rs @@ -5,7 +5,7 @@ use crate::{ pyclass::PyClassImpl, types::PyComparisonOp, utils::Either, - AsPyObject, PyContext, PyObject, PyObjectRef, PyResult, PyValue, VirtualMachine, + AsObject, PyContext, PyObject, PyObjectRef, PyResult, PyValue, VirtualMachine, }; /// object() diff --git a/vm/src/builtins/property.rs b/vm/src/builtins/property.rs index e21cb9ad9a..1353e7f2e6 100644 --- a/vm/src/builtins/property.rs +++ b/vm/src/builtins/property.rs @@ -4,7 +4,7 @@ use super::PyTypeRef; use crate::common::lock::PyRwLock; use crate::{ - function::FuncArgs, pyclass::PyClassImpl, types::GetDescriptor, AsPyObject, PyContext, + function::FuncArgs, pyclass::PyClassImpl, types::GetDescriptor, AsObject, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, VirtualMachine, }; diff --git a/vm/src/builtins/pybool.rs b/vm/src/builtins/pybool.rs index a4e0850287..d522b415ce 100644 --- a/vm/src/builtins/pybool.rs +++ b/vm/src/builtins/pybool.rs @@ -1,8 +1,7 @@ use super::{PyInt, PyStrRef, PyTypeRef}; use crate::{ - convert::ToPyObject, function::OptionalArg, pyclass::PyClassImpl, types::Constructor, - AsPyObject, PyContext, PyObject, PyObjectRef, PyResult, PyValue, TryFromBorrowedObject, - VirtualMachine, + convert::ToPyObject, function::OptionalArg, pyclass::PyClassImpl, types::Constructor, AsObject, + PyContext, PyObject, PyObjectRef, PyResult, PyValue, TryFromBorrowedObject, VirtualMachine, }; use num_bigint::Sign; use num_traits::Zero; diff --git a/vm/src/builtins/pystr.rs b/vm/src/builtins/pystr.rs index 70935116e1..b79a7b0946 100644 --- a/vm/src/builtins/pystr.rs +++ b/vm/src/builtins/pystr.rs @@ -16,7 +16,7 @@ use crate::{ AsMapping, AsSequence, Comparable, Constructor, Hashable, IterNext, IterNextIterable, Iterable, PyComparisonOp, Unconstructible, }, - AsPyObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, + AsObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, TryFromBorrowedObject, VirtualMachine, }; use ascii::{AsciiStr, AsciiString}; diff --git a/vm/src/builtins/pysuper.rs b/vm/src/builtins/pysuper.rs index 31b9052f71..064a96ac23 100644 --- a/vm/src/builtins/pysuper.rs +++ b/vm/src/builtins/pysuper.rs @@ -8,7 +8,7 @@ use crate::{ function::OptionalArg, pyclass::PyClassImpl, types::{Constructor, GetAttr, GetDescriptor}, - AsPyObject, PyContext, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine, + AsObject, PyContext, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine, }; #[pyclass(module = false, name = "super")] diff --git a/vm/src/builtins/pytype.rs b/vm/src/builtins/pytype.rs index bb6f11c6d7..a0eb3b3d2a 100644 --- a/vm/src/builtins/pytype.rs +++ b/vm/src/builtins/pytype.rs @@ -11,7 +11,7 @@ use crate::{ function::{FuncArgs, KwArgs, OptionalArg}, pyclass::{PyClassImpl, StaticType}, types::{Callable, GetAttr, PyTypeFlags, PyTypeSlots, SetAttr}, - AsPyObject, PyContext, PyObjectRef, PyObjectWeak, PyRef, PyResult, PyValue, VirtualMachine, + AsObject, PyContext, PyObjectRef, PyObjectWeak, PyRef, PyResult, PyValue, VirtualMachine, }; use itertools::Itertools; use std::{ diff --git a/vm/src/builtins/pyunion.rs b/vm/src/builtins/pyunion.rs index 248a1415e8..49390418bb 100644 --- a/vm/src/builtins/pyunion.rs +++ b/vm/src/builtins/pyunion.rs @@ -7,7 +7,7 @@ use crate::{ protocol::PyMappingMethods, pyclass::PyClassImpl, types::{AsMapping, Comparable, GetAttr, Hashable, Iterable, PyComparisonOp}, - AsPyObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, + AsObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, TryFromObject, VirtualMachine, }; use std::fmt; diff --git a/vm/src/builtins/range.rs b/vm/src/builtins/range.rs index 79cb88b603..1e7936b18c 100644 --- a/vm/src/builtins/range.rs +++ b/vm/src/builtins/range.rs @@ -9,7 +9,7 @@ use crate::{ AsMapping, AsSequence, Comparable, Constructor, Hashable, IterNext, IterNextIterable, Iterable, PyComparisonOp, Unconstructible, }, - AsPyObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, + AsObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, TryFromObject, VirtualMachine, }; use crossbeam_utils::atomic::AtomicCell; diff --git a/vm/src/builtins/set.rs b/vm/src/builtins/set.rs index 22c8a76674..4413a5b4fe 100644 --- a/vm/src/builtins/set.rs +++ b/vm/src/builtins/set.rs @@ -17,7 +17,7 @@ use crate::{ }, utils::collection_repr, vm::{ReprGuard, VirtualMachine}, - AsPyObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, + AsObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, }; use std::borrow::Cow; use std::{fmt, ops::Deref}; diff --git a/vm/src/builtins/singletons.rs b/vm/src/builtins/singletons.rs index c7d4a53c36..131d6a740e 100644 --- a/vm/src/builtins/singletons.rs +++ b/vm/src/builtins/singletons.rs @@ -1,6 +1,6 @@ use super::PyTypeRef; use crate::{ - convert::ToPyObject, pyclass::PyClassImpl, types::Constructor, AsPyObject, PyContext, + convert::ToPyObject, pyclass::PyClassImpl, types::Constructor, AsObject, PyContext, PyObjectRef, PyResult, PyValue, VirtualMachine, }; diff --git a/vm/src/builtins/slice.rs b/vm/src/builtins/slice.rs index c229805741..3ba125053d 100644 --- a/vm/src/builtins/slice.rs +++ b/vm/src/builtins/slice.rs @@ -5,7 +5,7 @@ use crate::{ function::{FuncArgs, OptionalArg, PyComparisonValue}, pyclass::PyClassImpl, types::{Comparable, Constructor, Hashable, PyComparisonOp, Unhashable}, - AsPyObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine, + AsObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine, }; use num_bigint::{BigInt, ToBigInt}; use num_traits::{One, Signed, ToPrimitive, Zero}; diff --git a/vm/src/builtins/tuple.rs b/vm/src/builtins/tuple.rs index d52ff2faa9..6aa60d28ee 100644 --- a/vm/src/builtins/tuple.rs +++ b/vm/src/builtins/tuple.rs @@ -14,7 +14,7 @@ use crate::{ }, utils::collection_repr, vm::{ReprGuard, VirtualMachine}, - AsPyObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, + AsObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, }; use std::{borrow::Cow, fmt, marker::PhantomData}; diff --git a/vm/src/builtins/weakref.rs b/vm/src/builtins/weakref.rs index 0db91cb57a..2425ec3f0f 100644 --- a/vm/src/builtins/weakref.rs +++ b/vm/src/builtins/weakref.rs @@ -7,7 +7,7 @@ use crate::{ function::OptionalArg, pyclass::PyClassImpl, types::{Callable, Comparable, Constructor, Hashable, PyComparisonOp}, - AsPyObject, PyContext, PyObject, PyObjectRef, PyObjectWrap, PyRef, PyResult, PyValue, + AsObject, PyContext, PyObject, PyObjectRef, PyObjectWrap, PyRef, PyResult, PyValue, VirtualMachine, }; diff --git a/vm/src/builtins/zip.rs b/vm/src/builtins/zip.rs index 3e56514a35..fa63cbbb72 100644 --- a/vm/src/builtins/zip.rs +++ b/vm/src/builtins/zip.rs @@ -5,7 +5,7 @@ use crate::{ protocol::{PyIter, PyIterReturn}, pyclass::PyClassImpl, types::{Constructor, IterNext, IterNextIterable}, - AsPyObject, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, VirtualMachine, + AsObject, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, VirtualMachine, }; use rustpython_common::atomic::{self, PyAtomic, Radium}; diff --git a/vm/src/bytesinner.rs b/vm/src/bytesinner.rs index ed50c56f4d..90d99ee04a 100644 --- a/vm/src/bytesinner.rs +++ b/vm/src/bytesinner.rs @@ -9,7 +9,7 @@ use crate::{ sequence::{SequenceMutOp, SequenceOp}, types::PyComparisonOp, utils::Either, - AsPyObject, PyObject, PyObjectRef, PyResult, PyValue, TryFromBorrowedObject, VirtualMachine, + AsObject, PyObject, PyObjectRef, PyResult, PyValue, TryFromBorrowedObject, VirtualMachine, }; use bstr::ByteSlice; use itertools::Itertools; diff --git a/vm/src/cformat.rs b/vm/src/cformat.rs index 7c7e865aee..19a87ab2f8 100644 --- a/vm/src/cformat.rs +++ b/vm/src/cformat.rs @@ -7,7 +7,7 @@ use crate::{ function::ArgIntoFloat, protocol::PyBuffer, stdlib::builtins, - AsPyObject, PyObjectRef, PyResult, TryFromBorrowedObject, TryFromObject, VirtualMachine, + AsObject, PyObjectRef, PyResult, TryFromBorrowedObject, TryFromObject, VirtualMachine, }; use itertools::Itertools; use num_bigint::{BigInt, Sign}; diff --git a/vm/src/codecs.rs b/vm/src/codecs.rs index 9b8b0f5354..3794d0934a 100644 --- a/vm/src/codecs.rs +++ b/vm/src/codecs.rs @@ -2,7 +2,7 @@ use crate::{ builtins::{PyBaseExceptionRef, PyBytesRef, PyStr, PyStrRef, PyTuple, PyTupleRef}, common::{ascii, lock::PyRwLock}, convert::ToPyObject, - AsPyObject, PyContext, PyObject, PyObjectRef, PyResult, PyValue, TryFromObject, VirtualMachine, + AsObject, PyContext, PyObject, PyObjectRef, PyResult, PyValue, TryFromObject, VirtualMachine, }; use std::{borrow::Cow, collections::HashMap, fmt::Write, ops::Range}; diff --git a/vm/src/convert/transmute_from.rs b/vm/src/convert/transmute_from.rs index e6c1eff7a9..94e0759393 100644 --- a/vm/src/convert/transmute_from.rs +++ b/vm/src/convert/transmute_from.rs @@ -1,5 +1,5 @@ use crate::{ - pyobject::{AsPyObject, PyObject, PyRef, PyResult, PyValue}, + pyobject::{AsObject, PyObject, PyRef, PyResult, PyValue}, vm::VirtualMachine, }; diff --git a/vm/src/convert/try_from.rs b/vm/src/convert/try_from.rs index 13b0f587f2..6fd30b3a6a 100644 --- a/vm/src/convert/try_from.rs +++ b/vm/src/convert/try_from.rs @@ -1,5 +1,5 @@ use crate::{ - pyobject::{AsPyObject, PyObject, PyObjectRef, PyRef, PyResult, PyValue}, + pyobject::{AsObject, PyObject, PyObjectRef, PyRef, PyResult, PyValue}, vm::VirtualMachine, }; diff --git a/vm/src/coroutine.rs b/vm/src/coroutine.rs index 4217b97ecd..801fe5c834 100644 --- a/vm/src/coroutine.rs +++ b/vm/src/coroutine.rs @@ -3,7 +3,7 @@ use crate::{ common::lock::PyMutex, frame::{ExecutionResult, FrameRef}, protocol::PyIterReturn, - AsPyObject, PyObject, PyObjectRef, PyResult, VirtualMachine, + AsObject, PyObject, PyObjectRef, PyResult, VirtualMachine, }; use crossbeam_utils::atomic::AtomicCell; diff --git a/vm/src/dictdatatype.rs b/vm/src/dictdatatype.rs index 994cf8161e..733ad57c13 100644 --- a/vm/src/dictdatatype.rs +++ b/vm/src/dictdatatype.rs @@ -10,7 +10,7 @@ use crate::common::{ use crate::{ builtins::{PyInt, PyStr, PyStrRef}, convert::ToPyObject, - AsPyObject, PyObject, PyObjectRef, PyRefExact, PyResult, VirtualMachine, + AsObject, PyObject, PyObjectRef, PyRefExact, PyResult, VirtualMachine, }; use num_traits::ToPrimitive; use std::{fmt, mem::size_of, ops::ControlFlow}; diff --git a/vm/src/exceptions.rs b/vm/src/exceptions.rs index 40d3ab2785..5c4067a0d8 100644 --- a/vm/src/exceptions.rs +++ b/vm/src/exceptions.rs @@ -10,7 +10,7 @@ use crate::{ pyclass::{PyClassImpl, StaticType}, stdlib::sys, suggestion::offer_suggestions, - AsPyObject, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, VirtualMachine, + AsObject, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, VirtualMachine, }; use crossbeam_utils::atomic::AtomicCell; use itertools::Itertools; diff --git a/vm/src/format.rs b/vm/src/format.rs index 38f4ebfa2f..b26af01108 100644 --- a/vm/src/format.rs +++ b/vm/src/format.rs @@ -4,7 +4,7 @@ use crate::{ convert::ToPyException, function::FuncArgs, stdlib::builtins, - AsPyObject, PyObject, PyObjectRef, PyResult, VirtualMachine, + AsObject, PyObject, PyObjectRef, PyResult, VirtualMachine, }; use itertools::{Itertools, PeekingNext}; use num_bigint::{BigInt, Sign}; diff --git a/vm/src/frame.rs b/vm/src/frame.rs index 29d20fceea..b023ea90b0 100644 --- a/vm/src/frame.rs +++ b/vm/src/frame.rs @@ -16,7 +16,7 @@ use crate::{ scope::Scope, stdlib::builtins, types::PyComparisonOp, - AsPyObject, PyMethod, PyObject, PyObjectRef, PyObjectWrap, PyRef, PyResult, PyValue, + AsObject, PyMethod, PyObject, PyObjectRef, PyObjectWrap, PyRef, PyResult, PyValue, TryFromObject, VirtualMachine, }; use indexmap::IndexMap; diff --git a/vm/src/function.rs b/vm/src/function.rs index 19ca84036a..a7c9eea28a 100644 --- a/vm/src/function.rs +++ b/vm/src/function.rs @@ -7,7 +7,7 @@ use crate::{ builtins::{PyBaseExceptionRef, PyTupleRef, PyTypeRef}, convert::{ToPyObject, ToPyResult}, pyobject::PyThreadingConstraint, - AsPyObject, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, VirtualMachine, + AsObject, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, VirtualMachine, }; use indexmap::IndexMap; use itertools::Itertools; diff --git a/vm/src/function/argument.rs b/vm/src/function/argument.rs index 0decb6f474..59693fff49 100644 --- a/vm/src/function/argument.rs +++ b/vm/src/function/argument.rs @@ -3,7 +3,7 @@ use crate::{ builtins::{iter::PySequenceIterator, PyDict, PyDictRef}, convert::ToPyObject, protocol::{PyIter, PyIterIter, PyMapping, PyMappingMethods}, - AsPyObject, PyObject, PyObjectRef, PyObjectWrap, PyResult, PyValue, TryFromObject, + AsObject, PyObject, PyObjectRef, PyObjectWrap, PyResult, PyValue, TryFromObject, VirtualMachine, }; use std::{borrow::Borrow, marker::PhantomData}; diff --git a/vm/src/function/arithmetic.rs b/vm/src/function/arithmetic.rs index 4229328c0a..86dad0d29b 100644 --- a/vm/src/function/arithmetic.rs +++ b/vm/src/function/arithmetic.rs @@ -1,6 +1,6 @@ use crate::{ convert::{ToPyObject, TryFromObject}, - pyobject::{AsPyObject, PyObjectRef, PyResult}, + pyobject::{AsObject, PyObjectRef, PyResult}, VirtualMachine, }; diff --git a/vm/src/function/number.rs b/vm/src/function/number.rs index 9636801624..06f0767155 100644 --- a/vm/src/function/number.rs +++ b/vm/src/function/number.rs @@ -1,4 +1,4 @@ -use crate::{AsPyObject, PyObjectRef, PyResult, TryFromObject, VirtualMachine}; +use crate::{AsObject, PyObjectRef, PyResult, TryFromObject, VirtualMachine}; use num_complex::Complex64; /// A Python complex-like object. diff --git a/vm/src/import.rs b/vm/src/import.rs index 14533434b1..0b753d6916 100644 --- a/vm/src/import.rs +++ b/vm/src/import.rs @@ -8,7 +8,7 @@ use crate::{ scope::Scope, version::get_git_revision, vm::{InitParameter, VirtualMachine}, - AsPyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, + AsObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, }; use rand::Rng; diff --git a/vm/src/lib.rs b/vm/src/lib.rs index 702cc1f003..b1918ea281 100644 --- a/vm/src/lib.rs +++ b/vm/src/lib.rs @@ -89,7 +89,7 @@ mod pyobject { pub use self::convert::{TryFromBorrowedObject, TryFromObject}; // pyobject items pub use self::pyobject::{ - AsPyObject, PyContext, PyMethod, PyObjectWrap, PyRefExact, PyResult, PyStructSequence, PyValue, + AsObject, PyContext, PyMethod, PyObjectWrap, PyRefExact, PyResult, PyStructSequence, PyValue, }; // pyobjectrc items pub use self::pyobject::{PyObject, PyObjectRef, PyObjectView, PyObjectWeak, PyRef, PyWeakRef}; diff --git a/vm/src/macros.rs b/vm/src/macros.rs index 5945682730..d1a2a8ba5d 100644 --- a/vm/src/macros.rs +++ b/vm/src/macros.rs @@ -49,7 +49,7 @@ macro_rules! py_namespace { { let namespace = $crate::builtins::PyNamespace::new_ref(&$vm.ctx); $( - $vm.__module_set_attr($crate::pyobject::AsPyObject::as_object(&namespace), $name, $value).unwrap(); + $vm.__module_set_attr($crate::pyobject::AsObject::as_object(&namespace), $name, $value).unwrap(); )* namespace } diff --git a/vm/src/protocol/buffer.rs b/vm/src/protocol/buffer.rs index 00e6b410ed..7a871beed6 100644 --- a/vm/src/protocol/buffer.rs +++ b/vm/src/protocol/buffer.rs @@ -9,7 +9,7 @@ use crate::{ pyobject::PyObjectPayload, sliceable::wrap_index, types::{Constructor, Unconstructible}, - AsPyObject, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult, PyValue, + AsObject, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult, PyValue, TryFromBorrowedObject, VirtualMachine, }; use itertools::Itertools; diff --git a/vm/src/protocol/iter.rs b/vm/src/protocol/iter.rs index 7559fbead7..d9b347249a 100644 --- a/vm/src/protocol/iter.rs +++ b/vm/src/protocol/iter.rs @@ -1,7 +1,7 @@ use crate::{ builtins::iter::PySequenceIterator, convert::{ToPyObject, ToPyResult}, - AsPyObject, PyObject, PyObjectRef, PyObjectWrap, PyResult, PyValue, TryFromObject, + AsObject, PyObject, PyObjectRef, PyObjectWrap, PyResult, PyValue, TryFromObject, VirtualMachine, }; use std::borrow::Borrow; diff --git a/vm/src/protocol/mapping.rs b/vm/src/protocol/mapping.rs index fec685ddad..d78bda4598 100644 --- a/vm/src/protocol/mapping.rs +++ b/vm/src/protocol/mapping.rs @@ -5,7 +5,7 @@ use crate::{ }, common::lock::OnceCell, convert::ToPyResult, - AsPyObject, PyObject, PyObjectRef, PyResult, VirtualMachine, + AsObject, PyObject, PyObjectRef, PyResult, VirtualMachine, }; // Mapping protocol @@ -91,13 +91,13 @@ impl PyMapping<'_> { })? } - pub fn subscript(&self, needle: &impl AsPyObject, vm: &VirtualMachine) -> PyResult { + pub fn subscript(&self, needle: &impl AsObject, vm: &VirtualMachine) -> PyResult { self._subscript(needle.as_object(), vm) } pub fn ass_subscript( &self, - needle: &impl AsPyObject, + needle: &impl AsObject, value: Option, vm: &VirtualMachine, ) -> PyResult<()> { diff --git a/vm/src/protocol/object.rs b/vm/src/protocol/object.rs index eca1109aa7..c4cb47a1d6 100644 --- a/vm/src/protocol/object.rs +++ b/vm/src/protocol/object.rs @@ -14,7 +14,7 @@ use crate::{ protocol::{PyIter, PyMapping, PySequence}, types::{Constructor, PyComparisonOp}, utils::Either, - AsPyObject, PyObject, PyObjectRef, PyResult, TryFromObject, VirtualMachine, + AsObject, PyObject, PyObjectRef, PyResult, TryFromObject, VirtualMachine, }; // RustPython doesn't need these items diff --git a/vm/src/protocol/sequence.rs b/vm/src/protocol/sequence.rs index 063ccb9d08..ca055d5c28 100644 --- a/vm/src/protocol/sequence.rs +++ b/vm/src/protocol/sequence.rs @@ -4,7 +4,7 @@ use crate::{ convert::ToPyObject, function::PyArithmeticValue, protocol::PyMapping, - AsPyObject, PyObject, PyObjectRef, PyResult, PyValue, VirtualMachine, + AsObject, PyObject, PyObjectRef, PyResult, PyValue, VirtualMachine, }; use itertools::Itertools; use std::{ diff --git a/vm/src/py_serde.rs b/vm/src/py_serde.rs index 2ec878cd56..5d2c72149f 100644 --- a/vm/src/py_serde.rs +++ b/vm/src/py_serde.rs @@ -4,7 +4,7 @@ use serde::de::{DeserializeSeed, Visitor}; use serde::ser::{Serialize, SerializeMap, SerializeSeq}; use crate::builtins::{dict::PyDictRef, float, int, list::PyList, pybool, tuple::PyTuple, PyStr}; -use crate::{AsPyObject, PyObject, PyObjectRef, VirtualMachine}; +use crate::{AsObject, PyObject, PyObjectRef, VirtualMachine}; #[inline] pub fn serialize( diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index e916842447..e127997abe 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -398,7 +398,7 @@ impl ToPyObject for PyRefExact { } } -pub trait AsPyObject +pub trait AsObject where Self: Borrow, { @@ -415,7 +415,7 @@ where #[inline(always)] fn is(&self, other: &T) -> bool where - T: AsPyObject, + T: AsObject, { self.get_id() == other.get_id() } @@ -437,7 +437,7 @@ where } } -impl AsPyObject for T where T: Borrow {} +impl AsObject for T where T: Borrow {} impl PyObject { #[inline(always)] @@ -687,7 +687,7 @@ pub trait PyStructSequence: StaticType + PyClassImpl + Sized + 'static { pub trait PyObjectWrap where - Self: AsPyObject, + Self: AsObject, { fn into_object(self) -> PyObjectRef; } diff --git a/vm/src/pyobjectrc.rs b/vm/src/pyobjectrc.rs index d5ded55702..a66fa1ef58 100644 --- a/vm/src/pyobjectrc.rs +++ b/vm/src/pyobjectrc.rs @@ -3,7 +3,7 @@ use crate::common::linked_list::{Link, LinkedList, Pointers}; use crate::common::lock::{PyMutex, PyMutexGuard, PyRwLock}; use crate::common::refcount::RefCount; use crate::{ - _pyobject::{AsPyObject, PyObjectPayload, PyObjectWrap, PyResult}, + _pyobject::{AsObject, PyObjectPayload, PyObjectWrap, PyResult}, builtins::{PyBaseExceptionRef, PyDictRef, PyTypeRef}, vm::VirtualMachine, }; diff --git a/vm/src/sequence.rs b/vm/src/sequence.rs index dc3784d84e..83af45be77 100644 --- a/vm/src/sequence.rs +++ b/vm/src/sequence.rs @@ -3,7 +3,7 @@ use crate::{ types::{richcompare_wrapper, PyComparisonOp, RichCompareFunc}, utils::Either, vm::VirtualMachine, - AsPyObject, PyObject, PyObjectRef, PyResult, + AsObject, PyObject, PyObjectRef, PyResult, }; use itertools::Itertools; use optional::Optioned; diff --git a/vm/src/sliceable.rs b/vm/src/sliceable.rs index b02bb516be..c3b17acd72 100644 --- a/vm/src/sliceable.rs +++ b/vm/src/sliceable.rs @@ -7,7 +7,7 @@ use crate::{ int::PyInt, slice::{PySlice, SaturatedSliceIter}, }, - AsPyObject, PyObject, PyResult, TryFromBorrowedObject, VirtualMachine, + AsObject, PyObject, PyResult, TryFromBorrowedObject, VirtualMachine, }; pub trait SliceableSequenceMutOp { diff --git a/vm/src/stdlib/ast.rs b/vm/src/stdlib/ast.rs index 67d896de05..a6e4e7cd5f 100644 --- a/vm/src/stdlib/ast.rs +++ b/vm/src/stdlib/ast.rs @@ -8,7 +8,7 @@ mod gen; use crate::{ builtins::{self, PyStrRef, PyTypeRef}, pyclass::{PyClassImpl, StaticType}, - AsPyObject, PyContext, PyObject, PyObjectRef, PyResult, PyValue, TryFromObject, VirtualMachine, + AsObject, PyContext, PyObject, PyObjectRef, PyResult, PyValue, TryFromObject, VirtualMachine, }; use num_complex::Complex64; use num_traits::{ToPrimitive, Zero}; @@ -21,7 +21,7 @@ use rustpython_parser::parser; #[pymodule] mod _ast { use crate::{ - builtins::PyStrRef, function::FuncArgs, AsPyObject, PyObjectRef, PyResult, PyValue, + builtins::PyStrRef, function::FuncArgs, AsObject, PyObjectRef, PyResult, PyValue, VirtualMachine, }; #[pyattr] diff --git a/vm/src/stdlib/atexit.rs b/vm/src/stdlib/atexit.rs index 80bd83094b..5a442874af 100644 --- a/vm/src/stdlib/atexit.rs +++ b/vm/src/stdlib/atexit.rs @@ -3,7 +3,7 @@ pub(crate) use atexit::make_module; #[pymodule] mod atexit { - use crate::{function::FuncArgs, AsPyObject, PyObjectRef, PyResult, VirtualMachine}; + use crate::{function::FuncArgs, AsObject, PyObjectRef, PyResult, VirtualMachine}; #[pyfunction] fn register(func: PyObjectRef, args: FuncArgs, vm: &VirtualMachine) -> PyObjectRef { diff --git a/vm/src/stdlib/builtins.rs b/vm/src/stdlib/builtins.rs index 6a0c2f3642..a2ec713355 100644 --- a/vm/src/stdlib/builtins.rs +++ b/vm/src/stdlib/builtins.rs @@ -33,7 +33,7 @@ mod builtins { stdlib::sys, types::PyComparisonOp, utils::Either, - AsPyObject, PyObject, PyObjectRef, PyObjectWrap, PyRef, PyResult, PyValue, TryFromObject, + AsObject, PyObject, PyObjectRef, PyObjectWrap, PyRef, PyResult, PyValue, TryFromObject, VirtualMachine, }; use num_traits::{Signed, ToPrimitive, Zero}; diff --git a/vm/src/stdlib/codecs.rs b/vm/src/stdlib/codecs.rs index 484e1dda52..0702226e41 100644 --- a/vm/src/stdlib/codecs.rs +++ b/vm/src/stdlib/codecs.rs @@ -7,7 +7,7 @@ mod _codecs { builtins::{PyBaseExceptionRef, PyBytes, PyBytesRef, PyStr, PyStrRef, PyTuple}, codecs, function::{ArgBytesLike, FuncArgs}, - AsPyObject, PyObject, PyObjectRef, PyResult, TryFromBorrowedObject, VirtualMachine, + AsObject, PyObject, PyObjectRef, PyResult, TryFromBorrowedObject, VirtualMachine, }; use std::ops::Range; diff --git a/vm/src/stdlib/collections.rs b/vm/src/stdlib/collections.rs index 11252aa9eb..54912b2cec 100644 --- a/vm/src/stdlib/collections.rs +++ b/vm/src/stdlib/collections.rs @@ -19,7 +19,7 @@ mod _collections { }, utils::collection_repr, vm::ReprGuard, - AsPyObject, PyObject, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine, + AsObject, PyObject, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine, }; use crossbeam_utils::atomic::AtomicCell; use std::cmp::max; diff --git a/vm/src/stdlib/io.rs b/vm/src/stdlib/io.rs index 2875c96ff8..a11ccf9e42 100644 --- a/vm/src/stdlib/io.rs +++ b/vm/src/stdlib/io.rs @@ -88,7 +88,7 @@ mod _io { types::{Constructor, Destructor, IterNext, Iterable}, utils::Either, vm::{ReprGuard, VirtualMachine}, - AsPyObject, PyContext, PyObject, PyObjectRef, PyObjectWrap, PyRef, PyResult, PyValue, + AsObject, PyContext, PyObject, PyObjectRef, PyObjectWrap, PyRef, PyResult, PyValue, TryFromBorrowedObject, TryFromObject, }; use bstr::ByteSlice; @@ -3660,7 +3660,7 @@ mod fileio { crt_fd::Fd, function::{ArgBytesLike, ArgMemoryBuffer, FuncArgs, OptionalArg, OptionalOption}, stdlib::os, - AsPyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, VirtualMachine, + AsObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, VirtualMachine, }; use crossbeam_utils::atomic::AtomicCell; use std::io::{Read, Write}; diff --git a/vm/src/stdlib/itertools.rs b/vm/src/stdlib/itertools.rs index 1049d036b8..400cc10b9b 100644 --- a/vm/src/stdlib/itertools.rs +++ b/vm/src/stdlib/itertools.rs @@ -13,7 +13,7 @@ mod decl { protocol::{PyIter, PyIterReturn}, stdlib::sys, types::{Constructor, IterNext, IterNextIterable}, - AsPyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, PyWeakRef, VirtualMachine, + AsObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, PyWeakRef, VirtualMachine, }; use crossbeam_utils::atomic::AtomicCell; use num_bigint::BigInt; diff --git a/vm/src/stdlib/marshal.rs b/vm/src/stdlib/marshal.rs index c5e7497fe4..fd5f50a270 100644 --- a/vm/src/stdlib/marshal.rs +++ b/vm/src/stdlib/marshal.rs @@ -11,7 +11,7 @@ mod decl { convert::ToPyObject, function::ArgBytesLike, protocol::PyBuffer, - pyobject::AsPyObject, + pyobject::AsObject, PyObjectRef, PyResult, TryFromObject, VirtualMachine, }; /// TODO diff --git a/vm/src/stdlib/operator.rs b/vm/src/stdlib/operator.rs index 1366ca7bb3..e22a07fb05 100644 --- a/vm/src/stdlib/operator.rs +++ b/vm/src/stdlib/operator.rs @@ -20,7 +20,7 @@ mod _operator { }, utils::Either, vm::ReprGuard, - AsPyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, VirtualMachine, + AsObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, VirtualMachine, }; /// Same as a < b. diff --git a/vm/src/stdlib/os.rs b/vm/src/stdlib/os.rs index 87bcb18066..fa17e4740b 100644 --- a/vm/src/stdlib/os.rs +++ b/vm/src/stdlib/os.rs @@ -4,7 +4,7 @@ use crate::{ crt_fd::Fd, function::{ArgumentError, FromArgs, FuncArgs}, protocol::PyBuffer, - AsPyObject, PyObject, PyObjectRef, PyResult, PyValue, TryFromBorrowedObject, TryFromObject, + AsObject, PyObject, PyObjectRef, PyResult, PyValue, TryFromBorrowedObject, TryFromObject, VirtualMachine, }; use std::{ @@ -442,7 +442,7 @@ pub(super) mod _os { types::{IterNext, IterNextIterable}, utils::Either, vm::{ReprGuard, VirtualMachine}, - AsPyObject, PyObjectRef, PyRef, PyResult, PyStructSequence, PyValue, TryFromObject, + AsObject, PyObjectRef, PyRef, PyResult, PyStructSequence, PyValue, TryFromObject, }; use crossbeam_utils::atomic::AtomicCell; use itertools::Itertools; diff --git a/vm/src/stdlib/posix.rs b/vm/src/stdlib/posix.rs index 99f7c5836a..9c31d17dd4 100644 --- a/vm/src/stdlib/posix.rs +++ b/vm/src/stdlib/posix.rs @@ -38,7 +38,7 @@ pub mod module { }, types::Constructor, utils::{Either, ToCString}, - AsPyObject, PyObjectRef, PyResult, PyValue, VirtualMachine, + AsObject, PyObjectRef, PyResult, PyValue, VirtualMachine, }; use bitflags::bitflags; use nix::{ @@ -558,7 +558,7 @@ pub mod module { target_os = "android" ))] fn try_to_libc(&self, vm: &VirtualMachine) -> PyResult { - use crate::AsPyObject; + use crate::AsObject; let priority_class = self.sched_priority.class(); let priority_type = priority_class.name(); let priority = self.sched_priority.clone(); diff --git a/vm/src/stdlib/pwd.rs b/vm/src/stdlib/pwd.rs index 125ae5a532..cea7ba6f17 100644 --- a/vm/src/stdlib/pwd.rs +++ b/vm/src/stdlib/pwd.rs @@ -5,7 +5,7 @@ mod pwd { use crate::{ builtins::{PyIntRef, PyStrRef}, convert::{ToPyException, ToPyObject}, - AsPyObject, PyObjectRef, PyResult, PyStructSequence, VirtualMachine, + AsObject, PyObjectRef, PyResult, PyStructSequence, VirtualMachine, }; use nix::unistd::{self, User}; use std::ptr::NonNull; diff --git a/vm/src/stdlib/thread.rs b/vm/src/stdlib/thread.rs index 55b454edfa..a540d0ef3a 100644 --- a/vm/src/stdlib/thread.rs +++ b/vm/src/stdlib/thread.rs @@ -11,7 +11,7 @@ pub(crate) mod _thread { py_io, types::{Constructor, GetAttr, SetAttr}, utils::Either, - AsPyObject, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine, + AsObject, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine, }; use parking_lot::{ lock_api::{RawMutex as RawMutexT, RawMutexTimed, RawReentrantMutex}, diff --git a/vm/src/stdlib/warnings.rs b/vm/src/stdlib/warnings.rs index 4ecc46aa6e..011baa0f08 100644 --- a/vm/src/stdlib/warnings.rs +++ b/vm/src/stdlib/warnings.rs @@ -5,7 +5,7 @@ mod _warnings { use crate::{ builtins::{PyStrRef, PyTypeRef}, function::OptionalArg, - AsPyObject, PyResult, VirtualMachine, + AsObject, PyResult, VirtualMachine, }; #[derive(FromArgs)] diff --git a/vm/src/suggestion.rs b/vm/src/suggestion.rs index 81fdcaabb5..c215b611ef 100644 --- a/vm/src/suggestion.rs +++ b/vm/src/suggestion.rs @@ -2,7 +2,7 @@ use crate::{ builtins::{PyStr, PyStrRef}, exceptions::types::PyBaseExceptionRef, sliceable::SliceableSequenceOp, - AsPyObject, PyObjectRef, PyObjectView, VirtualMachine, + AsObject, PyObjectRef, PyObjectView, VirtualMachine, }; use rustpython_common::str::levenshtein::{levenshtein_distance, MOVE_COST}; use std::iter::ExactSizeIterator; diff --git a/vm/src/types/slot.rs b/vm/src/types/slot.rs index 4dae0ccaa4..00d51216bc 100644 --- a/vm/src/types/slot.rs +++ b/vm/src/types/slot.rs @@ -8,7 +8,7 @@ use crate::{ PyBuffer, PyIterReturn, PyMapping, PyMappingMethods, PySequence, PySequenceMethods, }, utils::Either, - AsPyObject, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, VirtualMachine, + AsObject, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, VirtualMachine, }; use crossbeam_utils::atomic::AtomicCell; use num_traits::{Signed, ToPrimitive}; diff --git a/vm/src/utils.rs b/vm/src/utils.rs index 043b9de910..8ef67dec6c 100644 --- a/vm/src/utils.rs +++ b/vm/src/utils.rs @@ -1,7 +1,7 @@ use crate::{ builtins::{PyFloat, PyStr}, convert::{ToPyException, ToPyObject}, - AsPyObject, PyObject, PyObjectRef, PyObjectWrap, PyResult, TryFromObject, VirtualMachine, + AsObject, PyObject, PyObjectRef, PyObjectWrap, PyResult, TryFromObject, VirtualMachine, }; use num_traits::ToPrimitive; use std::borrow::Borrow; diff --git a/vm/src/vm.rs b/vm/src/vm.rs index b5c3b5ec0a..88c4df2628 100644 --- a/vm/src/vm.rs +++ b/vm/src/vm.rs @@ -24,7 +24,7 @@ use crate::{ import, protocol::PyIterIter, scope::Scope, - signal, stdlib, AsPyObject, PyContext, PyObject, PyObjectRef, PyRef, PyRefExact, PyResult, + signal, stdlib, AsObject, PyContext, PyObject, PyObjectRef, PyRef, PyRefExact, PyResult, PyValue, }; use crossbeam_utils::atomic::AtomicCell; @@ -67,7 +67,7 @@ struct ExceptionStack { } pub(crate) mod thread { - use super::{AsPyObject, PyObject, VirtualMachine}; + use super::{AsObject, PyObject, VirtualMachine}; use itertools::Itertools; use std::{cell::RefCell, ptr::NonNull, thread_local}; diff --git a/vm/src/vm_new.rs b/vm/src/vm_new.rs index d1b6a07a13..701cd52e67 100644 --- a/vm/src/vm_new.rs +++ b/vm/src/vm_new.rs @@ -10,7 +10,7 @@ use crate::{ convert::ToPyObject, scope::Scope, vm::VirtualMachine, - AsPyObject, PyObject, PyObjectRef, PyObjectWrap, PyRef, PyValue, + AsObject, PyObject, PyObjectRef, PyObjectWrap, PyRef, PyValue, }; /// Collection of object creation helpers @@ -288,7 +288,7 @@ impl VirtualMachine { pub(crate) fn new_downcast_runtime_error( &self, class: &PyTypeRef, - obj: &impl AsPyObject, + obj: &impl AsObject, ) -> PyBaseExceptionRef { self.new_downcast_error( "payload", @@ -301,7 +301,7 @@ impl VirtualMachine { pub(crate) fn new_downcast_type_error( &self, class: &PyTypeRef, - obj: &impl AsPyObject, + obj: &impl AsObject, ) -> PyBaseExceptionRef { self.new_downcast_error( "type", diff --git a/vm/src/vm_object.rs b/vm/src/vm_object.rs index d9f1bd2d77..8e39ebae66 100644 --- a/vm/src/vm_object.rs +++ b/vm/src/vm_object.rs @@ -2,7 +2,7 @@ use crate::{ builtins::{PyBaseExceptionRef, PyList, PyStr}, function::{FuncArgs, IntoFuncArgs}, vm::VirtualMachine, - AsPyObject, PyMethod, PyObject, PyObjectRef, PyResult, PyValue, + AsObject, PyMethod, PyObject, PyObjectRef, PyResult, PyValue, }; /// Trace events for sys.settrace and sys.setprofile. @@ -179,7 +179,7 @@ impl VirtualMachine { } #[inline(always)] - pub fn invoke(&self, func: &impl AsPyObject, args: impl IntoFuncArgs) -> PyResult { + pub fn invoke(&self, func: &impl AsObject, args: impl IntoFuncArgs) -> PyResult { self._invoke(func.as_object(), args.into_args(self)) } diff --git a/vm/src/vm_ops.rs b/vm/src/vm_ops.rs index ade8a14a4c..0a3d9738bd 100644 --- a/vm/src/vm_ops.rs +++ b/vm/src/vm_ops.rs @@ -4,7 +4,7 @@ use crate::{ protocol::PyIterReturn, types::PyComparisonOp, vm::VirtualMachine, - AsPyObject, PyMethod, PyObject, PyObjectRef, PyResult, + AsObject, PyMethod, PyObject, PyObjectRef, PyResult, }; /// Collection of operators diff --git a/wasm/lib/src/convert.rs b/wasm/lib/src/convert.rs index f810aefa0f..8a516e2b9a 100644 --- a/wasm/lib/src/convert.rs +++ b/wasm/lib/src/convert.rs @@ -7,7 +7,7 @@ use rustpython_vm::{ compile::{CompileError, CompileErrorType}, exceptions, function::{ArgBytesLike, FuncArgs}, - py_serde, AsPyObject, PyObjectRef, PyResult, PyValue, TryFromBorrowedObject, VirtualMachine, + py_serde, AsObject, PyObjectRef, PyResult, PyValue, TryFromBorrowedObject, VirtualMachine, }; use wasm_bindgen::{closure::Closure, prelude::*, JsCast};