Skip to content

Commit 997f85f

Browse files
committed
Less PyListRef
1 parent 7377664 commit 997f85f

File tree

5 files changed

+35
-41
lines changed

5 files changed

+35
-41
lines changed

stdlib/src/termios.rs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -67,26 +67,23 @@ mod termios {
6767
termios::cfsetospeed(&mut termios, ospeed.try_into_value(vm)?)
6868
.map_err(|e| termios_error(e, vm))?;
6969
let cc = PyListRef::try_from_object(vm, cc)?;
70-
{
71-
let cc = cc.borrow_vec();
72-
let cc = <&[PyObjectRef; NCCS]>::try_from(&*cc).map_err(|_| {
73-
vm.new_type_error(format!(
74-
"tcsetattr: attributes[6] must be {} element list",
75-
NCCS
76-
))
77-
})?;
78-
for (cc, x) in termios.c_cc.iter_mut().zip(cc.iter()) {
79-
*cc = if let Some(c) = x.payload::<PyBytes>().filter(|b| b.as_bytes().len() == 1) {
80-
c.as_bytes()[0] as _
81-
} else if let Some(i) = x.payload::<PyInt>() {
82-
i.try_to_primitive(vm)?
83-
} else {
84-
return Err(vm.new_type_error(
85-
"tcsetattr: elements of attributes must be characters or integers"
86-
.to_owned(),
87-
));
88-
};
89-
}
70+
let cc = cc.borrow_vec();
71+
let cc = <&[PyObjectRef; NCCS]>::try_from(&*cc).map_err(|_| {
72+
vm.new_type_error(format!(
73+
"tcsetattr: attributes[6] must be {} element list",
74+
NCCS
75+
))
76+
})?;
77+
for (cc, x) in termios.c_cc.iter_mut().zip(cc.iter()) {
78+
*cc = if let Some(c) = x.payload::<PyBytes>().filter(|b| b.as_bytes().len() == 1) {
79+
c.as_bytes()[0] as _
80+
} else if let Some(i) = x.payload::<PyInt>() {
81+
i.try_to_primitive(vm)?
82+
} else {
83+
return Err(vm.new_type_error(
84+
"tcsetattr: elements of attributes must be characters or integers".to_owned(),
85+
));
86+
};
9087
}
9188

9289
termios::tcsetattr(fd, when, &termios).map_err(|e| termios_error(e, vm))?;

vm/src/builtins/list.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl PyList {
111111
}
112112

113113
#[pymethod(magic)]
114-
fn add(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyListRef> {
114+
fn add(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
115115
let other = other.payload_if_subclass::<PyList>(vm).ok_or_else(|| {
116116
vm.new_type_error(format!(
117117
"Cannot add {} and {}",
@@ -146,7 +146,7 @@ impl PyList {
146146
}
147147

148148
#[pymethod]
149-
fn copy(&self, vm: &VirtualMachine) -> PyListRef {
149+
fn copy(&self, vm: &VirtualMachine) -> PyRef<Self> {
150150
Self::new_ref(self.borrow_vec().to_vec(), &vm.ctx)
151151
}
152152

vm/src/builtins/memory.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ impl PyMemoryView {
501501
}
502502

503503
#[pymethod]
504-
fn tolist(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult<PyListRef> {
504+
fn tolist(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult<Vec<PyObjectRef>> {
505505
zelf.try_not_released(vm)?;
506506

507507
let bytes = &*zelf.obj_bytes();
@@ -516,7 +516,7 @@ impl PyMemoryView {
516516
})
517517
.try_collect()?;
518518

519-
Ok(PyList::from(elements).into_ref(vm))
519+
Ok(elements)
520520
}
521521

522522
#[pymethod]
@@ -845,7 +845,7 @@ fn format_unpack(
845845
})
846846
}
847847

848-
pub fn unpack_bytes_seq_to_list(
848+
fn unpack_bytes_seq_to_list(
849849
bytes: &[u8],
850850
format: &str,
851851
vm: &VirtualMachine,

vm/src/stdlib/sre.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ pub(crate) use _sre::make_module;
44
mod _sre {
55
use crate::{
66
builtins::{
7-
PyCallableIterator, PyDictRef, PyInt, PyList, PyListRef, PyStr, PyStrRef, PyTuple,
8-
PyTupleRef,
7+
PyCallableIterator, PyDictRef, PyInt, PyList, PyStr, PyStrRef, PyTuple, PyTupleRef,
98
},
109
common::{ascii, hash::PyHash},
1110
function::{ArgCallable, IntoPyObject, OptionalArg, PosArgs},
@@ -247,7 +246,7 @@ mod _sre {
247246
zelf: PyRef<Pattern>,
248247
string_args: StringArgs,
249248
vm: &VirtualMachine,
250-
) -> PyResult<PyListRef> {
249+
) -> PyResult<Vec<PyObjectRef>> {
251250
zelf.with_state(
252251
string_args.string.clone(),
253252
string_args.pos,
@@ -277,7 +276,7 @@ mod _sre {
277276
state.start = state.string_position;
278277
state.reset();
279278
}
280-
Ok(PyList::from(matchlist).into_ref(vm))
279+
Ok(matchlist)
281280
},
282281
)
283282
}
@@ -332,7 +331,7 @@ mod _sre {
332331
zelf: PyRef<Pattern>,
333332
split_args: SplitArgs,
334333
vm: &VirtualMachine,
335-
) -> PyResult<PyListRef> {
334+
) -> PyResult<Vec<PyObjectRef>> {
336335
zelf.with_state(split_args.string.clone(), 0, usize::MAX, vm, |mut state| {
337336
let mut splitlist: Vec<PyObjectRef> = Vec::new();
338337

@@ -367,7 +366,7 @@ mod _sre {
367366
// get segment following last match (even if empty)
368367
splitlist.push(slice_drive(&state.string, last, state.string.count(), vm));
369368

370-
Ok(PyList::from(splitlist).into_ref(vm))
369+
Ok(splitlist)
371370
})
372371
}
373372

vm/src/stdlib/sys.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ mod sys {
1111
hash::{PyHash, PyUHash},
1212
};
1313
use crate::{
14-
builtins::{PyDictRef, PyListRef, PyNamespace, PyStr, PyStrRef, PyTupleRef, PyTypeRef},
14+
builtins::{PyDictRef, PyNamespace, PyStr, PyStrRef, PyTupleRef, PyTypeRef},
1515
frame::FrameRef,
1616
function::{FuncArgs, OptionalArg, PosArgs},
1717
stdlib::builtins,
@@ -99,15 +99,13 @@ mod sys {
9999
// alphabetical order with segments of pyattr and others
100100

101101
#[pyattr]
102-
fn argv(vm: &VirtualMachine) -> PyListRef {
103-
vm.ctx.new_list(
104-
vm.state
105-
.settings
106-
.argv
107-
.iter()
108-
.map(|arg| vm.ctx.new_str(arg.clone()).into())
109-
.collect(),
110-
)
102+
fn argv(vm: &VirtualMachine) -> Vec<PyObjectRef> {
103+
vm.state
104+
.settings
105+
.argv
106+
.iter()
107+
.map(|arg| vm.ctx.new_str(arg.clone()).into())
108+
.collect()
111109
}
112110

113111
#[pyattr]

0 commit comments

Comments
 (0)