Skip to content

Commit 0e7d55a

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 3674eb5 commit 0e7d55a

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:: __str__(self)
198199

exec_helpers/exec_result.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ def __getitem__(self, item: typing.Union[int, slice, typing.Iterable[typing.Unio
7474
:type item: typing.Union[int, slice, typing.Iterable[typing.Union[int, slice, ellipsis]]]
7575
:returns: Joined selected lines
7676
:rtype: str
77+
:raises KeyError: Unexpected key
7778
"""
7879
if isinstance(item, int):
7980
return _get_str_from_bin(_get_bytearray_from_array([self._data[item]]))
@@ -85,8 +86,10 @@ def __getitem__(self, item: typing.Union[int, slice, typing.Iterable[typing.Unio
8586
buf.append(self._data[rule])
8687
elif isinstance(rule, slice):
8788
buf.extend(self._data[rule])
88-
else:
89+
elif rule is Ellipsis:
8990
buf.append(b"...\n")
91+
else:
92+
raise KeyError("Unexpected key: {rule!r} (from {item!r})".format(rule=rule, item=item))
9093
return _get_str_from_bin(_get_bytearray_from_array(buf))
9194

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

test/test_exec_result.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,3 +286,5 @@ def test_indexed_lines_access(self):
286286
self.assertEqual(result.stdout_lines[0, 2], 'line0\nline2')
287287
self.assertEqual(result.stdout_lines[0, ..., 2], 'line0\n...\nline2')
288288
self.assertEqual(result.stdout_lines[:1, ..., 2], 'line0\n...\nline2')
289+
with self.assertRaises(KeyError):
290+
_ = result.stdout_lines[1, 'aaa'] # noqa

0 commit comments

Comments
 (0)