Skip to content

[mypyc] Implement list.clear() primitive #19344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 1, 2025
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
1 change: 1 addition & 0 deletions mypyc/lib-rt/CPy.h
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,7 @@ PyObject *CPySequence_Multiply(PyObject *seq, CPyTagged t_size);
PyObject *CPySequence_RMultiply(CPyTagged t_size, PyObject *seq);
PyObject *CPySequence_InPlaceMultiply(PyObject *seq, CPyTagged t_size);
PyObject *CPyList_GetSlice(PyObject *obj, CPyTagged start, CPyTagged end);
char CPyList_Clear(PyObject *list);
PyObject *CPyList_Copy(PyObject *list);
int CPySequence_Check(PyObject *obj);

Expand Down
17 changes: 17 additions & 0 deletions mypyc/lib-rt/list_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@ PyObject *CPyList_Build(Py_ssize_t len, ...) {
return res;
}

char CPyList_Clear(PyObject *list) {
if (PyList_CheckExact(list)) {
PyList_Clear(list);
} else {
_Py_IDENTIFIER(clear);
PyObject *name = _PyUnicode_FromId(&PyId_clear);
if (name == NULL) {
return 0;
}
PyObject *res = PyObject_CallMethodNoArgs(list, name);
if (res == NULL) {
return 0;
}
}
return 1;
}

PyObject *CPyList_Copy(PyObject *list) {
if(PyList_CheckExact(list)) {
return PyList_GetSlice(list, 0, PyList_GET_SIZE(list));
Expand Down
9 changes: 9 additions & 0 deletions mypyc/primitives/list_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,15 @@
error_kind=ERR_MAGIC,
)

# list.clear()
method_op(
name="clear",
arg_types=[list_rprimitive],
return_type=bit_rprimitive,
c_function_name="CPyList_Clear",
error_kind=ERR_FALSE,
)

# list.copy()
method_op(
name="copy",
Expand Down
1 change: 1 addition & 0 deletions mypyc/test-data/fixtures/ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ def sort(self) -> None: pass
def reverse(self) -> None: pass
def remove(self, o: _T) -> None: pass
def index(self, o: _T) -> int: pass
def clear(self) -> None: pass
def copy(self) -> List[_T]: pass

class dict(Mapping[_K, _V]):
Expand Down
12 changes: 12 additions & 0 deletions mypyc/test-data/irbuild-lists.test
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,18 @@ L0:
r1 = r0 << 1
return r1

[case testListClear]
from typing import List
def f(l: List[int]) -> None:
return l.clear()
[out]
def f(l):
l :: list
r0 :: bit
L0:
r0 = CPyList_Clear(l)
return 1

[case testListCopy]
from typing import List
from typing import Any
Expand Down
26 changes: 26 additions & 0 deletions mypyc/test-data/run-lists.test
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,32 @@ print(2, a)
1 [-1, 5]
2 [340282366920938463463374607431768211461, -170141183460469231731687303715884105736]

[case testListClear]
from typing import List, Any
from copysubclass import subc

def test_list_clear() -> None:
l1 = [1, 2, 3, -4, 5]
l1.clear()
assert l1 == []
l1.clear()
assert l1 == []
l2: List[Any] = []
l2.clear()
assert l2 == []
l3 = [1, 2, 3, "abcdef"]
l3.clear()
assert l3 == []
# subclass testing
l4: subc = subc([1, 2, 3])
l4.clear()
assert l4 == []

[file copysubclass.py]
from typing import Any
class subc(list[Any]):
pass

[case testListCopy]
from typing import List
from copysubclass import subc
Expand Down