diff options
| -rw-r--r-- | doc/release/1.14.0-notes.rst | 12 | ||||
| -rw-r--r-- | numpy/core/arrayprint.py | 22 | ||||
| -rw-r--r-- | numpy/core/src/umath/ufunc_object.c | 69 | ||||
| -rw-r--r-- | numpy/core/tests/test_arrayprint.py | 11 | ||||
| -rw-r--r-- | numpy/ma/core.py | 4 |
5 files changed, 62 insertions, 56 deletions
diff --git a/doc/release/1.14.0-notes.rst b/doc/release/1.14.0-notes.rst index 793b20c6d..2aaf0246a 100644 --- a/doc/release/1.14.0-notes.rst +++ b/doc/release/1.14.0-notes.rst @@ -265,14 +265,14 @@ In summary, the major changes are: * Float arrays printed in scientific notation no longer use fixed-precision, and now instead show the shortest unique representation. * The ``str`` of floating-point scalars is no longer truncated in python2. - + * For other data types: * Non-finite complex scalars print like ``nanj`` instead of ``nan*j``. * ``NaT`` values in datetime arrays are now properly aligned. * Arrays and scalars of ``np.void`` datatype are now printed using hex notation. - + * For line-wrapping: * The "dtype" part of ndarray reprs will now be printed on the next line @@ -280,11 +280,11 @@ In summary, the major changes are: * The ``linewidth`` format option is now always respected. The `repr` or `str` of an array will never exceed this, unless a single element is too wide. - * All but the last line of array strings will contain the same number of - elements. * The last line of an array string will never have more elements than earlier lines. - + * An extra space is no longer inserted on the first line if the elements are + too wide. + * For summarization (the use of ``...`` to shorten long arrays): * A trailing comma is no longer inserted for ``str``. @@ -294,7 +294,7 @@ In summary, the major changes are: order to summarize any but the last axis, newlines are now appended to that line to match its leading newlines and a trailing space character is removed. - + * ``MaskedArray`` arrays now separate printed elements with commas, always print the dtype, and correctly wrap the elements of long arrays to multiple lines. If there is more than 1 dimension, the array attributes are now diff --git a/numpy/core/arrayprint.py b/numpy/core/arrayprint.py index eaec91259..238e1782f 100644 --- a/numpy/core/arrayprint.py +++ b/numpy/core/arrayprint.py @@ -622,8 +622,14 @@ def array2string(a, max_line_width=None, precision=None, return _array2string(a, options, separator, prefix) -def _extendLine(s, line, word, line_width, next_line_prefix): - if len(line) + len(word) > line_width: +def _extendLine(s, line, word, line_width, next_line_prefix, legacy): + needs_wrap = len(line) + len(word) > line_width + if legacy != '1.13': + s# don't wrap lines if it won't help + if len(line) <= len(next_line_prefix): + needs_wrap = False + + if needs_wrap: s += line.rstrip() + "\n" line = next_line_prefix line += word @@ -682,11 +688,13 @@ def _formatArray(a, format_function, line_width, next_line_prefix, line = hanging_indent for i in range(leading_items): word = recurser(index + (i,), next_hanging_indent, next_width) - s, line = _extendLine(s, line, word, elem_width, hanging_indent) + s, line = _extendLine( + s, line, word, elem_width, hanging_indent, legacy) line += separator if show_summary: - s, line = _extendLine(s, line, summary_insert, elem_width, hanging_indent) + s, line = _extendLine( + s, line, summary_insert, elem_width, hanging_indent, legacy) if legacy == '1.13': line += ", " else: @@ -694,14 +702,16 @@ def _formatArray(a, format_function, line_width, next_line_prefix, for i in range(trailing_items, 1, -1): word = recurser(index + (-i,), next_hanging_indent, next_width) - s, line = _extendLine(s, line, word, elem_width, hanging_indent) + s, line = _extendLine( + s, line, word, elem_width, hanging_indent, legacy) line += separator if legacy == '1.13': # width of the seperator is not considered on 1.13 elem_width = curr_width word = recurser(index + (-1,), next_hanging_indent, next_width) - s, line = _extendLine(s, line, word, elem_width, hanging_indent) + s, line = _extendLine( + s, line, word, elem_width, hanging_indent, legacy) s += line diff --git a/numpy/core/src/umath/ufunc_object.c b/numpy/core/src/umath/ufunc_object.c index 35c7724b1..257067023 100644 --- a/numpy/core/src/umath/ufunc_object.c +++ b/numpy/core/src/umath/ufunc_object.c @@ -1751,7 +1751,7 @@ make_arr_prep_args(npy_intp nin, PyObject *args, PyObject *kwds) /* * Validate the core dimensions of all the operands, and collect all of * the labelled core dimensions into 'core_dim_sizes'. - * + * * Returns 0 on success, and -1 on failure * * The behavior has been changed in NumPy 1.10.0, and the following @@ -3656,7 +3656,7 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args, int i, naxes=0, ndim; int axes[NPY_MAXDIMS]; PyObject *axes_in = NULL; - PyArrayObject *mp, *ret = NULL; + PyArrayObject *mp = NULL, *ret = NULL; PyObject *op, *res = NULL; PyObject *obj_ind, *context; PyArrayObject *indices = NULL; @@ -3707,24 +3707,22 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args, PyDict_SetItem(kwds, npy_um_str_out, out_obj); } } - + if (operation == UFUNC_REDUCEAT) { PyArray_Descr *indtype; indtype = PyArray_DescrFromType(NPY_INTP); if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|OO&O&:reduceat", reduceat_kwlist, - &op, - &obj_ind, - &axes_in, - PyArray_DescrConverter2, &otype, - PyArray_OutputConverter, &out)) { - Py_XDECREF(otype); - return NULL; + &op, + &obj_ind, + &axes_in, + PyArray_DescrConverter2, &otype, + PyArray_OutputConverter, &out)) { + goto fail; } indices = (PyArrayObject *)PyArray_FromAny(obj_ind, indtype, 1, 1, NPY_ARRAY_CARRAY, NULL); if (indices == NULL) { - Py_XDECREF(otype); - return NULL; + goto fail; } } else if (operation == UFUNC_ACCUMULATE) { @@ -3734,8 +3732,7 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args, &axes_in, PyArray_DescrConverter2, &otype, PyArray_OutputConverter, &out)) { - Py_XDECREF(otype); - return NULL; + goto fail; } } else { @@ -3746,8 +3743,7 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args, PyArray_DescrConverter2, &otype, PyArray_OutputConverter, &out, &keepdims)) { - Py_XDECREF(otype); - return NULL; + goto fail; } } /* Ensure input is an array */ @@ -3760,7 +3756,7 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args, mp = (PyArrayObject *)PyArray_FromAny(op, NULL, 0, 0, 0, context); Py_XDECREF(context); if (mp == NULL) { - return NULL; + goto fail; } ndim = PyArray_NDIM(mp); @@ -3771,9 +3767,7 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args, PyErr_Format(PyExc_TypeError, "cannot perform %s with flexible type", _reduce_type[operation]); - Py_XDECREF(otype); - Py_DECREF(mp); - return NULL; + goto fail; } /* Convert the 'axis' parameter into a list of axes */ @@ -3793,22 +3787,16 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args, if (naxes < 0 || naxes > NPY_MAXDIMS) { PyErr_SetString(PyExc_ValueError, "too many values for 'axis'"); - Py_XDECREF(otype); - Py_DECREF(mp); - return NULL; + goto fail; } for (i = 0; i < naxes; ++i) { PyObject *tmp = PyTuple_GET_ITEM(axes_in, i); int axis = PyArray_PyIntAsInt(tmp); if (error_converting(axis)) { - Py_XDECREF(otype); - Py_DECREF(mp); - return NULL; + goto fail; } if (check_and_adjust_axis(&axis, ndim) < 0) { - Py_XDECREF(otype); - Py_DECREF(mp); - return NULL; + goto fail; } axes[i] = (int)axis; } @@ -3818,16 +3806,14 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args, int axis = PyArray_PyIntAsInt(axes_in); /* TODO: PyNumber_Index would be good to use here */ if (error_converting(axis)) { - Py_XDECREF(otype); - Py_DECREF(mp); - return NULL; + goto fail; } /* Special case letting axis={0 or -1} slip through for scalars */ if (ndim == 0 && (axis == 0 || axis == -1)) { axis = 0; } else if (check_and_adjust_axis(&axis, ndim) < 0) { - return NULL; + goto fail; } axes[0] = (int)axis; naxes = 1; @@ -3847,9 +3833,7 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args, (naxes == 0 || (naxes == 1 && axes[0] == 0)))) { PyErr_Format(PyExc_TypeError, "cannot %s on a scalar", _reduce_type[operation]); - Py_XDECREF(otype); - Py_DECREF(mp); - return NULL; + goto fail; } } @@ -3895,9 +3879,7 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args, if (naxes != 1) { PyErr_SetString(PyExc_ValueError, "accumulate does not allow multiple axes"); - Py_XDECREF(otype); - Py_DECREF(mp); - return NULL; + goto fail; } ret = (PyArrayObject *)PyUFunc_Accumulate(ufunc, mp, out, axes[0], otype->type_num); @@ -3906,9 +3888,7 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args, if (naxes != 1) { PyErr_SetString(PyExc_ValueError, "reduceat does not allow multiple axes"); - Py_XDECREF(otype); - Py_DECREF(mp); - return NULL; + goto fail; } ret = (PyArrayObject *)PyUFunc_Reduceat(ufunc, mp, indices, out, axes[0], otype->type_num); @@ -3941,6 +3921,11 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args, } } return PyArray_Return(ret); + +fail: + Py_XDECREF(otype); + Py_XDECREF(mp); + return NULL; } /* diff --git a/numpy/core/tests/test_arrayprint.py b/numpy/core/tests/test_arrayprint.py index 950004508..d491d53aa 100644 --- a/numpy/core/tests/test_arrayprint.py +++ b/numpy/core/tests/test_arrayprint.py @@ -289,6 +289,17 @@ class TestArray2String(object): ' 11\n' ' 11]]]') + def test_wide_element(self): + a = np.array(['xxxxx']) + assert_equal( + np.array2string(a, max_line_width=5), + "['xxxxx']" + ) + assert_equal( + np.array2string(a, max_line_width=5, legacy='1.13'), + "[ 'xxxxx']" + ) + class TestPrintOptions(object): """Test getting and setting global print options.""" diff --git a/numpy/ma/core.py b/numpy/ma/core.py index 04c1c1a6a..dad675600 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -7931,9 +7931,9 @@ def load(F): _pickle_warn('load') if not hasattr(F, 'readline'): with open(F, 'r') as F: - pickle.load(F) + return pickle.load(F) else: - pickle.load(F) + return pickle.load(F) def loads(strg): |
