Skip to content

Commit 371f7a1

Browse files
committed
Handle incorrect keys
Signed-off-by: Aleksei Stepanov <penguinolog@gmail.com> (cherry picked from commit 6aa67f8) Signed-off-by: Aleksei Stepanov <penguinolog@gmail.com>
1 parent a9a5aac commit 371f7a1

File tree

3 files changed

+7
-1
lines changed

3 files changed

+7
-1
lines changed

doc/source/ExecResult.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ API: ExecResult
193193
:type item: typing.Union[int, slice, typing.Iterable[typing.Union[int, slice, ellipsis]]]
194194
:returns: Joined selected lines
195195
:rtype: str
196+
:raises KeyError: Unexpected key
196197

197198
.. py:method:: __unicode__(self)
198199

exec_helpers/exec_result.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ def __getitem__(
8282
:type item: typing.Union[int, slice, typing.Iterable[typing.Union[int, slice, ellipsis]]]
8383
:returns: Joined selected lines
8484
:rtype: str
85+
:raises KeyError: Unexpected key
8586
"""
8687
if isinstance(item, six.integer_types):
8788
return _get_str_from_bin(_get_bytearray_from_array([self._data[item]]))
@@ -93,8 +94,10 @@ def __getitem__(
9394
buf.append(self._data[rule])
9495
elif isinstance(rule, slice):
9596
buf.extend(self._data[rule])
96-
else:
97+
elif rule is Ellipsis:
9798
buf.append(b"...\n")
99+
else:
100+
raise KeyError("Unexpected key: {rule!r} (from {item!r})".format(rule=rule, item=item))
98101
return _get_str_from_bin(_get_bytearray_from_array(buf))
99102

100103
def __len__(self): # type: () -> int # pragma: no cover

test/test_exec_result.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,3 +297,5 @@ def test_indexed_lines_access(self):
297297
self.assertEqual(result.stdout_lines[0, 2], 'line0\nline2')
298298
self.assertEqual(result.stdout_lines[0, ..., 2], 'line0\n...\nline2')
299299
self.assertEqual(result.stdout_lines[:1, ..., 2], 'line0\n...\nline2')
300+
with self.assertRaises(KeyError):
301+
_ = result.stdout_lines[1, 'aaa'] # noqa

0 commit comments

Comments
 (0)