Skip to content

Commit 032d073

Browse files
committed
Implement list insert primitive
1 parent 849a7f7 commit 032d073

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

mypyc/primitives/list_ops.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@
105105
c_function_name='CPyList_Count',
106106
error_kind=ERR_MAGIC)
107107

108+
# list.insert(index, obj)
109+
method_op(
110+
name='insert',
111+
arg_types=[list_rprimitive, int_rprimitive, object_rprimitive],
112+
return_type=int_rprimitive,
113+
c_function_name='PyList_Insert',
114+
error_kind=ERR_MAGIC)
115+
108116
# list * int
109117
binary_op(
110118
name='*',

mypyc/test-data/irbuild-lists.test

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,18 @@ L0:
233233
r2 = r1 >= 0 :: signed
234234
r3 = truncate r1: int32 to builtins.bool
235235
return r3
236+
237+
[case testListInsert]
238+
from typing import List
239+
def f(x: List[int], y: int) -> None:
240+
x.insert(0, y)
241+
[out]
242+
def f(x, y):
243+
x :: list
244+
y :: int
245+
r0 :: object
246+
r1 :: int
247+
L0:
248+
r0 = box(int, y)
249+
r1 = PyList_Insert(x, 0, r0)
250+
return 1

mypyc/test-data/run-lists.test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ pop(l, -2)
103103
assert l == [1, 3]
104104
assert count(l, 1) == 1
105105
assert count(l, 2) == 0
106+
l.insert(0, 0)
107+
assert l == [0, 1, 2]
108+
l.insert(3, 3)
109+
assert l == [0, 1, 2, 3]
110+
l.insert(2, 2)
111+
assert l == [0, 1, 2, 2, 3]
106112

107113
[case testListOfUserDefinedClass]
108114
class C:

0 commit comments

Comments
 (0)