Skip to content

Commit 584d972

Browse files
authored
Merge pull request #3632 from youknowone/cleanup-traits
Rename wrong traits names and relocate convert traits
2 parents ea39443 + fee3bf2 commit 584d972

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

118 files changed

+815
-823
lines changed

derive/src/pystructseq.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ pub(crate) fn impl_pystruct_sequence(input: DeriveInput) -> Result<TokenStream>
2929
impl ::rustpython_vm::PyStructSequence for #ty {
3030
const FIELD_NAMES: &'static [&'static str] = &[#(stringify!(#field_names)),*];
3131
fn into_tuple(self, vm: &::rustpython_vm::VirtualMachine) -> ::rustpython_vm::builtins::PyTuple {
32-
let items = vec![#(::rustpython_vm::function::IntoPyObject::into_pyobject(
32+
let items = vec![#(::rustpython_vm::convert::ToPyObject::to_pyobject(
3333
self.#field_names,
3434
vm,
3535
)),*];
3636
::rustpython_vm::builtins::PyTuple::new_unchecked(items.into_boxed_slice())
3737
}
3838
}
39-
impl ::rustpython_vm::function::IntoPyObject for #ty {
40-
fn into_pyobject(self, vm: &::rustpython_vm::VirtualMachine) -> ::rustpython_vm::PyObjectRef {
39+
impl ::rustpython_vm::convert::ToPyObject for #ty {
40+
fn to_pyobject(self, vm: &::rustpython_vm::VirtualMachine) -> ::rustpython_vm::PyObjectRef {
4141
::rustpython_vm::PyStructSequence::into_struct_sequence(self, vm).into()
4242
}
4343
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ use rustpython_vm::{
5151
compile, match_class,
5252
scope::Scope,
5353
stdlib::{atexit, sys},
54-
AsPyObject, InitParameter, Interpreter, PyObjectRef, PyResult, PySettings, TryFromObject,
54+
AsObject, InitParameter, Interpreter, PyObjectRef, PyResult, PySettings, TryFromObject,
5555
VirtualMachine,
5656
};
5757
use std::{env, path::Path, process, str::FromStr};

src/shell.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustpython_vm::{
66
builtins::PyBaseExceptionRef,
77
compile::{self, CompileError, CompileErrorType},
88
scope::Scope,
9-
AsPyObject, PyResult, VirtualMachine,
9+
AsObject, PyResult, VirtualMachine,
1010
};
1111

1212
enum ShellExecResult {

stdlib/src/array.rs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ mod array {
1717
PyListRef, PyStr, PyStrRef, PyTupleRef, PyTypeRef,
1818
},
1919
class_or_notimplemented,
20-
function::{
21-
ArgBytesLike, ArgIntoFloat, ArgIterable, IntoPyObject, IntoPyResult, OptionalArg,
22-
PyComparisonValue,
23-
},
20+
convert::{ToPyObject, ToPyResult, TryFromBorrowedObject, TryFromObject},
21+
function::{ArgBytesLike, ArgIntoFloat, ArgIterable, OptionalArg, PyComparisonValue},
2422
protocol::{
2523
BufferDescriptor, BufferMethods, BufferResizeGuard, PyBuffer, PyIterReturn,
2624
PyMappingMethods,
@@ -33,8 +31,8 @@ mod array {
3331
AsBuffer, AsMapping, Comparable, Constructor, IterNext, IterNextIterable, Iterable,
3432
PyComparisonOp,
3533
},
36-
AsPyObject, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue,
37-
TryFromBorrowedObject, TryFromObject, VirtualMachine,
34+
AsObject, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue,
35+
VirtualMachine,
3836
},
3937
};
4038
use itertools::Itertools;
@@ -117,7 +115,7 @@ mod array {
117115
let i = v.wrap_index(i).ok_or_else(|| {
118116
vm.new_index_error("pop index out of range".to_owned())
119117
})?;
120-
v.remove(i).into_pyresult(vm)
118+
v.remove(i).to_pyresult(vm)
121119
})*
122120
}
123121
}
@@ -258,15 +256,15 @@ mod array {
258256
) -> Option<PyResult> {
259257
match self {
260258
$(ArrayContentType::$n(v) => {
261-
v.get(i).map(|x| x.into_pyresult(vm))
259+
v.get(i).map(|x| x.to_pyresult(vm))
262260
})*
263261
}
264262
}
265263

