Skip to content

Commit 2aa1d9b

Browse files
committed
Fixed a bug when edit is passed a large negative integer in quotes
Fixed a weird corner case. Also added some comments to do_edit to better explain what the code is doing.
1 parent 4742464 commit 2aa1d9b

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

cmd2.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,15 +1669,33 @@ def do_edit(self, arg, opts=None):
16691669
filename = None
16701670
if arg and arg[0]:
16711671
try:
1672+
# Try to convert argument to an integer
16721673
history_idx = int(arg[0])
16731674
except ValueError:
1675+
# Argument passed is not convertible to an integer, so treat it as a file path
16741676
filename = arg[0]
16751677
history_item = ''
16761678
else:
1677-
history_item = self._last_matching(history_idx)
1678-
if history_item is None:
1679-
self.perror('index {!r} does not exist within the history'.format(history_idx), traceback_war=False)
1679+
# Argument passed IS convertible to an integer, so treat it as a history index
1680+
1681+
# Save off original index for pringing
1682+
orig_indx = history_idx
1683+
1684+
# Convert negative index into equivalent positive one
1685+
if history_idx < 0:
1686+
history_idx += len(self.history) + 1
1687+
1688+
# Make sure the index is actually within the history
1689+
if 1 <= history_idx <= len(self.history):
1690+
history_item = self._last_matching(history_idx)
1691+
if history_item is None:
1692+
self.perror('index {!r} does not exist within the history'.format(orig_indx),
1693+
traceback_war=False)
1694+
return
1695+
else:
1696+
self.perror('index {!r} does not exist within the history'.format(orig_indx), traceback_war=False)
16801697
return
1698+
16811699
else:
16821700
try:
16831701
history_item = self.history[-1]

0 commit comments

Comments
 (0)