@@ -3270,11 +3270,10 @@ def do_run_pyscript(self, args: argparse.Namespace) -> bool:
32703270 finally :
32713271 # Restore command line arguments to original state
32723272 sys .argv = orig_args
3273-
3274- if args .__statement__ .command == "pyscript" :
3275- self .perror ("pyscript has been renamed and will be removed in the next release, "
3276- "please use run_pyscript instead\n " ,
3277- traceback_war = False , err_color = Fore .LIGHTYELLOW_EX )
3273+ if args .__statement__ .command == "pyscript" :
3274+ self .perror ("pyscript has been renamed and will be removed in the next release, "
3275+ "please use run_pyscript instead\n " ,
3276+ traceback_war = False , err_color = Fore .LIGHTYELLOW_EX )
32783277
32793278 return py_return
32803279
@@ -3655,49 +3654,52 @@ def do_run_script(self, args: argparse.Namespace) -> Optional[bool]:
36553654 """
36563655 expanded_path = os .path .abspath (os .path .expanduser (args .script_path ))
36573656
3658- # Make sure the path exists and we can access it
3659- if not os .path .exists (expanded_path ):
3660- self .perror ("'{}' does not exist or cannot be accessed" .format (expanded_path ), traceback_war = False )
3661- return
3657+ # Wrap everything in a try/finally just to make sure the warning prints at end if `load` was called
3658+ try :
3659+ # Make sure the path exists and we can access it
3660+ if not os .path .exists (expanded_path ):
3661+ self .perror ("'{}' does not exist or cannot be accessed" .format (expanded_path ), traceback_war = False )
3662+ return
36623663
3663- # Make sure expanded_path points to a file
3664- if not os .path .isfile (expanded_path ):
3665- self .perror ("'{}' is not a file" .format (expanded_path ), traceback_war = False )
3666- return
3664+ # Make sure expanded_path points to a file
3665+ if not os .path .isfile (expanded_path ):
3666+ self .perror ("'{}' is not a file" .format (expanded_path ), traceback_war = False )
3667+ return
36673668
3668- # Make sure the file is not empty
3669- if os .path .getsize (expanded_path ) == 0 :
3670- self .perror ("'{}' is empty" .format (expanded_path ), traceback_war = False )
3671- return
3669+ # Make sure the file is not empty
3670+ if os .path .getsize (expanded_path ) == 0 :
3671+ self .perror ("'{}' is empty" .format (expanded_path ), traceback_war = False )
3672+ return
36723673
3673- # Make sure the file is ASCII or UTF-8 encoded text
3674- if not utils .is_text_file (expanded_path ):
3675- self .perror ("'{}' is not an ASCII or UTF-8 encoded text file" .format (expanded_path ), traceback_war = False )
3676- return
3674+ # Make sure the file is ASCII or UTF-8 encoded text
3675+ if not utils .is_text_file (expanded_path ):
3676+ self .perror ("'{}' is not an ASCII or UTF-8 encoded text file" .format (expanded_path ), traceback_war = False )
3677+ return
36773678
3678- try :
3679- # Read all lines of the script
3680- with open (expanded_path , encoding = 'utf-8' ) as target :
3681- script_commands = target .read ().splitlines ()
3682- except OSError as ex : # pragma: no cover
3683- self .perror ("Problem accessing script from '{}': {}" .format (expanded_path , ex ))
3684- return
3679+ try :
3680+ # Read all lines of the script
3681+ with open (expanded_path , encoding = 'utf-8' ) as target :
3682+ script_commands = target .read ().splitlines ()
3683+ except OSError as ex : # pragma: no cover
3684+ self .perror ("Problem accessing script from '{}': {}" .format (expanded_path , ex ))
3685+ return
36853686
3686- orig_script_dir_count = len (self ._script_dir )
3687+ orig_script_dir_count = len (self ._script_dir )
36873688
3688- try :
3689- self ._script_dir .append (os .path .dirname (expanded_path ))
3689+ try :
3690+ self ._script_dir .append (os .path .dirname (expanded_path ))
36903691
3691- if args .transcript :
3692- self ._generate_transcript (script_commands , os .path .expanduser (args .transcript ))
3693- else :
3694- return self .runcmds_plus_hooks (script_commands )
3692+ if args .transcript :
3693+ self ._generate_transcript (script_commands , os .path .expanduser (args .transcript ))
3694+ else :
3695+ return self .runcmds_plus_hooks (script_commands )
36953696
3697+ finally :
3698+ with self .sigint_protection :
3699+ # Check if a script dir was added before an exception occurred
3700+ if orig_script_dir_count != len (self ._script_dir ):
3701+ self ._script_dir .pop ()
36963702 finally :
3697- with self .sigint_protection :
3698- # Check if a script dir was added before an exception occurred
3699- if orig_script_dir_count != len (self ._script_dir ):
3700- self ._script_dir .pop ()
37013703 if args .__statement__ .command == "load" :
37023704 self .perror ("load has been renamed and will be removed in the next release, "
37033705 "please use run_script instead\n " ,
0 commit comments