266264
fn getitem_by_index(&self, i: isize, vm: &VirtualMachine) -> PyResult {
267265
match self {
268266
$(ArrayContentType::$n(v) => {
269-
v.get_item_by_index(vm, i).map(|x| x.into_pyresult(vm))?
267+
v.get_item_by_index(vm, i).map(|x| x.to_pyresult(vm))?
270268
})*
271269
}
272270
}
@@ -276,7 +274,7 @@ mod array {
276274
$(ArrayContentType::$n(v) => {
277275
let r = v.get_item_by_slice(vm, slice)?;
278276
let array = PyArray::from(ArrayContentType::$n(r));
279-
array.into_pyresult(vm)
277+
array.to_pyresult(vm)
280278
})*
281279
}
282280
}
@@ -568,11 +566,11 @@ mod array {
568566
}
569567
}
570568

571-
impl IntoPyResult for WideChar {
572-
fn into_pyresult(self, vm: &VirtualMachine) -> PyResult {
569+
impl ToPyResult for WideChar {
570+
fn to_pyresult(self, vm: &VirtualMachine) -> PyResult {
573571
Ok(
574572
String::from(char::try_from(self).map_err(|e| vm.new_unicode_encode_error(e))?)
575-
.into_pyobject(vm),
573+
.to_pyobject(vm),
576574
)
577575
}
578576
}
@@ -1139,7 +1137,7 @@ mod array {
11391137
let typecode = vm.ctx.new_str(array.typecode_str());
11401138
let values = if array.typecode() == 'u' {
11411139
let s = Self::_wchar_bytes_to_string(array.get_bytes(), array.itemsize(), vm)?;
1142-
s.chars().map(|x| x.into_pyobject(vm)).collect()
1140+
s.chars().map(|x| x.to_pyobject(vm)).collect()
11431141
} else {
11441142
array.get_objects(vm)
11451143
};
@@ -1472,15 +1470,15 @@ mod array {
14721470
}
14731471
}};
14741472
($VM:ident, $BYTE:ident, $TY:ty, $BIG_ENDIAN:ident) => {
1475-
chunk_to_obj!($BYTE, $TY, $BIG_ENDIAN).into_pyobject($VM)
1473+
chunk_to_obj!($BYTE, $TY, $BIG_ENDIAN).to_pyobject($VM)
14761474
};
14771475
($VM:ident, $BYTE:ident, $SIGNED_TY:ty, $UNSIGNED_TY:ty, $SIGNED:ident, $BIG_ENDIAN:ident) => {{
14781476
let b = <[u8; ::std::mem::size_of::<$SIGNED_TY>()]>::try_from($BYTE).unwrap();
14791477
match ($SIGNED, $BIG_ENDIAN) {
1480-
(false, false) => <$UNSIGNED_TY>::from_le_bytes(b).into_pyobject($VM),
1481-
(false, true) => <$UNSIGNED_TY>::from_be_bytes(b).into_pyobject($VM),
1482-
(true, false) => <$SIGNED_TY>::from_le_bytes(b).into_pyobject($VM),
1483-
(true, true) => <$SIGNED_TY>::from_be_bytes(b).into_pyobject($VM),
1478+
(false, false) => <$UNSIGNED_TY>::from_le_bytes(b).to_pyobject($VM),
1479+
(false, true) => <$UNSIGNED_TY>::from_be_bytes(b).to_pyobject($VM),
1480+
(true, false) => <$SIGNED_TY>::from_le_bytes(b).to_pyobject($VM),
1481+
(true, true) => <$SIGNED_TY>::from_be_bytes(b).to_pyobject($VM),
14841482
}
14851483
}};
14861484
}

stdlib/src/csv.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ mod _csv {
1616
match_class,
1717
protocol::{PyIter, PyIterReturn},
1818
types::{IterNext, IterNextIterable},
19-
AsPyObject, PyObjectRef, PyObjectView, PyResult, PyValue, TryFromObject, VirtualMachine,
19+
AsObject, PyObjectRef, PyObjectView, PyResult, PyValue, TryFromObject, VirtualMachine,
2020
};
2121
use itertools::{self, Itertools};
2222
use std::fmt;

