Skip to content

Commit a90cbe6

Browse files
committed
Move PyList Python methods and operators to Py<PyList>
The #[pymethod]s, #[pyclassmethod] __class_getitem__ and the operators (__add__, which replaces concat(), __iadd__, __mul__, __imul__, __getitem__, __setitem__, __delitem__, __contains__, inplace_concat) are defined in a #[pyclass] impl of Py<PyList>, registered through with(Py, ...). The #[pyclass] impl of PyList keeps the class attributes and is empty. PyList keeps the helpers that work on the payload (_getitem, _setitem, _delitem, repeat, irepeat and sort, which also sorts PyList values that have no object yet); Py<PyList> reaches them through the payload field. The mapping and sequence slots call the Py<PyList> operators. PyList::__len__() is removed; its callers read borrow_vec().len(). GenericAlias.__dir__ appends to the PyList returned by vm.dir() through borrow_vec_mut() and checks membership with mut_contains(). Assisted-by: Claude Code:claude-opus-5-5
1 parent 90afe00 commit a90cbe6

5 files changed

Lines changed: 139 additions & 140 deletions

File tree

‎crates/capi/src/listobject.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub extern "C" fn PyList_New(size: isize) -> *mut PyObject {
2525
pub unsafe extern "C" fn PyList_Size(obj: *mut PyObject) -> isize {
2626
with_vm(|vm| {
2727
let list = unsafe { obj.assume_borrowed_and_cast::<PyList>(vm) }?;
28-
Ok(list.__len__())
28+
Ok(list.borrow_vec().len())
2929
})
3030
}
3131

‎crates/stdlib/src/_heapq.rs‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ mod _heapq {
1717
mut pos: usize,
1818
vm: &VirtualMachine,
1919
) -> PyResult<()> {
20-
let size = heap.__len__();
20+
let size = heap.borrow_vec().len();
2121

2222
let newitem = match heap.borrow_vec().get(pos) {
2323
Some(v) => v.clone(),
@@ -35,7 +35,7 @@ mod _heapq {
3535

3636
let cmp = newitem.rich_compare_bool(&parent, PyComparisonOp::Lt, vm)?;
3737

38-
if size != heap.__len__() {
38+
if size != heap.borrow_vec().len() {
3939
return Err(vm.new_runtime_error("list changed size during iteration"));
4040
}
4141

@@ -56,7 +56,7 @@ mod _heapq {
5656

5757
/// [CPython's siftup](https://github.com/python/cpython/blob/v3.14.5/Modules/_heapqmodule.c#L70-L118)
5858
fn siftup(heap: &Py<PyList>, mut pos: usize, vm: &VirtualMachine) -> PyResult<()> {
59-
let endpos = heap.__len__();
59+
let endpos = heap.borrow_vec().len();
6060
let startpos = pos;
6161

6262
if pos >= endpos {
@@ -78,7 +78,7 @@ mod _heapq {
7878

7979
let cmp = a.rich_compare_bool(&b, PyComparisonOp::Lt, vm)?;
8080

81-
if endpos != heap.__len__() {
81+
if endpos != heap.borrow_vec().len() {
8282
return Err(vm.new_runtime_error("list changed size during iteration"));
8383
}
8484

@@ -119,7 +119,7 @@ mod _heapq {
119119
vec.push(item);
120120
}
121121

122-
let size = heap.__len__();
122+
let size = heap.borrow_vec().len();
123123

124124
siftdown_func(heap, 0, size - 1, vm)
125125
}
@@ -251,7 +251,7 @@ mod _heapq {
251251
where
252252
F: Fn(&Py<PyList>, usize, &VirtualMachine) -> PyResult<()>,
253253
{
254-
let m = heap.__len__() >> 1; // index of first childless node
254+
let m = heap.borrow_vec().len() >> 1; // index of first childless node
255255
let leftmost = keep_top_bit(m + 1) - 1; // leftmost node in row of m
256256
let mhalf = m >> 1; // parent of first childless node
257257

@@ -291,7 +291,7 @@ mod _heapq {
291291
where
292292
F: Fn(&Py<PyList>, usize, &VirtualMachine) -> PyResult<()>,
293293
{
294-
let n = heap.__len__();
294+
let n = heap.borrow_vec().len();
295295

296296
if n > 2500 {
297297
return cache_friendly_heapify(heap, siftup_func, vm);
@@ -316,7 +316,7 @@ mod _heapq {
316316
mut pos: usize,
317317
vm: &VirtualMachine,
318318
) -> PyResult<()> {
319-
let size = heap.__len__();
319+
let size = heap.borrow_vec().len();
320320

321321
let newitem = match heap.borrow_vec().get(pos) {
322322
Some(v) => v.clone(),
@@ -334,7 +334,7 @@ mod _heapq {
334334

335335
let cmp = parent.rich_compare_bool(&newitem, PyComparisonOp::Lt, vm)?;
336336

337-
if size != heap.__len__() {
337+
if size != heap.borrow_vec().len() {
338338
return Err(vm.new_runtime_error("list changed size during iteration"));
339339
}
340340

@@ -355,7 +355,7 @@ mod _heapq {
355355

356356
/// [CPython's siftup_max](https://github.com/python/cpython/blob/v3.14.5/Modules/_heapqmodule.c#L451-L499)
357357
fn siftup_max(heap: &Py<PyList>, mut pos: usize, vm: &VirtualMachine) -> PyResult<()> {
358-
let endpos = heap.__len__();
358+
let endpos = heap.borrow_vec().len();
359359
let startpos = pos;
360360

361361
if pos >= endpos {
@@ -377,7 +377,7 @@ mod _heapq {
377377

378378
let cmp = a.rich_compare_bool(&b, PyComparisonOp::Lt, vm)?;
379379

380-
if endpos != heap.__len__() {
380+
if endpos != heap.borrow_vec().len() {
381381
return Err(vm.new_runtime_error("list changed size during iteration"));
382382
}
383383

‎crates/vm/src/builtins/genericalias.rs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::{
1111
convert::ToPyObject,
1212
function::{FuncArgs, PyComparisonValue},
1313
protocol::{PyMappingMethods, PyNumberMethods},
14+
sequence::MutObjectSequenceOp,
1415
types::{
1516
AsMapping, AsNumber, Callable, Comparable, Constructor, GetAttr, Hashable, IterNext,
1617
Iterable, PyComparisonOp, Representable,
@@ -244,8 +245,8 @@ impl PyGenericAlias {
244245
let dir = vm.dir(Some(self.origin.clone()))?;
245246
for exc in &ATTR_EXCEPTIONS {
246247
let exc_obj = (*exc).to_pyobject(vm);
247-
if !dir.__contains__(&exc_obj, vm)? {
248-
dir.append((*exc).to_pyobject(vm));
248+
if !dir.mut_contains(vm, &exc_obj)? {
249+
dir.borrow_vec_mut().push((*exc).to_pyobject(vm));
249250
}
250251
}
251252
Ok(dir)

0 commit comments

Comments
 (0)