Author: Brian Kearns <bdkea...@gmail.com> Branch: Changeset: r70951:a6ca15aed189 Date: 2014-04-24 19:33 -0400 http://bitbucket.org/pypy/pypy/changeset/a6ca15aed189/
Log: array.fromstring accepts buffers diff --git a/lib-python/2.7/test/test_array.py b/lib-python/2.7/test/test_array.py --- a/lib-python/2.7/test/test_array.py +++ b/lib-python/2.7/test/test_array.py @@ -298,6 +298,7 @@ b = array.array(self.badtypecode()) with self.assertRaises(TypeError): a + b + with self.assertRaises(TypeError): a + 'bad' @@ -320,6 +321,7 @@ b = array.array(self.badtypecode()) with self.assertRaises(TypeError): a += b + with self.assertRaises(TypeError): a += 'bad' diff --git a/pypy/module/array/interp_array.py b/pypy/module/array/interp_array.py --- a/pypy/module/array/interp_array.py +++ b/pypy/module/array/interp_array.py @@ -42,7 +42,7 @@ if len(__args__.arguments_w) > 0: w_initializer = __args__.arguments_w[0] if space.type(w_initializer) is space.w_str: - a.descr_fromstring(space, space.str_w(w_initializer)) + a.descr_fromstring(space, w_initializer) elif space.type(w_initializer) is space.w_list: a.descr_fromlist(space, w_initializer) else: @@ -232,13 +232,13 @@ self._charbuf_stop() return self.space.wrap(s) - @unwrap_spec(s=str) - def descr_fromstring(self, space, s): + def descr_fromstring(self, space, w_s): """ fromstring(string) Appends items from the string, interpreting it as an array of machine values,as if it had been read from a file using the fromfile() method). """ + s = space.getarg_w('s#', w_s) if len(s) % self.itemsize != 0: msg = 'string length not a multiple of item size' raise OperationError(self.space.w_ValueError, self.space.wrap(msg)) @@ -270,10 +270,10 @@ elems = max(0, len(item) - (len(item) % self.itemsize)) if n != 0: item = item[0:elems] - self.descr_fromstring(space, item) + self.descr_fromstring(space, space.wrap(item)) msg = "not enough items in file" raise OperationError(space.w_EOFError, space.wrap(msg)) - self.descr_fromstring(space, item) + self.descr_fromstring(space, w_item) @unwrap_spec(w_f=W_File) def descr_tofile(self, space, w_f): diff --git a/pypy/module/array/test/test_array.py b/pypy/module/array/test/test_array.py --- a/pypy/module/array/test/test_array.py +++ b/pypy/module/array/test/test_array.py @@ -155,6 +155,11 @@ a.fromstring('Hi!') assert a[0] == 'H' and a[1] == 'i' and a[2] == '!' and len(a) == 3 a = self.array('c') + a.fromstring(buffer('xyz')) + exc = raises(TypeError, a.fromstring, memoryview('xyz')) + assert str(exc.value) == "must be string or read-only buffer, not memoryview" + assert a[0] == 'x' and a[1] == 'y' and a[2] == 'z' and len(a) == 3 + a = self.array('c') a.fromstring('') assert not len(a) _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit