Skip to content
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 crates/capi/src/abstract_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mod sequence;
const PY_VECTORCALL_ARGUMENTS_OFFSET: usize = 1usize << (usize::BITS as usize - 1);

fn tuple_to_args(tuple: &Py<PyTuple>) -> PosArgs {
tuple.iter().cloned().collect::<Vec<_>>().into()
tuple.as_slice().to_vec().into()
}

fn dict_to_kwargs(vm: &VirtualMachine, dict: &Py<PyDict>) -> PyResult<KwArgs> {
Expand Down Expand Up @@ -89,7 +89,7 @@ pub unsafe extern "C" fn PyObject_Vectorcall(
let kwnames: Option<&[PyObjectRef]> = unsafe {
kwnames
.assume_borrowed_or_opt()
.map(|tuple| Ok(&***tuple.try_downcast_ref::<PyTuple>(vm)?))
.map(|tuple| Ok(tuple.try_downcast_ref::<PyTuple>(vm)?.as_slice()))
.transpose()?
};

Expand Down Expand Up @@ -150,7 +150,7 @@ pub unsafe extern "C" fn PyVectorcall_Call(
let callable = unsafe { callable.assume_borrowed() };
let tuple = unsafe { tuple.assume_borrowed_and_cast::<PyTuple>(vm) }?;

let mut args = tuple.iter().cloned().collect::<Vec<_>>();
let mut args = tuple.as_slice().to_vec();
let num_positional_args = args.len();

let mut kwnames = Vec::new();
Expand Down
2 changes: 1 addition & 1 deletion crates/capi/src/complexobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn try_to_complex(vm: &VirtualMachine, obj: &PyObject) -> PyResult<Complex64> {
Err(type_err)
}
},
|complex| Ok(complex.to_complex()),
|complex| Ok(complex.as_complex()),
)
}

