Skip to content

Commit

Permalink
Add support for clear for lists
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanlaroche committed Nov 28, 2023
1 parent 2e02ee9 commit 1ddf3d5
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions pythran/pythonic/include/types/list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ namespace types
iterator erase(size_t n);

T pop(long x = -1);
void clear();

// TODO: have to raise a valueError
none_type remove(T const &x);
Expand Down
8 changes: 8 additions & 0 deletions pythran/pythonic/types/list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,14 @@ namespace types
erase(x);
return res;
}
template <class T>
void list<T>::clear()
{
long sz = size();
for (long ii=0;ii<sz;ii++)
erase(0);
}

// TODO: have to raise a valueError
template <class T>
none_type list<T>::remove(T const &x)
Expand Down
1 change: 1 addition & 0 deletions pythran/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ def update_effects(self, node):
"count": ConstMethodIntr(signature=Fun[[List[T0], T0], int]),
"remove": MethodIntr(signature=Fun[[List[T0], T0], None]),
"insert": MethodIntr(signature=Fun[[List[T0], int, T0], None]),
"clear": MethodIntr(signature=Fun[[List[T0]], None]),
},
"slice": {
"start": AttributeIntr(signature=Fun[[T0], int]),
Expand Down
3 changes: 3 additions & 0 deletions pythran/tests/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ def test_index_(self):
def test_index_tuple(self):
self.run_test("def index_tuple(a):\n b=[1,2,3,8,7,4]\n return tuple(b).index(a)", 1, index_tuple=[int])

def test_clear_(self):
self.run_test("def clear_():\n b=[1,3,4,5,6,7]\n b.clear()\n return b", clear_=[])

def test_pop_(self):
self.run_test("def pop_(a):\n b=[1,3,4,5,6,7]\n return b.pop(a)", 2, pop_=[int])

Expand Down

0 comments on commit 1ddf3d5

Please sign in to comment.