diff --git a/lib/iris/tests/unit/coords/test_CellMeasure.py b/lib/iris/tests/unit/coords/test_CellMeasure.py index b27e58da15..109357aabc 100644 --- a/lib/iris/tests/unit/coords/test_CellMeasure.py +++ b/lib/iris/tests/unit/coords/test_CellMeasure.py @@ -120,14 +120,14 @@ def test_repr_other_metadata(self): self.assertEqual(self.measure._repr_other_metadata(), expected) def test__str__(self): - expected = ("CellMeasure(array([ 10., 12., 16., 9.]), " + expected = ("CellMeasure(array([10., 12., 16., 9.]), " "measure=area, standard_name='cell_area', " "units=Unit('m^2'), long_name='measured_area', " "var_name='area', attributes={'notes': '1m accuracy'})") self.assertEqual(self.measure.__str__(), expected) def test__repr__(self): - expected = ("CellMeasure(array([ 10., 12., 16., 9.]), " + expected = ("CellMeasure(array([10., 12., 16., 9.]), " "measure=area, standard_name='cell_area', " "units=Unit('m^2'), long_name='measured_area', " "var_name='area', attributes={'notes': '1m accuracy'})") diff --git a/lib/iris/util.py b/lib/iris/util.py index f3277e52d7..4f4c42dc4b 100644 --- a/lib/iris/util.py +++ b/lib/iris/util.py @@ -25,6 +25,7 @@ import abc import collections +from contextlib import contextmanager import copy import functools import inspect @@ -948,16 +949,34 @@ def format_array(arr): summary_insert = '...' options = np.get_printoptions() options['legacy'] = legacy - np.set_printoptions(**options) - result = formatArray(arr, ffunc, max_line_len, - next_line_prefix='\t\t', separator=', ', - edge_items=edge_items, - summary_insert=summary_insert, - legacy=legacy) + with _printopts_context(**options): + result = formatArray( + arr, ffunc, max_line_len, + next_line_prefix='\t\t', separator=', ', + edge_items=edge_items, + summary_insert=summary_insert, + legacy=legacy) 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