Skip to content

Commit cf2aa3b

Browse files
committed
Better detection of iterables. Fixes robotframework#3547.
1 parent fa78485 commit cf2aa3b

File tree

3 files changed

+14
-22
lines changed

3 files changed

+14
-22
lines changed

src/robot/utils/robottypes2.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16-
from collections import Mapping, MutableMapping, Sequence
16+
from collections import Iterable, Mapping, MutableMapping, Sequence
1717
from UserDict import UserDict
1818
from UserString import UserString
1919
from types import ClassType, NoneType
@@ -56,14 +56,7 @@ def is_list_like(item):
5656
if isinstance(item, (str, unicode, bytes, bytearray, UserString, String,
5757
file)):
5858
return False
59-
try:
60-
iter(item)
61-
except RERAISED_EXCEPTIONS:
62-
raise
63-
except:
64-
return False
65-
else:
66-
return True
59+
return isinstance(item, (Iterable, UserDict))
6760

6861

6962
def is_dict_like(item):

src/robot/utils/robottypes3.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16-
from collections.abc import Mapping, MutableMapping, Sequence
16+
from collections.abc import Iterable, Mapping, MutableMapping, Sequence
1717
from collections import UserString
1818
from io import IOBase
1919

@@ -53,14 +53,7 @@ def is_pathlike(item):
5353
def is_list_like(item):
5454
if isinstance(item, (str, bytes, bytearray, UserString, IOBase)):
5555
return False
56-
try:
57-
iter(item)
58-
except RERAISED_EXCEPTIONS:
59-
raise
60-
except:
61-
return False
62-
else:
63-
return True
56+
return isinstance(item, Iterable)
6457

6558

6659
def is_dict_like(item):

utest/utils/test_robottypes.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,19 @@ def test_files_are_not_list_like(self):
8484
assert_equal(is_list_like(f), False)
8585
assert_equal(is_list_like(f), False)
8686

87-
def test_object_raising_exception_are_not_list_like(self):
88-
class O(object):
87+
def test_iter_makes_object_iterable_regardless_implementation(self):
88+
class Example(object):
8989
def __iter__(self):
9090
1/0
91-
assert_equal(is_list_like(O()), False)
91+
assert_equal(is_list_like(Example()), True)
9292

93-
def test_other_iterables_are_list_like(self):
93+
def test_only_getitem_does_not_make_object_iterable(self):
94+
class Example(object):
95+
def __getitem__(self, item):
96+
return "I'm not iterable!"
97+
assert_equal(is_list_like(Example()), False)
98+
99+
def test_iterables_in_general_are_list_like(self):
94100
for thing in [[], (), set(), xrange(1), generator(), array('i'), UserList()]:
95101
assert_equal(is_list_like(thing), True, thing)
96102

0 commit comments

Comments
 (0)