Skip to content

Commit

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

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

PYTHONIC_NS_BEGIN

namespace array
{

namespace array
{

template <class T>
types::none_type frombytes(types::array<T> &seq, types::str const &s)
{
long size = seq.size();
seq.resize(size + s.size() / sizeof(T));
memcpy(&*seq.begin() + size, s.c_str(), s.size());
return {};
}

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

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

PYTHONIC_NS_BEGIN

namespace array
{

namespace array
{

template <class T>
types::none_type frombytes(types::array<T> &seq, types::str const &);

DEFINE_FUNCTOR(pythonic::array::array, frombytes);
} // 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 @@ -188,6 +188,7 @@ def update_effects(self, node):
"extend": MethodIntr(),
"fromfile": MethodIntr(),
"fromlist": MethodIntr(),
"frombytes": MethodIntr(),
},
"list": {
"append": MethodIntr(signature=Fun[[List[T0], T0], None]),
Expand Down
10 changes: 10 additions & 0 deletions pythran/tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import unittest
from tempfile import mkstemp
import os
import sys

class TestArray(TestEnv):
Expand Down Expand Up @@ -53,7 +54,16 @@ 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])
os.remove(filename)

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])

def test_array_frombytes(self):
filename = mkstemp()[1]
with open(filename, "w") as fd:
fd.write("12345678"*10)
self.run_test("def array_frombytes_(s): import array; x = array.array('i'); x.frombytes(open(s, 'rb').read()); return x.tolist()",
filename, array_frombytes_=[str])
os.remove(filename)

0 comments on commit 031a5ba

Please sign in to comment.