Skip to content

Commit

Permalink
[mypyc] Add primitives for list.sort() and list.reverse() (python#9897)
Browse files Browse the repository at this point in the history
Adding these since they are trivial to implement.
  • Loading branch information
JukkaL authored Jan 10, 2021
1 parent a4a5fd6 commit 3497675
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
5 changes: 4 additions & 1 deletion mypyc/doc/list_operations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ Methods

* ``lst.append(item)``
* ``lst.extend(x: Iterable)``
* ``lst.pop()``
* ``lst.insert(index, item)``
* ``lst.pop(index=-1)``
* ``lst.count(item)``
* ``lst.reverse()``
* ``lst.sort()``

Functions
---------
Expand Down
16 changes: 16 additions & 0 deletions mypyc/primitives/list_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,22 @@
c_function_name='CPyList_Insert',
error_kind=ERR_NEG_INT)

# list.sort()
method_op(
name='sort',
arg_types=[list_rprimitive],
return_type=c_int_rprimitive,
c_function_name='PyList_Sort',
error_kind=ERR_NEG_INT)

# list.reverse()
method_op(
name='reverse',
arg_types=[list_rprimitive],
return_type=c_int_rprimitive,
c_function_name='PyList_Reverse',
error_kind=ERR_NEG_INT)

# list * int
binary_op(
name='*',
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 @@ -144,6 +144,7 @@ def count(self, T) -> int: pass
def extend(self, l: Iterable[T]) -> None: pass
def insert(self, i: int, x: T) -> None: pass
def sort(self) -> None: pass
def reverse(self) -> None: pass

class dict(Mapping[K, V]):
@overload
Expand Down
20 changes: 20 additions & 0 deletions mypyc/test-data/run-lists.test
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,26 @@ def test_insert() -> None:
else:
assert False

def test_sort() -> None:
l = [1, 4, 3, 6, -1]
l.sort()
assert l == [-1, 1, 3, 4, 6]
l.sort()
assert l == [-1, 1, 3, 4, 6]
l = []
l.sort()
assert l == []

def test_reverse() -> None:
l = [1, 4, 3, 6, -1]
l.reverse()
assert l == [-1, 6, 3, 4, 1]
l.reverse()
assert l == [1, 4, 3, 6, -1]
l = []
l.reverse()
assert l == []

[case testListOfUserDefinedClass]
class C:
x: int
Expand Down

0 comments on commit 3497675

Please sign in to comment.