3939from pathlib import Path
4040from types import ModuleType , TracebackType
4141from typing import (
42+ Iterable ,
4243 cast ,
4344 List ,
4445 Tuple ,
@@ -155,7 +156,7 @@ def runsource(
155156 with self .timer :
156157 return super ().runsource (source , filename , symbol )
157158
158- def showsyntaxerror (self , filename = None ):
159+ def showsyntaxerror (self , filename : Optional [ str ] = None ) -> None :
159160 """Override the regular handler, the code's copied and pasted from
160161 code.py, as per showtraceback, but with the syntaxerror callback called
161162 and the text in a pretty colour."""
@@ -182,7 +183,7 @@ def showsyntaxerror(self, filename=None):
182183 exc_formatted = traceback .format_exception_only (exc_type , value )
183184 self .writetb (exc_formatted )
184185
185- def showtraceback (self ):
186+ def showtraceback (self ) -> None :
186187 """This needs to override the default traceback thing
187188 so it can put it into a pretty colour and maybe other
188189 stuff, I don't know"""
@@ -194,11 +195,10 @@ def showtraceback(self):
194195 tblist = traceback .extract_tb (tb )
195196 del tblist [:1 ]
196197
197- for i , (fname , lineno , module , something ) in enumerate (tblist ):
198- # strip linecache line number
199- if self .bpython_input_re .match (fname ):
200- fname = "<input>"
201- tblist [i ] = (fname , lineno , module , something )
198+ for frame in tblist :
199+ if self .bpython_input_re .match (frame .filename ):
200+ # strip linecache line number
201+ frame .filename = "<input>"
202202
203203 l = traceback .format_list (tblist )
204204 if l :
@@ -209,7 +209,7 @@ def showtraceback(self):
209209
210210 self .writetb (l )
211211
212- def writetb (self , lines ) :
212+ def writetb (self , lines : Iterable [ str ]) -> None :
213213 """This outputs the traceback and should be overridden for anything
214214 fancy."""
215215 for line in lines :
@@ -463,9 +463,8 @@ def __init__(self, interp: Interpreter, config: Config):
463463 # all input and output, stored as old style format strings
464464 # (\x01, \x02, ...) for cli.py
465465 self .screen_hist : List [str ] = []
466- self .history : List [
467- str
468- ] = [] # commands executed since beginning of session
466+ # commands executed since beginning of session
467+ self .history : List [str ] = []
469468 self .redo_stack : List [str ] = []
470469 self .evaluating = False
471470 self .matches_iter = MatchesIterator ()
@@ -870,25 +869,22 @@ def write2file(self) -> None:
870869 self .interact .notify (_ ("Save cancelled." ))
871870 return
872871
873- fn = Path (fn ).expanduser ()
874- if fn .suffix != ".py" and self .config .save_append_py :
872+ path = Path (fn ).expanduser ()
873+ if path .suffix != ".py" and self .config .save_append_py :
875874 # fn.with_suffix(".py") does not append if fn has a non-empty suffix
876- fn = Path (f"{ fn } .py" )
875+ path = Path (f"{ path } .py" )
877876
878877 mode = "w"
879- if fn .exists ():
880- mode = self .interact .file_prompt (
878+ if path .exists ():
879+ new_mode = self .interact .file_prompt (
881880 _ (
882- "%s already exists. Do you "
883- "want to (c)ancel, "
884- " (o)verwrite or "
885- "(a)ppend? "
881+ "%s already exists. Do you want to (c)ancel, (o)verwrite or (a)ppend? "
886882 )
887- % (fn ,)
883+ % (path ,)
888884 )
889- if mode in ("o" , "overwrite" , _ ("overwrite" )):
885+ if new_mode in ("o" , "overwrite" , _ ("overwrite" )):
890886 mode = "w"
891- elif mode in ("a" , "append" , _ ("append" )):
887+ elif new_mode in ("a" , "append" , _ ("append" )):
892888 mode = "a"
893889 else :
894890 self .interact .notify (_ ("Save cancelled." ))
@@ -897,12 +893,12 @@ def write2file(self) -> None:
897893 stdout_text = self .get_session_formatted_for_file ()
898894
899895 try :
900- with open (fn , mode ) as f :
896+ with open (path , mode ) as f :
901897 f .write (stdout_text )
902898 except OSError as e :
903- self .interact .notify (_ ("Error writing file '%s': %s" ) % (fn , e ))
899+ self .interact .notify (_ ("Error writing file '%s': %s" ) % (path , e ))
904900 else :
905- self .interact .notify (_ ("Saved to %s." ) % (fn ,))
901+ self .interact .notify (_ ("Saved to %s." ) % (path ,))
906902
907903 def copy2clipboard (self ) -> None :
908904 """Copy current content to clipboard."""
@@ -1003,6 +999,10 @@ def prompt_undo(self) -> int:
1003999 _ ("Undo how many lines? (Undo will take up to ~%.1f seconds) [1]" )
10041000 % (est ,)
10051001 )
1002+ if m is None :
1003+ self .interact .notify (_ ("Undo canceled" ), 0.1 )
1004+ return 0
1005+
10061006 try :
10071007 if m == "" :
10081008 m = "1"
0 commit comments