Skip to content

Commit 4742464

Browse files
committed
Fixed a bug which occurred when edit was called with an integer index larger than the length of the history
Previously a edit was creating a file named 'None' in this case. Now an error is printed to the screen and no editor is opened. Also did some miscellaneous cleanup based on PyCharm warnings.
1 parent 7388e0d commit 4742464

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

cmd2.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,7 @@ def pseudo_raw_input(self, prompt):
10261026
else:
10271027
line = sm.input()
10281028
if self.echo:
1029-
sys.stdout.write('{}{}\n'.format(safe_prompt,line))
1029+
sys.stdout.write('{}{}\n'.format(safe_prompt, line))
10301030
except EOFError:
10311031
line = 'eof'
10321032
else:
@@ -1669,10 +1669,15 @@ def do_edit(self, arg, opts=None):
16691669
filename = None
16701670
if arg and arg[0]:
16711671
try:
1672-
history_item = self._last_matching(int(arg[0]))
1672+
history_idx = int(arg[0])
16731673
except ValueError:
16741674
filename = arg[0]
16751675
history_item = ''
1676+
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)
1680+
return
16761681
else:
16771682
try:
16781683
history_item = self.history[-1]
@@ -1817,7 +1822,7 @@ def do_load(self, file_path):
18171822
# command queue. Add an "end of script (eos)" command to cleanup the
18181823
# self._script_dir list when done. Specify file encoding in Python
18191824
# 3, but Python 2 doesn't allow that argument to open().
1820-
kwargs = {'encoding' : 'utf-8'} if six.PY3 else {}
1825+
kwargs = {'encoding': 'utf-8'} if six.PY3 else {}
18211826
with open(expanded_path, **kwargs) as target:
18221827
self.cmdqueue = target.read().splitlines() + ['eos'] + self.cmdqueue
18231828
except IOError as e:
@@ -2368,8 +2373,6 @@ def _test_transcript(self, fname, transcript):
23682373

23692374
def _transform_transcript_expected(self, s):
23702375
"""parse the string with slashed regexes into a valid regex"""
2371-
slash = '/'
2372-
backslash = '\\'
23732376
regex = ''
23742377
start = 0
23752378

@@ -2400,7 +2403,8 @@ def _transform_transcript_expected(self, s):
24002403
break
24012404
return regex
24022405

2403-
def _escaped_find(self, regex, s, start, in_regex):
2406+
@staticmethod
2407+
def _escaped_find(regex, s, start, in_regex):
24042408
"""
24052409
Find the next slash in {s} after {start} that is not preceded by a backslash.
24062410
@@ -2446,7 +2450,7 @@ def _escaped_find(self, regex, s, start, in_regex):
24462450
else:
24472451
# slash is not escaped, this is what we are looking for
24482452
break
2449-
return (regex, pos, start)
2453+
return regex, pos, start
24502454

24512455
def tearDown(self):
24522456
if self.cmdapp:

0 commit comments

Comments
 (0)