summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cmd2/cmd2.py80
-rwxr-xr-xexamples/hooks.py4
-rw-r--r--tests/test_cmd2.py2
-rw-r--r--tests/test_run_pyscript.py2
4 files changed, 45 insertions, 43 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",
diff --git a/examples/hooks.py b/examples/hooks.py
index c533c696..42224403 100755
--- a/examples/hooks.py
+++ b/examples/hooks.py
@@ -38,10 +38,10 @@ class CmdLineApp(cmd2.Cmd):
# Setting this true makes it run a shell command if a cmd2/cmd command doesn't exist
# default_to_shell = True
def __init__(self, *args, **kwargs):
- # sneakily remove the cmd2.Cmd command called load
+ # sneakily remove the cmd2.Cmd command called run_script
# this lets a user enter a command like "l5" and allows it to
# be unambiguous
- delattr(cmd2.Cmd, "do_load")
+ delattr(cmd2.Cmd, "do_run_script")
super().__init__(*args, **kwargs)
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index a8278351..77542d76 100644
--- a/tests/test_cmd2.py
+++ b/tests/test_cmd2.py
@@ -288,7 +288,7 @@ def test_run_script(base_app, request):
def test_load_deprecated(base_app):
"""Delete this when load alias is removed"""
_, err = run_cmd(base_app, "load fake")
- assert "load has been renamed and will be removed" in err[0]
+ assert "load has been renamed and will be removed" in err[-1]
def test_run_script_with_empty_args(base_app):
out, err = run_cmd(base_app, 'run_script')
diff --git a/tests/test_run_pyscript.py b/tests/test_run_pyscript.py
index c8b44271..9eb33b31 100644
--- a/tests/test_run_pyscript.py
+++ b/tests/test_run_pyscript.py
@@ -88,4 +88,4 @@ def test_run_pyscript_stop(base_app, request):
def test_pyscript_deprecated(base_app):
"""Delete this when pyscript alias is removed"""
_, err = run_cmd(base_app, "pyscript fake")
- assert "pyscript has been renamed and will be removed" in err[0]
+ assert "pyscript has been renamed and will be removed" in err[-1]