@@ -181,24 +181,25 @@ def next_token_inside_string(s, inside_string):
181181
182182class Interpreter (code .InteractiveInterpreter ):
183183
184- def __init__ (self ):
184+ def __init__ (self , encoding ):
185185 """The syntaxerror callback can be set at any time and will be called
186186 on a caught syntax error. The purpose for this in bpython is so that
187187 the repl can be instantiated after the interpreter (which it
188188 necessarily must be with the current factoring) and then an exception
189189 callback can be added to the Interpeter instance afterwards - more
190190 specifically, this is so that autoindentation does not occur after a
191- traceback.
192-
193- Interpreter.tblist_hook can be a function that will receive the tblist
194- from showtraceback() (after it has been modified to retain only the
195- last element) as the only argument and should mutate it in place."""
191+ traceback."""
196192
193+ self .encoding = encoding
197194 self .syntaxerror_callback = None
198- self .tblist_hook = None
199195# Unfortunately code.InteractiveInterpreter is a classic class, so no super()
200196 code .InteractiveInterpreter .__init__ (self )
201197
198+ def runsource (self , source ):
199+ source = '# coding: %s\n %s' % (self .encoding ,
200+ source .encode (self .encoding ))
201+ return code .InteractiveInterpreter .runsource (self , source )
202+
202203 def showsyntaxerror (self , filename = None ):
203204 """Override the regular handler, the code's copied and pasted from
204205 code.py, as per showtraceback, but with the syntaxerror callback called
@@ -217,8 +218,8 @@ def showsyntaxerror(self, filename=None):
217218 # Not the format we expect; leave it alone
218219 pass
219220 else :
220- # Stuff in the right filename
221- value = SyntaxError (msg , (filename , lineno , offset , line ))
221+ # Stuff in the right filename and right lineno
222+ value = SyntaxError (msg , (filename , lineno - 1 , offset , line ))
222223 sys .last_value = value
223224 list = traceback .format_exception_only (type , value )
224225 self .writetb (list )
@@ -234,8 +235,8 @@ def showtraceback(self):
234235 sys .last_traceback = tb
235236 tblist = traceback .extract_tb (tb )
236237 del tblist [:1 ]
237- if self . tblist_hook is not None :
238- self . tblist_hook (tblist )
238+ # Set the right lineno (encoding header adds an extra line)
239+ tblist [ 0 ] = (tblist [ 0 ][ 0 ], 1 ) + tblist [ 0 ][ 2 :]
239240
240241 l = traceback .format_list (tblist )
241242 if l :
@@ -334,8 +335,6 @@ def __init__(self, scr, interp, statusbar=None, idle=None):
334335 self .paste_time = 0.02
335336 sys .path .insert (0 , '.' )
336337
337- self .interp .tblist_hook = self .fix_traceback_offset
338-
339338 if not OPTS .arg_spec :
340339 return
341340
@@ -345,14 +344,6 @@ def __init__(self, scr, interp, statusbar=None, idle=None):
345344 'ignore' ) as hfile :
346345 self .rl_hist = hfile .readlines ()
347346
348- def fix_traceback_offset (self , tblist ):
349- """Will be assigned to interpreter.tblist_hook and, if the interpreter
350- supports it, will be called when the tblist is created and modified to
351- contain only the last value. This is basically a little hack to fix the
352- line number offset for the traceback due to us inserting the encoding
353- header into the interpreter."""
354- tblist [0 ] = (tblist [0 ][0 ], 1 ) + tblist [0 ][2 :]
355-
356347 def clean_object (self , obj ):
357348 """Try to make an object not exhibit side-effects on attribute
358349 lookup. Return the type's magic attributes so they can be reapplied
@@ -913,10 +904,7 @@ def push(self, s):
913904 self .buffer .append (s )
914905
915906 try :
916- encoding = getpreferredencoding ()
917- source = '# coding: %s\n ' % (encoding , )
918- source += '\n ' .join (self .buffer ).encode (encoding )
919- more = self .interp .runsource (source )
907+ more = self .interp .runsource ('\n ' .join (self .buffer ))
920908 except SystemExit :
921909 # Avoid a traceback on e.g. quit()
922910 self .do_exit = True
@@ -1962,7 +1950,7 @@ def main_curses(scr):
19621950
19631951 curses .raw (True )
19641952
1965- interpreter = Interpreter ()
1953+ interpreter = Interpreter (getpreferredencoding () )
19661954
19671955 repl = Repl (main_win , interpreter , statusbar , idle )
19681956 interpreter .syntaxerror_callback = repl .clear_current_line
0 commit comments