Skip to content

Rename wrong traits names and relocate convert traits #3632

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions derive/src/pystructseq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ pub(crate) fn impl_pystruct_sequence(input: DeriveInput) -> Result<TokenStream>
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::convert::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::convert::ToPyObject for #ty {
fn to_pyobject(self, vm: &::rustpython_vm::VirtualMachine) -> ::rustpython_vm::PyObjectRef {
::rustpython_vm::PyStructSequence::into_struct_sequence(self, vm).into()
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
2 changes: 1 addition & 1 deletion src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rustpython_vm::{
builtins::PyBaseExceptionRef,
compile::{self, CompileError, CompileErrorType},
scope::Scope,
AsPyObject, PyResult, VirtualMachine,
AsObject, PyResult, VirtualMachine,
};

enum ShellExecResult {
Expand Down
36 changes: 17 additions & 19 deletions stdlib/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ mod array {
PyListRef, PyStr, PyStrRef, PyTupleRef, PyTypeRef,
},
class_or_notimplemented,
function::{
ArgBytesLike, ArgIntoFloat, ArgIterable, IntoPyObject, IntoPyResult, OptionalArg,
PyComparisonValue,
},
convert::{ToPyObject, ToPyResult, TryFromBorrowedObject, TryFromObject},
function::{ArgBytesLike, ArgIntoFloat, ArgIterable, OptionalArg, PyComparisonValue},
protocol::{
BufferDescriptor, BufferMethods, BufferResizeGuard, PyBuffer, PyIterReturn,
PyMappingMethods,
Expand All @@ -33,8 +31,8 @@ mod array {
AsBuffer, AsMapping, Comparable, Constructor, IterNext, IterNextIterable, Iterable,
PyComparisonOp,
},
AsPyObject, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue,
TryFromBorrowedObject, TryFromObject, VirtualMachine,
AsObject, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue,
VirtualMachine,
},
};
use itertools::Itertools;
Expand Down Expand Up @@ -117,7 +115,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)
})*
}
}
Expand Down Expand Up @@ -258,15 +256,15 @@ mod array {
) -> Option<PyResult> {
match self {
$(ArrayContentType::$n(v) => {
v.get(i).map(|x| x.into_pyresult(vm))
v.get(i).map(|x| x.to_pyresult(vm))
})*
}
}

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))?
})*
}
}
Expand All @@ -276,7 +274,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)
})*
}
}
Expand Down Expand Up @@ -568,11 +566,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),
)
}
}
Expand Down Expand Up @@ -1139,7 +1137,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)
};
Expand Down Expand Up @@ -1472,15 +1470,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),
}
}};
}
Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 6 additions & 5 deletions stdlib/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ mod _json {
use super::machinery;
use crate::vm::{
builtins::{PyBaseExceptionRef, PyStrRef, PyTypeRef},
function::{IntoPyObject, IntoPyResult, OptionalArg},
convert::{ToPyObject, ToPyResult},
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;
Expand Down Expand Up @@ -85,7 +86,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
Expand Down Expand Up @@ -206,7 +207,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(),
Expand All @@ -215,7 +216,7 @@ mod _json {
zelf.to_owned().into(),
vm,
)
.and_then(|x| x.into_pyresult(vm))
.and_then(|x| x.to_pyresult(vm))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
4 changes: 2 additions & 2 deletions stdlib/src/posixsubprocess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{convert::ToPyException, PyResult, VirtualMachine};

#[pyfunction]
fn fork_exec(args: ForkExecArgs, vm: &VirtualMachine) -> PyResult<libc::pid_t> {
Expand All @@ -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()),
}
Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/pystruct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
35 changes: 20 additions & 15 deletions stdlib/src/re.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ mod re {
*/
use crate::vm::{
builtins::{PyInt, PyIntRef, PyStr, PyStrRef},
function::{IntoPyObject, 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};
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -418,14 +423,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<Vec<_>, _> = 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()
}
Expand All @@ -442,7 +447,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()),
)
})
Expand Down
12 changes: 6 additions & 6 deletions stdlib/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub(crate) use resource::make_module;
#[pymodule]
mod resource {
use crate::vm::{
function::{IntoPyException, IntoPyObject},
convert::{ToPyException, ToPyObject},
stdlib::os,
PyObject, PyObjectRef, PyResult, PyStructSequence, TryFromBorrowedObject, VirtualMachine,
};
Expand Down Expand Up @@ -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)
}
})
}
Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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),
})
}
}
4 changes: 2 additions & 2 deletions stdlib/src/scproxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod _scproxy {

use crate::vm::{
builtins::{PyDictRef, PyStr},
function::IntoPyObject,
convert::ToPyObject,
PyResult, VirtualMachine,
};
use system_configuration::core_foundation::{
Expand Down Expand Up @@ -62,7 +62,7 @@ mod _scproxy {
let a_string: std::borrow::Cow<str> = (&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)?;
Expand Down
Loading