Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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 @@ -665,6 +665,7 @@ CPyTagged CPyList_Index(PyObject *list, PyObject *obj);
PyObject *CPySequence_Multiply(PyObject *seq, CPyTagged t_size);
PyObject *CPySequence_RMultiply(CPyTagged t_size, PyObject *seq);
PyObject *CPyList_GetSlice(PyObject *obj, CPyTagged start, CPyTagged end);
PyObject *CPyList_Copy(PyObject *list);
int CPySequence_Check(PyObject *obj);


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

PyObject *CPyList_Copy(PyObject *list) {
if(PyList_CheckExact(list)) {
return PyList_GetSlice(list, 0, PyList_GET_SIZE(list));
}
_Py_IDENTIFIER(copy);

PyObject *name = _PyUnicode_FromId(&PyId_copy);
if (name == NULL) {
return NULL;
}
return PyObject_CallMethodNoArgs(list, name);
}


PyObject *CPyList_GetItemUnsafe(PyObject *list, CPyTagged index) {
Py_ssize_t n = CPyTagged_ShortAsSsize_t(index);
PyObject *result = PyList_GET_ITEM(list, n);
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 @@ -262,6 +262,15 @@
error_kind=ERR_MAGIC,
)

# list.copy()
method_op(
name="copy",
arg_types=[list_rprimitive],
return_type=list_rprimitive,
c_function_name="CPyList_Copy",
error_kind=ERR_MAGIC,
)

# list * int
binary_op(
name="*",
Expand Down
31 changes: 31 additions & 0 deletions test-data/unit/check-listcopy
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[case testListCopy]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a duplicate and can be removed.

[builtins fixtures/list.pyi]
def test_list_copy() -> None:
l1 = [1, 2, 3, -4, 5]
l2 = l1.copy()
assert l1.copy() == l1
assert l1.copy() == l2
assert l1 == l2
assert l1.copy() == l2.copy()
l1 = l2.copy()
assert l1 == l2
assert l1.copy() == l2
assert l1 == [1, 2, 3, -4, 5]
l2 = [1, 2, -3]
l1 = []
assert l1.copy() == []
assert l2.copy() != l1
assert l2 == l2.copy()
l1 = l2
assert l1.copy().copy() == l2.copy().copy().copy()
assert l1.copy() == l2.copy()
l1 == [1, 2, -3].copy()
assert l1 == l2
l2 = [1, 2, 3].copy()
assert l2 != l1
l1 = [1, 2, 3]
assert l1.copy() == l2.copy()
l3 = ["string", 2 , 3]
assert l3 == l3.copy()
l2 = ["string", 2, 3]
assert l2 == l3.copy()
31 changes: 31 additions & 0 deletions test-data/unit/check-listcopy.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[case testListCopy]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a mypy test case, which doesn't test the change made in this PR. Add test cases to mypyc/test-data/run-lists.test and mypyc/test-data/irbuild-lists.test instead, and remove this file. The irbuild test case only needs to test a single call to ensure that the new primitive is being used.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also maybe test calling copy on a subclass of list, when the subclass instance has been assigned to a variable with list type (so that the new primitive will be triggered).

Copy link
Contributor Author

@exertustfm exertustfm Mar 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's a subclass I can use in the test? I'm not familiar with mypy and things didn't work out when I tried to make a subclass inside the test.

When running tests, I get native.py:2: error: Inheriting from most builtin types is unimplemented error in run-lists.test

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll need to define the subclass in a module that is not compiled. You can achieve this by putting the class definition in a separate file using [file interp.py] in run-*.test file. Example:

[case testSomething]
from interp import C

# this code will be compiled

[file interp.py]
# This file is not compiled, so you can do more things here
class C(list): 
    pass

[builtins fixtures/list.pyi]
def test_list_copy() -> None:
l1 = [1, 2, 3, -4, 5]
l2 = l1.copy()
assert l1.copy() == l1
assert l1.copy() == l2
assert l1 == l2
assert l1.copy() == l2.copy()
l1 = l2.copy()
assert l1 == l2
assert l1.copy() == l2
assert l1 == [1, 2, 3, -4, 5]
l2 = [1, 2, -3]
l1 = []
assert l1.copy() == []
assert l2.copy() != l1
assert l2 == l2.copy()
l1 = l2
assert l1.copy().copy() == l2.copy().copy().copy()
assert l1.copy() == l2.copy()
l1 == [1, 2, -3].copy()
assert l1 == l2
l2 = [1, 2, 3].copy()
assert l2 != l1
l1 = [1, 2, 3]
assert l1.copy() == l2.copy()
l3 = ["string", 2 , 3]
assert l3 == l3.copy()
l2 = ["string", 2, 3]
assert l2 == l3.copy()
1 change: 1 addition & 0 deletions test-data/unit/fixtures/list.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class list(Sequence[T]):
def __setitem__(self, x: int, v: T) -> None: pass
def append(self, x: T) -> None: pass
def extend(self, x: Iterable[T]) -> None: pass
def copy(self) -> list[T]: pass
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is again only targeting mypy (not mypyc) test cases. Please update mypyc/test-data/fixtures/ir.py instead and revert this change.


class tuple(Generic[T]): pass
class function: pass
Expand Down