@@ -2957,14 +2957,13 @@ def do_py(self, args: argparse.Namespace) -> bool:
2957
2957
self .perror (err , traceback_war = False )
2958
2958
self ._last_result = CommandResult ('' , err )
2959
2959
return False
2960
- self ._in_py = True
2961
2960
2962
- # noinspection PyBroadException
2963
2961
try :
2962
+ self ._in_py = True
2963
+
2964
2964
# Support the run command even if called prior to invoking an interactive interpreter
2965
- def run (filename : str ):
2965
+ def py_run (filename : str ):
2966
2966
"""Run a Python script file in the interactive console.
2967
-
2968
2967
:param filename: filename of *.py script file to run
2969
2968
"""
2970
2969
expanded_filename = os .path .expanduser (filename )
@@ -2979,9 +2978,16 @@ def run(filename: str):
2979
2978
error_msg = "Error opening script file '{}': {}" .format (expanded_filename , ex )
2980
2979
self .perror (error_msg , traceback_war = False )
2981
2980
2981
+ def py_quit ():
2982
+ """Function callable from the interactive Python console to exit that environment"""
2983
+ raise EmbeddedConsoleExit
2984
+
2985
+ # Set up Python environment
2982
2986
bridge = PyscriptBridge (self )
2983
- self .pystate ['run' ] = run
2984
2987
self .pystate [self .pyscript_name ] = bridge
2988
+ self .pystate ['run' ] = py_run
2989
+ self .pystate ['quit' ] = py_quit
2990
+ self .pystate ['exit' ] = py_quit
2985
2991
2986
2992
if self .locals_in_py :
2987
2993
self .pystate ['self' ] = self
@@ -3002,18 +3008,16 @@ def run(filename: str):
3002
3008
# Set cmd_echo to True so PyscriptBridge statements like: py app('help')
3003
3009
# run at the command line will print their output.
3004
3010
bridge .cmd_echo = True
3005
- interp .runcode (full_command )
3011
+
3012
+ # noinspection PyBroadException
3013
+ try :
3014
+ interp .runcode (full_command )
3015
+ except BaseException :
3016
+ # We don't care about any exception that happened in the interactive console
3017
+ pass
3006
3018
3007
3019
# If there are no args, then we will open an interactive Python console
3008
3020
else :
3009
- # noinspection PyShadowingBuiltins
3010
- def quit ():
3011
- """Function callable from the interactive Python console to exit that environment"""
3012
- raise EmbeddedConsoleExit
3013
-
3014
- self .pystate ['quit' ] = quit
3015
- self .pystate ['exit' ] = quit
3016
-
3017
3021
# Set up readline for Python console
3018
3022
if rl_type != RlType .NONE :
3019
3023
# Save cmd2 history
@@ -3072,10 +3076,12 @@ def quit():
3072
3076
'Run Python code from external script files with: run("script.py")'
3073
3077
.format (self .pyscript_name ))
3074
3078
3079
+ # noinspection PyBroadException
3075
3080
try :
3076
3081
interp .interact (banner = "Python {} on {}\n {}\n \n {}\n " .
3077
3082
format (sys .version , sys .platform , cprt , instructions ))
3078
- except EmbeddedConsoleExit :
3083
+ except BaseException :
3084
+ # We don't care about any exception that happened in the interactive console
3079
3085
pass
3080
3086
3081
3087
finally :
@@ -3109,10 +3115,12 @@ def quit():
3109
3115
else :
3110
3116
sys .modules ['readline' ] = saved_readline
3111
3117
3112
- except Exception :
3118
+ except KeyboardInterrupt :
3113
3119
pass
3120
+
3114
3121
finally :
3115
3122
self ._in_py = False
3123
+
3116
3124
return self ._should_quit
3117
3125
3118
3126
pyscript_parser = ACArgumentParser ()
@@ -3123,21 +3131,30 @@ def quit():
3123
3131
ACTION_ARG_CHOICES , ('path_complete' ,))
3124
3132
3125
3133
@with_argparser (pyscript_parser )
3126
- def do_pyscript (self , args : argparse .Namespace ) -> None :
3134
+ def do_pyscript (self , args : argparse .Namespace ) -> bool :
3127
3135
"""Run a Python script file inside the console"""
3128
3136
script_path = os .path .expanduser (args .script_path )
3137
+ py_return = False
3129
3138
3130
3139
# Save current command line arguments
3131
3140
orig_args = sys .argv
3132
3141
3133
- # Overwrite sys.argv to allow the script to take command line arguments
3134
- sys .argv = [script_path ] + args .script_arguments
3142
+ try :
3143
+ # Overwrite sys.argv to allow the script to take command line arguments
3144
+ sys .argv = [script_path ] + args .script_arguments
3145
+
3146
+ # Run the script - use repr formatting to escape things which
3147
+ # need to be escaped to prevent issues on Windows
3148
+ py_return = self .do_py ("run({!r})" .format (script_path ))
3135
3149
3136
- # Run the script - use repr formatting to escape things which need to be escaped to prevent issues on Windows
3137
- self .do_py ("run({!r})" .format (script_path ))
3150
+ except KeyboardInterrupt :
3151
+ pass
3152
+
3153
+ finally :
3154
+ # Restore command line arguments to original state
3155
+ sys .argv = orig_args
3138
3156
3139
- # Restore command line arguments to original state
3140
- sys .argv = orig_args
3157
+ return py_return
3141
3158
3142
3159
# Only include the do_ipy() method if IPython is available on the system
3143
3160
if ipython_available : # pragma: no cover
0 commit comments