Thanks to visit codestin.com
Credit goes to github.com

Skip to content

DEP: Letting fromstring pretend to be frombuffer is a bad idea #9487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions doc/release/1.14.0-notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ Deprecations
empty, use ``array.size > 0``.
* Calling ``np.bincount`` with ``minlength=None`` is deprecated - instead,
``minlength=0`` should be used.
``np.fromstring`` should always be passed a ``sep`` argument
------------------------------------------------------------
Without this argument, this falls back on a broken version of `np.frombuffer`
that silently accepts and then encode unicode strings. If reading binary data
is desired, ``frombuffer`` should be used directly.


Future Changes
==============
Expand Down
23 changes: 13 additions & 10 deletions numpy/add_newdocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,7 @@ def luf(lamdaexpr, *args, **kwargs):
"""
fromstring(string, dtype=float, count=-1, sep='')

A new 1-D array initialized from raw binary or text data in a string.
A new 1-D array initialized from text data in a string.

Parameters
----------
Expand All @@ -975,11 +975,13 @@ def luf(lamdaexpr, *args, **kwargs):
negative (the default), the count will be determined from the
length of the data.
sep : str, optional
If not provided or, equivalently, the empty string, the data will
be interpreted as binary data; otherwise, as ASCII text with
decimal numbers. Also in this latter case, this argument is
interpreted as the string separating numbers in the data; extra
whitespace between elements is also ignored.
The string separating numbers in the data; extra whitespace between
elements is also ignored.

.. deprecated:: 1.14
If this argument is not provided, `fromstring` falls back on the
behaviour of `frombuffer` after encoding unicode string inputs as
either utf-8 (python 3), or the default encoding (python 2).

Returns
-------
Expand All @@ -998,14 +1000,10 @@ def luf(lamdaexpr, *args, **kwargs):

Examples
--------
>>> np.fromstring('\\x01\\x02', dtype=np.uint8)
array([1, 2], dtype=uint8)
>>> np.fromstring('1 2', dtype=int, sep=' ')
array([1, 2])
>>> np.fromstring('1, 2', dtype=int, sep=',')
array([1, 2])
>>> np.fromstring('\\x01\\x02\\x03\\x04\\x05', dtype=np.uint8, count=3)
array([1, 2, 3], dtype=uint8)

""")

Expand Down Expand Up @@ -1154,6 +1152,11 @@ def luf(lamdaexpr, *args, **kwargs):
array(['w', 'o', 'r', 'l', 'd'],
dtype='|S1')

>>> np.frombuffer(b'\\x01\\x02', dtype=np.uint8)
array([1, 2], dtype=uint8)
>>> np.frombuffer(b'\\x01\\x02\\x03\\x04\\x05', dtype=np.uint8, count=3)
array([1, 2, 3], dtype=uint8)

