Less PyListRef

This commit is contained in:
Jeong YunWon
2021-10-13 02:48:03 +09:00
parent 7377664bea
commit 997f85ff54
5 changed files with 35 additions and 41 deletions

View File

@@ -67,26 +67,23 @@ mod termios {
termios::cfsetospeed(&mut termios, ospeed.try_into_value(vm)?)
.map_err(|e| termios_error(e, vm))?;
let cc = PyListRef::try_from_object(vm, cc)?;
{
let cc = cc.borrow_vec();
let cc = <&[PyObjectRef; NCCS]>::try_from(&*cc).map_err(|_| {
vm.new_type_error(format!(
"tcsetattr: attributes[6] must be {} element list",
NCCS
))
})?;
for (cc, x) in termios.c_cc.iter_mut().zip(cc.iter()) {
*cc = if let Some(c) = x.payload::<PyBytes>().filter(|b| b.as_bytes().len() == 1) {
c.as_bytes()[0] as _
} else if let Some(i) = x.payload::<PyInt>() {
i.try_to_primitive(vm)?
} else {
return Err(vm.new_type_error(
"tcsetattr: elements of attributes must be characters or integers"
.to_owned(),
));
};
}
let cc = cc.borrow_vec();
let cc = <&[PyObjectRef; NCCS]>::try_from(&*cc).map_err(|_| {
vm.new_type_error(format!(
"tcsetattr: attributes[6] must be {} element list",
NCCS
))
})?;
for (cc, x) in termios.c_cc.iter_mut().zip(cc.iter()) {
*cc = if let Some(c) = x.payload::<PyBytes>().filter(|b| b.as_bytes().len() == 1) {
c.as_bytes()[0] as _
} else if let Some(i) = x.payload::<PyInt>() {
i.try_to_primitive(vm)?
} else {
return Err(vm.new_type_error(
"tcsetattr: elements of attributes must be characters or integers".to_owned(),
));
};
}
termios::tcsetattr(fd, when, &termios).map_err(|e| termios_error(e, vm))?;

View File

@@ -111,7 +111,7 @@ impl PyList {
}
#[pymethod(magic)]
fn add(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyListRef> {
fn add(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
let other = other.payload_if_subclass::<PyList>(vm).ok_or_else(|| {
vm.new_type_error(format!(
"Cannot add {} and {}",
@@ -146,7 +146,7 @@ impl PyList {
}
#[pymethod]
fn copy(&self, vm: &VirtualMachine) -> PyListRef {
fn copy(&self, vm: &VirtualMachine) -> PyRef<Self> {
Self::new_ref(self.borrow_vec().to_vec(), &vm.ctx)
}

View File

@@ -501,7 +501,7 @@ impl PyMemoryView {
}
#[pymethod]
fn tolist(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult<PyListRef> {
fn tolist(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult<Vec<PyObjectRef>> {
zelf.try_not_released(vm)?;
let bytes = &*zelf.obj_bytes();
@@ -516,7 +516,7 @@ impl PyMemoryView {
})
.try_collect()?;
Ok(PyList::from(elements).into_ref(vm))
Ok(elements)
}
#[pymethod]
@@ -845,7 +845,7 @@ fn format_unpack(
})
}
pub fn unpack_bytes_seq_to_list(
fn unpack_bytes_seq_to_list(
bytes: &[u8],
format: &str,
vm: &VirtualMachine,

View File

@@ -4,8 +4,7 @@ pub(crate) use _sre::make_module;
mod _sre {
use crate::{
builtins::{
PyCallableIterator, PyDictRef, PyInt, PyList, PyListRef, PyStr, PyStrRef, PyTuple,
PyTupleRef,
PyCallableIterator, PyDictRef, PyInt, PyList, PyStr, PyStrRef, PyTuple, PyTupleRef,
},
common::{ascii, hash::PyHash},
function::{ArgCallable, IntoPyObject, OptionalArg, PosArgs},
@@ -247,7 +246,7 @@ mod _sre {
zelf: PyRef<Pattern>,
string_args: StringArgs,
vm: &VirtualMachine,
) -> PyResult<PyListRef> {
) -> PyResult<Vec<PyObjectRef>> {
zelf.with_state(
string_args.string.clone(),
string_args.pos,
@@ -277,7 +276,7 @@ mod _sre {
state.start = state.string_position;
state.reset();
}
Ok(PyList::from(matchlist).into_ref(vm))
Ok(matchlist)
},
)
}
@@ -332,7 +331,7 @@ mod _sre {
zelf: PyRef<Pattern>,
split_args: SplitArgs,
vm: &VirtualMachine,
) -> PyResult<PyListRef> {
) -> PyResult<Vec<PyObjectRef>> {
zelf.with_state(split_args.string.clone(), 0, usize::MAX, vm, |mut state| {
let mut splitlist: Vec<PyObjectRef> = Vec::new();
@@ -367,7 +366,7 @@ mod _sre {
// get segment following last match (even if empty)
splitlist.push(slice_drive(&state.string, last, state.string.count(), vm));
Ok(PyList::from(splitlist).into_ref(vm))
Ok(splitlist)
})
}

View File

@@ -11,7 +11,7 @@ mod sys {
hash::{PyHash, PyUHash},
};
use crate::{
builtins::{PyDictRef, PyListRef, PyNamespace, PyStr, PyStrRef, PyTupleRef, PyTypeRef},
builtins::{PyDictRef, PyNamespace, PyStr, PyStrRef, PyTupleRef, PyTypeRef},
frame::FrameRef,
function::{FuncArgs, OptionalArg, PosArgs},
stdlib::builtins,
@@ -99,15 +99,13 @@ mod sys {
// alphabetical order with segments of pyattr and others
#[pyattr]
fn argv(vm: &VirtualMachine) -> PyListRef {
vm.ctx.new_list(
vm.state
.settings
.argv
.iter()
.map(|arg| vm.ctx.new_str(arg.clone()).into())
.collect(),
)
fn argv(vm: &VirtualMachine) -> Vec<PyObjectRef> {
vm.state
.settings
.argv
.iter()
.map(|arg| vm.ctx.new_str(arg.clone()).into())
.collect()
}
#[pyattr]