diff --git a/lib/iris/util.py b/lib/iris/util.py index 9ab413a493..43ff6c9271 100644 --- a/lib/iris/util.py +++ b/lib/iris/util.py @@ -10,7 +10,6 @@ from abc import ABCMeta, abstractmethod from collections.abc import Hashable, Iterable -from contextlib import contextmanager import copy import functools import inspect @@ -1054,18 +1053,20 @@ def format_array(arr): """ - summary_insert = "" summary_threshold = 85 + summary_insert = "..." if arr.size > summary_threshold else "" edge_items = 3 ffunc = str - formatArray = np.core.arrayprint._formatArray max_line_len = 50 - legacy = "1.13" - if arr.size > summary_threshold: - summary_insert = "..." - options = np.get_printoptions() - options["legacy"] = legacy - with _printopts_context(**options): + + # Format the array with version 1.13 legacy behaviour + with np.printoptions(legacy="1.13"): + # Use this (private) routine for more control. + formatArray = np.core.arrayprint._formatArray + # N.B. the 'legacy' arg had different forms in different numpy versions + # -- fetch the required form from the internal options dict + legacy_key = np.core.arrayprint._format_options["legacy"] + result = formatArray( arr, ffunc, @@ -1074,29 +1075,12 @@ def format_array(arr): separator=", ", edge_items=edge_items, summary_insert=summary_insert, - legacy=legacy, + legacy=legacy_key, ) return result -@contextmanager -def _printopts_context(**kwargs): - """ - Update the numpy printoptions for the life of this context manager. - - Note: this function can be removed with numpy>=1.15 thanks to - https://github.com/numpy/numpy/pull/10406 - - """ - original_opts = np.get_printoptions() - np.set_printoptions(**kwargs) - try: - yield - finally: - np.set_printoptions(**original_opts) - - def new_axis(src_cube, scalar_coord=None): """ Create a new axis as the leading dimension of the cube, promoting a scalar