From c11caeb7946929cbf167e6c4d6da2f869578bd4d Mon Sep 17 00:00:00 2001 From: serge-sans-paille Date: Sun, 11 Aug 2024 10:28:57 +0200 Subject: [PATCH] array module support: reverse() --- pythran/pythonic/__dispatch__/reverse.hpp | 24 +++++++++++++++++++ .../pythonic/include/__dispatch__/reverse.hpp | 18 ++++++++++++++ pythran/tables.py | 2 ++ pythran/tests/test_array.py | 4 ++++ 4 files changed, 48 insertions(+) create mode 100644 pythran/pythonic/__dispatch__/reverse.hpp create mode 100644 pythran/pythonic/include/__dispatch__/reverse.hpp diff --git a/pythran/pythonic/__dispatch__/reverse.hpp b/pythran/pythonic/__dispatch__/reverse.hpp new file mode 100644 index 000000000..055d33fb6 --- /dev/null +++ b/pythran/pythonic/__dispatch__/reverse.hpp @@ -0,0 +1,24 @@ +#ifndef PYTHONIC_DISPATCH_REVERSE_HPP +#define PYTHONIC_DISPATCH_REVERSE_HPP + +#include "pythonic/include/__dispatch__/reverse.hpp" + +#include "pythonic/utils/functor.hpp" + +#include + +PYTHONIC_NS_BEGIN + +namespace __dispatch__ +{ + + template + types::none_type reverse(Any &&any) + { + std::reverse(any.begin(), any.end()); + return {}; + } +} // namespace __dispatch__ +PYTHONIC_NS_END + +#endif diff --git a/pythran/pythonic/include/__dispatch__/reverse.hpp b/pythran/pythonic/include/__dispatch__/reverse.hpp new file mode 100644 index 000000000..0734d2131 --- /dev/null +++ b/pythran/pythonic/include/__dispatch__/reverse.hpp @@ -0,0 +1,18 @@ +#ifndef PYTHONIC_INCLUDE_DISPATCH_REVERSE_HPP +#define PYTHONIC_INCLUDE_DISPATCH_REVERSE_HPP + +#include "pythonic/include/utils/functor.hpp" + +PYTHONIC_NS_BEGIN + +namespace __dispatch__ +{ + + template + types::none_type reverse(Any &&any); + + DEFINE_FUNCTOR(pythonic::__dispatch__, reverse); +} // namespace __dispatch__ +PYTHONIC_NS_END + +#endif diff --git a/pythran/tables.py b/pythran/tables.py index 8c375ec6c..86e65d357 100644 --- a/pythran/tables.py +++ b/pythran/tables.py @@ -192,6 +192,7 @@ def update_effects(self, node): "insert": MethodIntr(), "pop": MethodIntr(), "remove": MethodIntr(), + "reverse": MethodIntr(), }, "list": { "append": MethodIntr(signature=Fun[[List[T0], T0], None]), @@ -4530,6 +4531,7 @@ def expand_numpy_2_args(args, defaults=None, force=False): "insert": MethodIntr(signature=Fun[[List[T0], int, T0], None]), "pop": MethodIntr(), "remove": MethodIntr(), + "reverse": MethodIntr(), "sort": MethodIntr(), "tolist": ConstMethodIntr(), "update": MethodIntr(update_effects), diff --git a/pythran/tests/test_array.py b/pythran/tests/test_array.py index 5d48197a0..a63e4c234 100644 --- a/pythran/tests/test_array.py +++ b/pythran/tests/test_array.py @@ -83,3 +83,7 @@ def test_array_pop(self): def test_array_remove(self): self.run_test("def array_remove_(f): import array; x = array.array('I',[f,2,3]); x.remove(2); return x.tolist()", 3, array_remove_=[int]) + + def test_array_reverse(self): + self.run_test("def array_reverse_(f): import array; x = array.array('I',[f,2,3]); x.reverse(); return x.tolist()", + 3, array_reverse_=[int])