stdlib/src/json.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ mod _json {
66
use super::machinery;
77
use crate::vm::{
88
builtins::{PyBaseExceptionRef, PyStrRef, PyTypeRef},
9-
function::{IntoPyObject, IntoPyResult, OptionalArg},
9+
convert::{ToPyObject, ToPyResult},
10+
function::OptionalArg,
1011
protocol::PyIterReturn,
1112
types::{Callable, Constructor},
12-
AsPyObject, PyObjectRef, PyObjectView, PyResult, PyValue, VirtualMachine,
13+
AsObject, PyObjectRef, PyObjectView, PyResult, PyValue, VirtualMachine,
1314
};
1415
use num_bigint::BigInt;
1516
use std::str::FromStr;
@@ -85,7 +86,7 @@ mod _json {
8586
match c {
8687
'"' => {
8788
return scanstring(pystr, next_idx, OptionalArg::Present(self.strict), vm)
88-
.map(|x| PyIterReturn::Return(x.into_pyobject(vm)))
89+
.map(|x| PyIterReturn::Return(x.to_pyobject(vm)))
8990
}
9091
'{' => {
9192
// TODO: parse the object in rust
@@ -206,7 +207,7 @@ mod _json {
206207
let idx = idx as usize;
207208
let mut chars = pystr.as_str().chars();
208209
if idx > 0 && chars.nth(idx - 1).is_none() {
209-
PyIterReturn::StopIteration(Some(vm.ctx.new_int(idx).into())).into_pyresult(vm)
210+
PyIterReturn::StopIteration(Some(vm.ctx.new_int(idx).into())).to_pyresult(vm)
210211
} else {
211212
zelf.parse(
212213
chars.as_str(),
@@ -215,7 +216,7 @@ mod _json {
215216
zelf.to_owned().into(),
216217
vm,
217218
)
218-
.and_then(|x| x.into_pyresult(vm))
219+
.and_then(|x| x.to_pyresult(vm))
219220
}
220221
}
221222
}

stdlib/src/math.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ mod math {
66
builtins::{try_bigint_to_f64, try_f64_to_bigint, PyFloat, PyInt, PyIntRef},
77
function::{ArgIntoFloat, ArgIterable, OptionalArg, PosArgs},
88
utils::Either,
9-
AsPyObject, PyObject, PyObjectRef, PyRef, PyResult, VirtualMachine,
9+
AsObject, PyObject, PyObjectRef, PyRef, PyResult, VirtualMachine,
1010
};
1111
use num_bigint::BigInt;
1212
use num_traits::{One, Signed, Zero};

stdlib/src/posixsubprocess.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub(crate) use _posixsubprocess::make_module;
1919
#[pymodule]
2020
mod _posixsubprocess {
2121
use super::{exec, CStrPathLike, ForkExecArgs, ProcArgs};
22-
use crate::vm::{function::IntoPyException, PyResult, VirtualMachine};
22+
use crate::vm::{convert::ToPyException, PyResult, VirtualMachine};
2323

2424
#[pyfunction]
2525
fn fork_exec(args: ForkExecArgs, vm: &VirtualMachine) -> PyResult<libc::pid_t> {
@@ -37,7 +37,7 @@ mod _posixsubprocess {
3737
let argv = &argv;
3838
let envp = args.env_list.as_ref().map(|s| cstrs_to_ptrs(s.as_slice()));
3939
let envp = envp.as_deref();
40-
match unsafe { nix::unistd::fork() }.map_err(|err| err.into_pyexception(vm))? {
40+
match unsafe { nix::unistd::fork() }.map_err(|err| err.to_pyexception(vm))? {
4141
nix::unistd::ForkResult::Child => exec(&args, ProcArgs { argv, envp }),
4242
nix::unistd::ForkResult::Parent { child } => Ok(child.as_raw()),
4343
}

stdlib/src/pystruct.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub(crate) mod _struct {
1616
match_class,
1717
protocol::PyIterReturn,
1818
types::{Constructor, IterNext, IterNextIterable},
19-
AsPyObject, PyObjectRef, PyObjectView, PyResult, PyValue, TryFromObject, VirtualMachine,
19+
AsObject, PyObjectRef, PyObjectView, PyResult, PyValue, TryFromObject, VirtualMachine,
2020
};
2121
use crossbeam_utils::atomic::AtomicCell;
2222

stdlib/src/re.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ mod re {
1010
*/
1111
use crate::vm::{
1212
builtins::{PyInt, PyIntRef, PyStr, PyStrRef},
13-
function::{IntoPyObject, OptionalArg, PosArgs},
14-
match_class, PyObjectRef, PyResult, PyValue, TryFromObject, VirtualMachine,
13+
convert::{ToPyObject, TryFromObject},
14+
function::{OptionalArg, PosArgs},
15+
match_class, PyObjectRef, PyResult, PyValue, VirtualMachine,
1516
};
1617
use num_traits::Signed;
1718
use regex::bytes::{Captures, Regex, RegexBuilder};
@@ -252,9 +253,11 @@ mod re {
252253
let split = output
253254
.into_iter()
254255
.map(|v| {
255-
vm.unwrap_or_none(
256-
v.map(|v| vm.ctx.new_str(String::from_utf8_lossy(v).into_owned()).into()),
257-
)
256+
vm.unwrap_or_none(v.map(|v| {
257+
vm.ctx
258+
.new_str(String::from_utf8_lossy(v).into_owned())
259+
.into()
260+
}))
258261
})
259262
.collect();
260263
Ok(vm.ctx.new_list(split).into())
@@ -369,18 +372,20 @@ mod re {
369372
#[pymethod]
370373
fn start(&self, group: OptionalArg, vm: &VirtualMachine) -> PyResult {
371374
let group = group.unwrap_or_else(|| vm.ctx.new_int(0).into());
372-
let start = self
373-
.get_bounds(group, vm)?
374-
.map_or_else(|| vm.ctx.new_int(-1).into(), |r| vm.ctx.new_int(r.start).into());
375+
let start = self.get_bounds(group, vm)?.map_or_else(
376+
|| vm.ctx.new_int(-1).into(),
377+
|r| vm.ctx.new_int(r.start).into(),
378+
);
375379
Ok(start)
376380
}
377381

378382
#[pymethod]
379383
fn end(&self, group: OptionalArg, vm: &VirtualMachine) -> PyResult {
380384
let group = group.unwrap_or_else(|| vm.ctx.new_int(0).into());
381-
let end = self
382-
.get_bounds(group, vm)?
383-
.map_or_else(|| vm.ctx.new_int(-1).into(), |r| vm.ctx.new_int(r.end).into());
385+
let end = self.get_bounds(group, vm)?.map_or_else(
386+
|| vm.ctx.new_int(-1).into(),
387+
|r| vm.ctx.new_int(r.end).into(),
388+
);
384389
Ok(end)
385390
}
386391

@@ -418,14 +423,14 @@ mod re {
418423
match groups.len() {
419424
0 => Ok(self
420425
.subgroup(self.captures[0].clone().unwrap())
421-
.into_pyobject(vm)),
426+
.to_pyobject(vm)),
422427
1 => self
423428
.get_group(groups.pop().unwrap(), vm)
424-
.map(|g| g.into_pyobject(vm)),
429+
.map(|g| g.to_pyobject(vm)),
425430
_ => {
426431
let output: Result<Vec<_>, _> = groups
427432
.into_iter()
428-
.map(|id| self.get_group(id, vm).map(|g| g.into_pyobject(vm)))
433+
.map(|id| self.get_group(id, vm).map(|g| g.to_pyobject(vm)))
429434
.collect();
430435
Ok(vm.ctx.new_tuple(output?)).into()
431436
}
@@ -442,7 +447,7 @@ mod re {
442447
vm.unwrap_or_none(
443448
capture
444449
.as_ref()
445-
.map(|bounds| self.subgroup(bounds.clone()).into_pyobject(vm))
450+
.map(|bounds| self.subgroup(bounds.clone()).to_pyobject(vm))
446451
.or_else(|| default.clone()),
447452
)
448453
})

stdlib/src/resource.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ pub(crate) use resource::make_module;
33
#[pymodule]
44
mod resource {
55
use crate::vm::{
6-
function::{IntoPyException, IntoPyObject},
6+
convert::{ToPyException, ToPyObject},
77
stdlib::os,
88
PyObject, PyObjectRef, PyResult, PyStructSequence, TryFromBorrowedObject, VirtualMachine,
99
};
@@ -122,7 +122,7 @@ mod resource {
122122
if e.kind() == io::ErrorKind::InvalidInput {
123123
vm.new_value_error("invalid who parameter".to_owned())
124124
} else {
125-
e.into_pyexception(vm)
125+
e.to_pyexception(vm)
126126
}
127127
})
128128
}
@@ -140,9 +140,9 @@ mod resource {
140140
}
141141
}
142142
}
143-
impl IntoPyObject for Limits {
144-
fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef {
145-
(self.0.rlim_cur, self.0.rlim_max).into_pyobject(vm)
143+
impl ToPyObject for Limits {
144+
fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef {
145+
(self.0.rlim_cur, self.0.rlim_max).to_pyobject(vm)
146146
}
147147
}
148148

@@ -180,7 +180,7 @@ mod resource {
180180
io::ErrorKind::PermissionDenied => {
181181
vm.new_value_error("not allowed to raise maximum limit".to_owned())
182182
}
183-
_ => e.into_pyexception(vm),
183+
_ => e.to_pyexception(vm),
184184
})
185185
}
186186
}

stdlib/src/scproxy.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ mod _scproxy {
66

77
use crate::vm::{
88
builtins::{PyDictRef, PyStr},
9-
function::IntoPyObject,
9+
convert::ToPyObject,
1010
PyResult, VirtualMachine,
1111
};
1212
use system_configuration::core_foundation::{
@@ -62,7 +62,7 @@ mod _scproxy {
6262
let a_string: std::borrow::Cow<str> = (&s).into();
6363
PyStr::from(a_string.into_owned())
6464
})
65-
.into_pyobject(vm)
65+
.to_pyobject(vm)
6666
})
6767
.collect();
6868
result.set_item("exceptions", vm.ctx.new_tuple(v).into(), vm)?;

0 commit comments

Comments
 (0)