From b7598762531c727bb6939a2017c15fa6cac86183 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Sat, 27 Jun 2015 01:27:47 -0400 Subject: [PATCH] py3k: Correctly handle string input to slicers. In Python 3, str has the __iter__ method, so just checking for it causes some invalid iteration through the string characters. --- lib/iris/cube.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/iris/cube.py b/lib/iris/cube.py index 5a58509151..fbbcc17fed 100644 --- a/lib/iris/cube.py +++ b/lib/iris/cube.py @@ -2306,7 +2306,8 @@ def _as_list_of_coords(self, names_or_coords): Convert a name, coord, or list of names/coords to a list of coords. """ # If not iterable, convert to list of a single item - if not hasattr(names_or_coords, '__iter__'): + if (not hasattr(names_or_coords, '__iter__') or + isinstance(names_or_coords, str)): names_or_coords = [names_or_coords] coords = [] @@ -2352,7 +2353,8 @@ def slices_over(self, ref_to_slice): """ # Required to handle a mix between types. - if not hasattr(ref_to_slice, '__iter__'): + if (not hasattr(ref_to_slice, '__iter__') or + isinstance(ref_to_slice, str)): ref_to_slice = [ref_to_slice] slice_dims = set() @@ -2412,7 +2414,8 @@ def slices(self, ref_to_slice, ordered=True): raise TypeError("'ordered' argument to slices must be boolean.") # Required to handle a mix between types - if not hasattr(ref_to_slice, '__iter__'): + if (not hasattr(ref_to_slice, '__iter__') or + isinstance(ref_to_slice, str)): ref_to_slice = [ref_to_slice] dim_to_slice = []