From 99e073b08e638909b4417381149c2c1f8702b410 Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Sat, 9 Jan 2021 19:48:11 +0000 Subject: [PATCH] [mypyc] Add primitives for list.sort() and list.reverse() Adding these since they are trivial to implement. --- mypyc/doc/list_operations.rst | 5 ++++- mypyc/primitives/list_ops.py | 16 ++++++++++++++++ mypyc/test-data/fixtures/ir.py | 1 + mypyc/test-data/run-lists.test | 20 ++++++++++++++++++++ 4 files changed, 41 insertions(+), 1 deletion(-) diff --git a/mypyc/doc/list_operations.rst b/mypyc/doc/list_operations.rst index 94c75773329d..d9e1ed91f132 100644 --- a/mypyc/doc/list_operations.rst +++ b/mypyc/doc/list_operations.rst @@ -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 --------- diff --git a/mypyc/primitives/list_ops.py b/mypyc/primitives/list_ops.py index fad7a4aacef2..fdd2a9d41cdc 100644 --- a/mypyc/primitives/list_ops.py +++ b/mypyc/primitives/list_ops.py @@ -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='*', diff --git a/mypyc/test-data/fixtures/ir.py b/mypyc/test-data/fixtures/ir.py index da8e6986ce05..66361c173c9b 100644 --- a/mypyc/test-data/fixtures/ir.py +++ b/mypyc/test-data/fixtures/ir.py @@ -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 diff --git a/mypyc/test-data/run-lists.test b/mypyc/test-data/run-lists.test index a5bc4d731ddc..b7d243200972 100644 --- a/mypyc/test-data/run-lists.test +++ b/mypyc/test-data/run-lists.test @@ -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