1212
1313from bpython .curtsiesfrontend .parse import parse
1414from bpython .repl import Interpreter as ReplInterpreter
15+ from bpython .config import getpreferredencoding
16+ from bpython ._py3compat import py3
1517
1618default_colors = {
1719 Generic .Error : 'R' ,
@@ -75,66 +77,30 @@ def __init__(self, locals=None):
7577 """
7678 if locals is None :
7779 locals = {"__name__" : "__console__" , "__doc__" : None }
78- ReplInterpreter .__init__ (self , locals )
80+ ReplInterpreter .__init__ (self , locals , getpreferredencoding ())
81+
7982 self .locals = locals
8083 self .compile = CommandCompiler ()
8184
8285 # typically changed after being instantiated
8386 self .write = lambda stuff : sys .stderr .write (stuff )
8487 self .outfile = self
8588
86- def showsyntaxerror (self , filename = None ):
87- """Display the syntax error that just occurred.
88-
89- This doesn't display a stack trace because there isn't one.
90-
91- If a filename is given, it is stuffed in the exception instead
92- of what was there before (because Python's parser always uses
93- "<string>" when reading from a string).
94-
95- The output is written by self.write(), below.
96-
97- """
98- type , value , sys .last_traceback = sys .exc_info ()
99- sys .last_type = type
100- sys .last_value = value
101- if filename and type is SyntaxError :
102- # Work hard to stuff the correct filename in the exception
103- try :
104- msg , (dummy_filename , lineno , offset , line ) = value
105- except :
106- # Not the format we expect; leave it alone
107- pass
108- else :
109- # Stuff in the right filename
110- value = SyntaxError (msg , (filename , lineno , offset , line ))
111- sys .last_value = value
112- l = traceback .format_exception_only (type , value )
113- tbtext = '' .join (l )
89+ def writetb (self , lines ):
90+ tbtext = '' .join (lines )
11491 lexer = get_lexer_by_name ("pytb" )
11592 self .format (tbtext , lexer )
116-
117- def showtraceback (self ):
118- """Display the exception that just occurred.
119-
120- We remove the first stack item because it is our own code.
121-
122-
123- """
124- type , value , tb = sys .exc_info ()
125- sys .last_type = type
126- sys .last_value = value
127- sys .last_traceback = tb
128- tblist = traceback .extract_tb (tb )
129- del tblist [:1 ]
130- l = traceback .format_list (tblist )
131- if l :
132- l .insert (0 , "Traceback (most recent call last):\n " )
133- l [len (l ):] = traceback .format_exception_only (type , value )
134- tbtext = '' .join (l )
135- lexer = get_lexer_by_name ("pytb" , stripall = True )
136-
137- self .format (tbtext , lexer )
93+ # TODO for tracebacks get_lexer_by_name("pytb", stripall=True)
94+
95+ if not py3 :
96+ def runsource (self , source , filename = "<input>" , symbol = "single" ,
97+ encode = True ):
98+ # TODO: write a test for and implement encoding
99+ with self .timer :
100+ if encode :
101+ source = '#\n %s' % (source ,) # dummy line so linenos match
102+ return code .InteractiveInterpreter .runsource (self , source ,
103+ filename , symbol )
138104
139105 def format (self , tbtext , lexer ):
140106 traceback_informative_formatter = BPythonFormatter (default_colors )
@@ -160,12 +126,6 @@ def format(self, tbtext, lexer):
160126 cur_line .append ((token , text ))
161127 assert cur_line == [], cur_line
162128
163- def runsource (self , source , filename = "<input>" , symbol = "single" ,
164- encode = None ):
165- # TODO: encode does nothing
166- with self .timer :
167- return code .InteractiveInterpreter .runsource (
168- self , source , filename , symbol )
169129
170130def code_finished_will_parse (s , compiler ):
171131 """Returns a tuple of whether the buffer could be complete and whether it
0 commit comments