Skip to content

Commit

Permalink
Fixed the intermittent test in SciTools#3035.
Browse files Browse the repository at this point in the history
The problem was that iris.util.format_array was stateful (via np.set_printoptions).
  • Loading branch information
pelson committed May 31, 2018
1 parent 2b4f1b3 commit 6cceca8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/iris/tests/unit/coords/test_CellMeasure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'})")
Expand Down
31 changes: 25 additions & 6 deletions lib/iris/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import abc
import collections
from contextlib import contextmanager
import copy
import functools
import inspect
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6cceca8

Please sign in to comment.