Skip to content

Commit 3497675

Browse files
authored
[mypyc] Add primitives for list.sort() and list.reverse() (#9897)
Adding these since they are trivial to implement.
1 parent a4a5fd6 commit 3497675

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

mypyc/doc/list_operations.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,11 @@ Methods
5353

5454
* ``lst.append(item)``
5555
* ``lst.extend(x: Iterable)``
56-
* ``lst.pop()``
56+
* ``lst.insert(index, item)``
57+
* ``lst.pop(index=-1)``
5758
* ``lst.count(item)``
59+
* ``lst.reverse()``
60+
* ``lst.sort()``
5861

5962
Functions
6063
---------

mypyc/primitives/list_ops.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,22 @@
113113
c_function_name='CPyList_Insert',
114114
error_kind=ERR_NEG_INT)
115115

116+
# list.sort()
117+
method_op(
118+
name='sort',
119+
arg_types=[list_rprimitive],
120+
return_type=c_int_rprimitive,
121+
c_function_name='PyList_Sort',
122+
error_kind=ERR_NEG_INT)
123+
124+
# list.reverse()
125+
method_op(
126+
name='reverse',
127+
arg_types=[list_rprimitive],
128+
return_type=c_int_rprimitive,
129+
c_function_name='PyList_Reverse',
130+
error_kind=ERR_NEG_INT)
131+
116132
# list * int
117133
binary_op(
118134
name='*',

mypyc/test-data/fixtures/ir.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ def count(self, T) -> int: pass
144144
def extend(self, l: Iterable[T]) -> None: pass
145145
def insert(self, i: int, x: T) -> None: pass
146146
def sort(self) -> None: pass
147+
def reverse(self) -> None: pass
147148

148149
class dict(Mapping[K, V]):
149150
@overload

mypyc/test-data/run-lists.test

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,26 @@ def test_insert() -> None:
129129
else:
130130
assert False
131131

132+
def test_sort() -> None:
133+
l = [1, 4, 3, 6, -1]
134+
l.sort()
135+
assert l == [-1, 1, 3, 4, 6]
136+
l.sort()
137+
assert l == [-1, 1, 3, 4, 6]
138+
l = []
139+
l.sort()
140+
assert l == []
141+
142+
def test_reverse() -> None:
143+
l = [1, 4, 3, 6, -1]
144+
l.reverse()
145+
assert l == [-1, 6, 3, 4, 1]
146+
l.reverse()
147+
assert l == [1, 4, 3, 6, -1]
148+
l = []
149+
l.reverse()
150+
assert l == []
151+
132152
[case testListOfUserDefinedClass]
133153
class C:
134154
x: int

0 commit comments

Comments
 (0)