""")

add_newdoc('numpy.core.multiarray', 'concatenate',
Expand Down
11 changes: 11 additions & 0 deletions numpy/core/src/multiarray/multiarraymodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2098,6 +2098,17 @@ array_fromstring(PyObject *NPY_UNUSED(ignored), PyObject *args, PyObject *keywds
Py_XDECREF(descr);
return NULL;
}

/* binary mode, condition copied from PyArray_FromString */
if (sep == NULL || strlen(sep) == 0) {
/* Numpy 1.14, 2017-10-19 */
if (DEPRECATE(
"The binary mode of fromstring is deprecated, as it behaves "
"surprisingly on unicode inputs. Use frombuffer instead") < 0) {
Py_DECREF(descr);
return NULL;
}
}
return PyArray_FromString(data, (npy_intp)s, descr, (npy_intp)nin, sep);
}

Expand Down
9 changes: 6 additions & 3 deletions numpy/core/tests/test_multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -4183,11 +4183,11 @@ def test_roundtrip_filename(self):

def test_roundtrip_binary_str(self):
s = self.x.tobytes()
y = np.fromstring(s, dtype=self.dtype)
y = np.frombuffer(s, dtype=self.dtype)
assert_array_equal(y, self.x.flat)

s = self.x.tobytes('F')
y = np.fromstring(s, dtype=self.dtype)
y = np.frombuffer(s, dtype=self.dtype)
assert_array_equal(y, self.x.flatten('F'))

def test_roundtrip_str(self):
Expand Down Expand Up @@ -4302,7 +4302,10 @@ def test_file_position_after_tofile(self):
assert_equal(pos, 10, err_msg=err_msg)

def _check_from(self, s, value, **kw):
y = np.fromstring(s, **kw)
if 'sep' not in kw:
y = np.frombuffer(s, **kw)
else:
y = np.fromstring(s, **kw)
assert_array_equal(y, value)

f = open(self.filename, 'wb')
Expand Down
16 changes: 8 additions & 8 deletions numpy/core/tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ def test_tobytes_FORTRANORDER_discontiguous(self):
# Fix in r2836
# Create non-contiguous Fortran ordered array
x = np.array(np.random.rand(3, 3), order='F')[:, :2]
assert_array_almost_equal(x.ravel(), np.fromstring(x.tobytes()))
assert_array_almost_equal(x.ravel(), np.frombuffer(x.tobytes()))

def test_flat_assignment(self):
# Correct behaviour of ticket #194
Expand Down Expand Up @@ -833,14 +833,14 @@ def test_searchsorted_variable_length(self):

def test_string_argsort_with_zeros(self):
# Check argsort for strings containing zeros.
x = np.fromstring("\x00\x02\x00\x01", dtype="|S2")
x = np.frombuffer(b"\x00\x02\x00\x01", dtype="|S2")
assert_array_equal(x.argsort(kind='m'), np.array([1, 0]))
assert_array_equal(x.argsort(kind='q'), np.array([1, 0]))

def test_string_sort_with_zeros(self):
# Check sort for strings containing zeros.
x = np.fromstring("\x00\x02\x00\x01", dtype="|S2")
y = np.fromstring("\x00\x01\x00\x02", dtype="|S2")
x = np.frombuffer(b"\x00\x02\x00\x01", dtype="|S2")
y = np.frombuffer(b"\x00\x01\x00\x02", dtype="|S2")
assert_array_equal(np.sort(x, kind="q"), y)

def test_copy_detection_zero_dim(self):
Expand Down Expand Up @@ -1430,10 +1430,10 @@ def test_byteswap_complex_scalar(self):
y = x.byteswap()
if x.dtype.byteorder == z.dtype.byteorder:
# little-endian machine
assert_equal(x, np.fromstring(y.tobytes(), dtype=dtype.newbyteorder()))
assert_equal(x, np.frombuffer(y.tobytes(), dtype=dtype.newbyteorder()))
else:
# big-endian machine
assert_equal(x, np.fromstring(y.tobytes(), dtype=dtype))
assert_equal(x, np.frombuffer(y.tobytes(), dtype=dtype))
# double check real and imaginary parts:
assert_equal(x.real, y.real.byteswap())
assert_equal(x.imag, y.imag.byteswap())
Expand Down Expand Up @@ -1783,8 +1783,8 @@ def test_ticket_1756(self):
assert_equal(a1, a2)

def test_fields_strides(self):
"Ticket #1760"
r = np.fromstring('abcdefghijklmnop'*4*3, dtype='i4,(2,3)u2')
"gh-2355"
r = np.frombuffer(b'abcdefghijklmnop'*4*3, dtype='i4,(2,3)u2')
assert_equal(r[0:3:2]['f1'], r['f1'][0:3:2])
assert_equal(r[0:3:2]['f1'][0], r[0:3:2][0]['f1'])
assert_equal(r[0:3:2]['f1'][0][()], r[0:3:2][0]['f1'][()])
Expand Down