Skip to content

Commit

Permalink
array module support: fromlist()
Browse files Browse the repository at this point in the history
  • Loading branch information
serge-sans-paille committed Aug 8, 2024
1 parent d4cb705 commit 0657217
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 2 deletions.
25 changes: 25 additions & 0 deletions pythran/pythonic/array/array/fromlist.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef PYTHONIC_ARRAY_ARRAY_FROMLIST_HPP
#define PYTHONIC_ARRAY_ARRAY_FROMLIST_HPP

#include "pythonic/include/types/array.hpp"
#include "pythonic/include/utils/functor.hpp"

PYTHONIC_NS_BEGIN

namespace array
{

namespace array
{

template <class T, class E>
types::none_type fromlist(types::array<T> &seq, E &&elts)
{
seq += std::forward<E>(elts);
return {};
}

} // namespace array
} // namespace array
PYTHONIC_NS_END
#endif
8 changes: 6 additions & 2 deletions pythran/pythonic/include/__dispatch__/tolist.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ namespace __dispatch__
}

template <class T>
types::list<long> tolist(types::array<T> &&a)
types::list<
typename std::conditional<std::is_integral<T>::value, long, double>::type>
tolist(types::array<T> &&a)
{
return a;
}

template <class T>
types::list<long> tolist(types::array<T> const &a)
types::list<
typename std::conditional<std::is_integral<T>::value, long, double>::type>
tolist(types::array<T> const &a)
{
return a;
}
Expand Down
27 changes: 27 additions & 0 deletions pythran/pythonic/include/array/array/fromlist.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef PYTHONIC_INCLUDE_ARRAY_ARRAY_FROMLIST_HPP
#define PYTHONIC_INCLUDE_ARRAY_ARRAY_FROMLIST_HPP

#include "pythonic/include/types/array.hpp"
#include "pythonic/include/utils/functor.hpp"

PYTHONIC_NS_BEGIN

namespace array
{

namespace array
{

template <class T, class E>
types::none_type fromlist(types::array<T> &seq, E &&elts);
template <class T, class E>
types::none_type fromlist(types::array<T> &&seq, E &&elts)
{
return fromlist(seq, elts);
}

DEFINE_FUNCTOR(pythonic::array::array, fromlist);
} // namespace array
} // namespace array
PYTHONIC_NS_END
#endif
1 change: 1 addition & 0 deletions pythran/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ def update_effects(self, node):
"count": ConstMethodIntr(),
"extend": MethodIntr(),
"fromfile": MethodIntr(),
"fromlist": MethodIntr(),
},
"list": {
"append": MethodIntr(signature=Fun[[List[T0], T0], None]),
Expand Down
4 changes: 4 additions & 0 deletions pythran/tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,7 @@ def test_array_fromfile(self):
fd.write("12345678"*100)
self.run_test("def array_fromfile_(s): import array; x = array.array('h'); x.fromfile(open(s, 'rb'), 8); return x.tolist()",
filename, array_fromfile_=[str])

def test_array_fromlist(self):
self.run_test("def array_fromlist_(f): import array; x = array.array('f'); x.fromlist([f]); return x.tolist()",
3., array_fromlist_=[float])

0 comments on commit 0657217

Please sign in to comment.