Expand Down
1 change: 1 addition & 0 deletions crates/capi/src/pyerrors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ pub unsafe extern "C" fn PyErr_NewException(
vec![ty.to_owned()]
} else if let Some(tuple) = bases.downcast_ref::<PyTuple>() {
tuple
.as_slice()
.iter()
.map(|item| item.to_owned().downcast())
.collect::<Result<Vec<_>, _>>()
Expand Down
8 changes: 4 additions & 4 deletions crates/capi/src/tupleobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub extern "C" fn PyTuple_SetItem(
pub unsafe extern "C" fn PyTuple_Size(tuple: *mut PyObject) -> isize {
with_vm(|vm| {
let tuple = unsafe { tuple.assume_borrowed_and_cast::<PyTuple>(vm) }?;
Ok(tuple.__len__())
Ok(tuple.as_slice().len())
})
}

Expand All @@ -68,7 +68,7 @@ pub unsafe extern "C" fn PyTuple_GetItem(tuple: *mut PyObject, pos: isize) -> *m
let result: &PyObject = pos
.try_into()
.ok()
.and_then(|index: usize| tuple.get(index))
.and_then(|index: usize| tuple.as_slice().get(index))
.ok_or_else(|| vm.new_index_error("tuple index out of range"))?;

Ok(result.as_raw())
Expand All @@ -83,10 +83,10 @@ pub unsafe extern "C" fn PyTuple_GetSlice(
) -> *mut PyObject {
with_vm(|vm| {
let tuple = unsafe { tuple.assume_borrowed_and_cast::<PyTuple>(vm) }?;
let len = tuple.__len__() as isize;
let len = tuple.as_slice().len() as isize;
let low = low.clamp(0, len);
let high = high.clamp(low, len);
let slice = tuple.do_slice(low as usize..high as usize);
let slice = tuple.as_slice().do_slice(low as usize..high as usize);
Ok(vm.ctx.new_tuple(slice))
})
}
Expand Down
14 changes: 8 additions & 6 deletions crates/stdlib/src/_asyncio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ pub(crate) mod _asyncio {
let item = list.borrow_vec().first().cloned();
if let Some(item) = item {
let tuple: &Py<PyTuple> = item.downcast_ref().unwrap();
let cb = tuple.first().unwrap().clone();
let cb = tuple.as_slice().first().unwrap().clone();
let cmp = vm.identical_or_equal(&cb, &func)?;
if cmp {
*self.fut_callbacks.write() = None;
Expand Down Expand Up @@ -445,7 +445,7 @@ pub(crate) mod _asyncio {
};

let tuple: &Py<PyTuple> = item.downcast_ref().unwrap();
let cb = tuple.first().unwrap().clone();
let cb = tuple.as_slice().first().unwrap().clone();
let cmp = vm.identical_or_equal(&cb, &func)?;

if !cmp {
Expand Down Expand Up @@ -544,7 +544,8 @@ pub(crate) mod _asyncio {
let items: Vec<_> = list.borrow_vec().iter().cloned().collect();
for item in items {
if let Some(tuple) = item.downcast_ref::<PyTuple>()
&& let (Some(cb), Some(ctx)) = (tuple.first(), tuple.get(1))
&& let (Some(cb), Some(ctx)) =
(tuple.as_slice().first(), tuple.as_slice().get(1))
{
callbacks_to_call.push((cb.clone(), Some(ctx.clone())));
}
Expand Down Expand Up @@ -1425,7 +1426,7 @@ pub(crate) mod _asyncio {
let item = list.borrow_vec().first().cloned();
if let Some(item) = item {
let tuple: &Py<PyTuple> = item.downcast_ref().unwrap();
let cb = tuple.first().unwrap().clone();
let cb = tuple.as_slice().first().unwrap().clone();
let cmp = vm.identical_or_equal(&cb, &func)?;
if cmp {
*self.base.fut_callbacks.write() = None;
Expand Down Expand Up @@ -1462,7 +1463,7 @@ pub(crate) mod _asyncio {
};

let tuple: &Py<PyTuple> = item.downcast_ref().unwrap();
let cb = tuple.first().unwrap().clone();
let cb = tuple.as_slice().first().unwrap().clone();
let cmp = vm.identical_or_equal(&cb, &func)?;

if !cmp {
Expand Down Expand Up @@ -1504,7 +1505,8 @@ pub(crate) mod _asyncio {
let items: Vec<_> = list.borrow_vec().iter().cloned().collect();
for item in items {
if let Some(tuple) = item.downcast_ref::<PyTuple>()
&& let (Some(cb), Some(ctx)) = (tuple.first(), tuple.get(1))
&& let (Some(cb), Some(ctx)) =
(tuple.as_slice().first(), tuple.as_slice().get(1))
{
callbacks_to_call.push((cb.clone(), Some(ctx.clone())));
}
Expand Down
19 changes: 14 additions & 5 deletions crates/stdlib/src/_sqlite3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2372,6 +2372,7 @@ mod _sqlite3 {
#[pymethod]
fn keys(&self, _vm: &VirtualMachine) -> Vec<PyObjectRef> {
self.description
.as_slice()
.iter()
.map(|x| x.downcast_ref::<PyTuple>().unwrap().as_slice()[0].clone())
.collect()
Expand All @@ -2380,9 +2381,9 @@ mod _sqlite3 {
fn subscript(&self, needle: &PyObject, vm: &VirtualMachine) -> PyResult {
if let Some(i) = needle.downcast_ref::<PyInt>() {
let i = i.try_to_primitive::<isize>(vm)?;
self.data.getitem_by_index(vm, i)
self.data.as_slice().getitem_by_index(vm, i)
} else if let Some(name) = needle.downcast_ref::<PyStr>() {
for (obj, i) in self.description.iter().zip(0..) {
for (obj, i) in self.description.as_slice().iter().zip(0..) {
let obj = &obj.downcast_ref::<PyTuple>().unwrap().as_slice()[0];
let Some(obj) = obj.downcast_ref::<PyStr>() else {
break;
Expand All @@ -2391,12 +2392,15 @@ mod _sqlite3 {
let b_iter = obj.expect_str().chars().flat_map(|x| x.to_uppercase());

if a_iter.eq(b_iter) {
return self.data.getitem_by_index(vm, i);
return self.data.as_slice().getitem_by_index(vm, i);
}
}
Err(vm.new_index_error(format!("No item with key '{}'", name.to_string_lossy())))
} else if let Some(slice) = needle.downcast_ref::<PySlice>() {
let list = self.data.getitem_by_slice(vm, slice.to_saturated(vm)?)?;
let list = self
.data
.as_slice()
.getitem_by_slice(vm, slice.to_saturated(vm)?)?;
Ok(vm.ctx.new_tuple(list).into())
} else {
Err(vm.new_index_error("Index must be int or string"))
Expand Down Expand Up @@ -2460,6 +2464,7 @@ mod _sqlite3 {
std::sync::LazyLock::new(|| PyMappingMethods {
length: atomic_func!(|mapping, _vm| Ok(Row::mapping_downcast(mapping)
.data
.as_slice()
.len())),
subscript: atomic_func!(|mapping, needle, vm| {
Row::mapping_downcast(mapping).subscript(needle, vm)
Expand All @@ -2474,9 +2479,13 @@ mod _sqlite3 {
fn as_sequence() -> &'static PySequenceMethods {
static AS_SEQUENCE: std::sync::LazyLock<PySequenceMethods> =
std::sync::LazyLock::new(|| PySequenceMethods {
length: atomic_func!(|seq, _vm| Ok(Row::sequence_downcast(seq).data.len())),
length: atomic_func!(|seq, _vm| Ok(Row::sequence_downcast(seq)
.data
.as_slice()
.len())),
item: atomic_func!(|seq, i, vm| Row::sequence_downcast(seq)
.data
.as_slice()
.getitem_by_index(vm, i)),
..PySequenceMethods::NOT_IMPLEMENTED
});
Expand Down
4 changes: 2 additions & 2 deletions crates/stdlib/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ pub mod array {
($($t:ty,)*) => {$(
impl ArrayElement for $t {
fn try_into_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
obj.try_index(vm)?.try_to_primitive_raw(vm)
obj.try_index(vm)?.try_to_primitive_in_range(vm)
}
fn byteswap(self) -> Self {
<$t>::swap_bytes(self)
Expand Down Expand Up @@ -955,7 +955,7 @@ pub mod array {
.downcast::<PyBytes>()
.map_err(|_| vm.new_type_error("read() didn't return bytes"))?;

let not_enough_bytes = b.len() != n_bytes;
let not_enough_bytes = b.as_bytes().len() != n_bytes;

self._from_bytes(b.as_bytes(), itemsize, vm)?;

Expand Down
4 changes: 2 additions & 2 deletions crates/stdlib/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ mod _json {
Ok(tuple) => {
use crate::vm::builtins::PyTupleRef;
let tuple: PyTupleRef = tuple.try_into_value(vm)?;
if tuple.len() != 2 {
if tuple.as_slice().len() != 2 {
return Err(vm.new_value_error("scan_once must return 2-tuple"));
}
let value = tuple.as_slice()[0].clone();
Expand Down Expand Up @@ -1144,7 +1144,7 @@ mod _json {
vm.extract_elements_with(&items_obj, |item| {
use crate::vm::builtins::PyTupleRef;
let tuple: PyTupleRef = item.try_into_value(vm)?;
if tuple.len() != 2 {
if tuple.as_slice().len() != 2 {
return Err(vm.new_value_error("items() must return 2-tuples"));
}
let slice = tuple.as_slice();
Expand Down
14 changes: 7 additions & 7 deletions crates/stdlib/src/overlapped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,11 @@ mod _overlapped {

/// Parse a Python address tuple to SOCKADDR
fn parse_address(addr_obj: &Py<PyTuple>, vm: &VirtualMachine) -> PyResult<(Vec<u8>, i32)> {
match addr_obj.len() {
match addr_obj.as_slice().len() {
2 => {
// IPv4: (host, port)
let host: PyStrRef = addr_obj[0].clone().try_into_value(vm)?;
let port: u16 = addr_obj[1].clone().try_to_value(vm)?;
let host: PyStrRef = addr_obj.as_slice()[0].clone().try_into_value(vm)?;
let port: u16 = addr_obj.as_slice()[1].clone().try_to_value(vm)?;
let host_wide = host
.as_wtf8()
.to_wide_cstring()
Expand All @@ -222,10 +222,10 @@ mod _overlapped {
}
4 => {
// IPv6: (host, port, flowinfo, scope_id)
let host: PyStrRef = addr_obj[0].clone().try_into_value(vm)?;
let port: u16 = addr_obj[1].clone().try_to_value(vm)?;
let flowinfo: u32 = addr_obj[2].clone().try_to_value(vm)?;
let scope_id: u32 = addr_obj[3].clone().try_to_value(vm)?;
let host: PyStrRef = addr_obj.as_slice()[0].clone().try_into_value(vm)?;
let port: u16 = addr_obj.as_slice()[1].clone().try_to_value(vm)?;
let flowinfo: u32 = addr_obj.as_slice()[2].clone().try_to_value(vm)?;
let scope_id: u32 = addr_obj.as_slice()[3].clone().try_to_value(vm)?;
let host_wide = host
.as_wtf8()
.to_wide_cstring()
Expand Down
16 changes: 9 additions & 7 deletions crates/stdlib/src/pickle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ mod _pickle {
let mapped: PyTupleRef = mapped.downcast().map_err(|_| {
vm.new_runtime_error("_compat_pickle.NAME_MAPPING values must be 2-tuples")
})?;
if mapped.len() != 2 {
if mapped.as_slice().len() != 2 {
return Err(
vm.new_runtime_error("_compat_pickle.NAME_MAPPING values must be 2-tuples")
);
Expand Down Expand Up @@ -1533,7 +1533,9 @@ mod _pickle {
return Ok(());
}
let (state, slotstate) = match state.downcast_ref::<PyTuple>() {
Some(t) if t.len() == 2 => (t.as_slice()[0].clone(), Some(t.as_slice()[1].clone())),
Some(t) if t.as_slice().len() == 2 => {
(t.as_slice()[0].clone(), Some(t.as_slice()[1].clone()))
}
_ => (state, None),
};
if !vm.is_none(&state) && state.try_to_bool(vm)? {
Expand Down Expand Up @@ -1592,7 +1594,7 @@ mod _pickle {
let key: PyTupleRef = key
.downcast()
.map_err(|_| vm.new_value_error("_inverted_registry values must be 2-tuples"))?;
if key.len() != 2 {
if key.as_slice().len() != 2 {
return Err(vm.new_value_error("_inverted_registry values must be 2-tuples"));
}
let module = key.as_slice()[0].clone();
Expand Down Expand Up @@ -2077,7 +2079,7 @@ mod _pickle {
let pair: PyTupleRef = val
.downcast()
.map_err(|_| vm.new_type_error("'memo' values must be 2-item tuples"))?;
if pair.len() != 2 {
if pair.as_slice().len() != 2 {
return Err(vm.new_type_error("'memo' values must be 2-item tuples"));
}
let idx = pair.as_slice()[0]
Expand Down Expand Up @@ -2494,7 +2496,7 @@ mod _pickle {

fn save_tuple(&mut self, obj: &PyObject, vm: &VirtualMachine) -> PyResult<()> {
let tuple = obj.downcast_ref::<PyTuple>().expect("tuple");
let n = tuple.len();
let n = tuple.as_slice().len();
if n == 0 {
if self.bin {
self.write(&[EMPTY_TUPLE as u8]);
Expand Down Expand Up @@ -2750,7 +2752,7 @@ mod _pickle {
let pair: PyTupleRef = o.downcast().map_err(|_| {
vm.new_type_error("dict items iterator must return 2-tuples")
})?;
if pair.len() != 2 {
if pair.as_slice().len() != 2 {
return Err(
vm.new_type_error("dict items iterator must return 2-tuples")
);
Expand Down Expand Up @@ -3616,7 +3618,7 @@ mod _pickle {
Ok(p) => p,
Err(_) => continue,
};
if pair.len() != 2 {
if pair.as_slice().len() != 2 {
continue;
}
let (key, module) = (pair.as_slice()[0].clone(), pair.as_slice()[1].clone());
Expand Down
Loading
Loading