Skip to content

Commit

Permalink
array module support: fromfile()
Browse files Browse the repository at this point in the history
  • Loading branch information
serge-sans-paille committed Aug 8, 2024
1 parent 0215a43 commit d4cb705
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pythran/pythonic/array/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace array
types::array<typename details::typecodes<c>::type>
array(std::integral_constant<char, c>)
{
return {};
return types::empty_list{};
}

template <char c, class E>
Expand Down
27 changes: 27 additions & 0 deletions pythran/pythonic/array/array/fromfile.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef PYTHONIC_ARRAY_ARRAY_FROMFILE_HPP
#define PYTHONIC_ARRAY_ARRAY_FROMFILE_HPP

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

PYTHONIC_NS_BEGIN

namespace array
{

namespace array
{

template <class T>
types::none_type fromfile(types::array<T> &seq, types::file &f, long n)
{
long p = seq.size();
seq.resize(p + n);
f.read_as(n, &*seq.begin() + p);
return {};
}

} // namespace array
} // namespace array
PYTHONIC_NS_END
#endif
41 changes: 41 additions & 0 deletions pythran/pythonic/include/array/array/fromfile.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#ifndef PYTHONIC_INCLUDE_ARRAY_ARRAY_FROMFILE_HPP
#define PYTHONIC_INCLUDE_ARRAY_ARRAY_FROMFILE_HPP

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

PYTHONIC_NS_BEGIN

namespace array
{

namespace array
{

template <class T>
types::none_type fromfile(types::array<T> &seq, types::file &f, long n);

template <class T>
types::none_type fromfile(types::array<T> &seq, types::file &&f, long n)
{
return fromfile(seq, f, n);
}

template <class T>
types::none_type fromfile(types::array<T> &&seq, types::file &&f, long n)
{
return fromfile(seq, f, n);
}

template <class T>
types::none_type fromfile(types::array<T> &&seq, types::file &f, long n)
{
return fromfile(seq, f, n);
}

DEFINE_FUNCTOR(pythonic::array::array, fromfile);
} // namespace array
} // namespace array
PYTHONIC_NS_END
#endif
3 changes: 3 additions & 0 deletions pythran/pythonic/include/types/file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ namespace types

types::str read(long size = -1);

template <class T>
void read_as(long n, T *buffer);

types::str readline(long size = std::numeric_limits<long>::max());

types::list<types::str> readlines(long sizehint = -1);
Expand Down
8 changes: 8 additions & 0 deletions pythran/pythonic/types/file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ namespace types
return res;
}

template <class T>
inline void file::read_as(long n, T *buffer)
{
if (fread(buffer, sizeof(T), n, **data) < size_t(n)) {
throw EOFError("read() didn't return enough bytes");
}
}

inline types::str file::readline(long size)
{
if (!is_open)
Expand Down
1 change: 1 addition & 0 deletions pythran/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ def update_effects(self, node):
"byteswap": MethodIntr(),
"count": ConstMethodIntr(),
"extend": MethodIntr(),
"fromfile": MethodIntr(),
},
"list": {
"append": MethodIntr(signature=Fun[[List[T0], T0], None]),
Expand Down
8 changes: 8 additions & 0 deletions pythran/tests/test_array.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pythran.tests import TestEnv

import unittest
from tempfile import mkstemp
import sys

class TestArray(TestEnv):
Expand Down Expand Up @@ -45,3 +46,10 @@ def test_array_count(self):
def test_array_extend(self):
self.run_test("def array_extend_(n): import array; x = array.array('h', [n]); x.extend((1, 1)); return x.tolist()",
2, array_extend_=[int])

def test_array_fromfile(self):
filename = mkstemp()[1]
with open(filename, "w") as fd:
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])

0 comments on commit d4cb705

Please sign in to comment.