summaryrefslogtreecommitdiff
path: root/cmd2/cmd2.py
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2019-06-15 10:13:12 -0400
committerTodd Leonhardt <todd.leonhardt@gmail.com>2019-06-15 10:13:12 -0400
commitea1716ad0c43ce0c2c354836dbc36e4ae419afb6 (patch)
tree901fcd355c5aa4912f12d6b09127ae0c3a1dc7d8 /cmd2/cmd2.py
parenta1be014e621bd1b9407cfb1eeefdd95ff67dd815 (diff)
downloadcmd2-git-ea1716ad0c43ce0c2c354836dbc36e4ae419afb6.tar.gz
Fix unit test failures I introduced in last commit
Diffstat (limited to 'cmd2/cmd2.py')
-rw-r--r--cmd2/cmd2.py80
1 files changed, 41 insertions, 39 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index 1d678180..46b098c5 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -3270,11 +3270,10 @@ class Cmd(cmd.Cmd):
finally:
# Restore command line arguments to original state
sys.argv = orig_args
-
- if args.__statement__.command == "pyscript":
- self.perror("pyscript has been renamed and will be removed in the next release, "
- "please use run_pyscript instead\n",
- traceback_war=False, err_color=Fore.LIGHTYELLOW_EX)
+ if args.__statement__.command == "pyscript":
+ self.perror("pyscript has been renamed and will be removed in the next release, "
+ "please use run_pyscript instead\n",
+ traceback_war=False, err_color=Fore.LIGHTYELLOW_EX)
return py_return
@@ -3655,49 +3654,52 @@ class Cmd(cmd.Cmd):
"""
expanded_path = os.path.abspath(os.path.expanduser(args.script_path))
- # Make sure the path exists and we can access it
- if not os.path.exists(expanded_path):
- self.perror("'{}' does not exist or cannot be accessed".format(expanded_path), traceback_war=False)
- return
+ # Wrap everything in a try/finally just to make sure the warning prints at end if `load` was called
+ try:
+ # Make sure the path exists and we can access it
+ if not os.path.exists(expanded_path):
+ self.perror("'{}' does not exist or cannot be accessed".format(expanded_path), traceback_war=False)
+ return
- # Make sure expanded_path points to a file
- if not os.path.isfile(expanded_path):
- self.perror("'{}' is not a file".format(expanded_path), traceback_war=False)
- return
+ # Make sure expanded_path points to a file
+ if not os.path.isfile(expanded_path):
+ self.perror("'{}' is not a file".format(expanded_path), traceback_war=False)
+ return
- # Make sure the file is not empty
- if os.path.getsize(expanded_path) == 0:
- self.perror("'{}' is empty".format(expanded_path), traceback_war=False)
- return
+ # Make sure the file is not empty
+ if os.path.getsize(expanded_path) == 0:
+ self.perror("'{}' is empty".format(expanded_path), traceback_war=False)
+ return
- # Make sure the file is ASCII or UTF-8 encoded text
- if not utils.is_text_file(expanded_path):
- self.perror("'{}' is not an ASCII or UTF-8 encoded text file".format(expanded_path), traceback_war=False)
- return
+ # Make sure the file is ASCII or UTF-8 encoded text
+ if not utils.is_text_file(expanded_path):
+ self.perror("'{}' is not an ASCII or UTF-8 encoded text file".format(expanded_path), traceback_war=False)
+ return
- try:
- # Read all lines of the script
- with open(expanded_path, encoding='utf-8') as target:
- script_commands = target.read().splitlines()
- except OSError as ex: # pragma: no cover
- self.perror("Problem accessing script from '{}': {}".format(expanded_path, ex))
- return
+ try:
+ # Read all lines of the script
+ with open(expanded_path, encoding='utf-8') as target:
+ script_commands = target.read().splitlines()
+ except OSError as ex: # pragma: no cover
+ self.perror("Problem accessing script from '{}': {}".format(expanded_path, ex))
+ return
- orig_script_dir_count = len(self._script_dir)
+ orig_script_dir_count = len(self._script_dir)
- try:
- self._script_dir.append(os.path.dirname(expanded_path))
+ try:
+ self._script_dir.append(os.path.dirname(expanded_path))
- if args.transcript:
- self._generate_transcript(script_commands, os.path.expanduser(args.transcript))
- else:
- return self.runcmds_plus_hooks(script_commands)
+ if args.transcript:
+ self._generate_transcript(script_commands, os.path.expanduser(args.transcript))
+ else:
+ return self.runcmds_plus_hooks(script_commands)
+ finally:
+ with self.sigint_protection:
+ # Check if a script dir was added before an exception occurred
+ if orig_script_dir_count != len(self._script_dir):
+ self._script_dir.pop()
finally:
- with self.sigint_protection:
- # Check if a script dir was added before an exception occurred
- if orig_script_dir_count != len(self._script_dir):
- self._script_dir.pop()
if args.__statement__.command == "load":
self.perror("load has been renamed and will be removed in the next release, "
"please use run_script instead\n",