12
12
13
13
from bpython .curtsiesfrontend .parse import parse
14
14
from bpython .repl import Interpreter as ReplInterpreter
15
+ from bpython .config import getpreferredencoding
16
+ from bpython ._py3compat import py3
15
17
16
18
default_colors = {
17
19
Generic .Error : 'R' ,
@@ -75,66 +77,30 @@ def __init__(self, locals=None):
75
77
"""
76
78
if locals is None :
77
79
locals = {"__name__" : "__console__" , "__doc__" : None }
78
- ReplInterpreter .__init__ (self , locals )
80
+ ReplInterpreter .__init__ (self , locals , getpreferredencoding ())
81
+
79
82
self .locals = locals
80
83
self .compile = CommandCompiler ()
81
84
82
85
# typically changed after being instantiated
83
86
self .write = lambda stuff : sys .stderr .write (stuff )
84
87
self .outfile = self
85
88
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 )
114
91
lexer = get_lexer_by_name ("pytb" )
115
92
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 )
138
104
139
105
def format (self , tbtext , lexer ):
140
106
traceback_informative_formatter = BPythonFormatter (default_colors )
@@ -160,12 +126,6 @@ def format(self, tbtext, lexer):
160
126
cur_line .append ((token , text ))
161
127
assert cur_line == [], cur_line
162
128
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 )
169
129
170
130
def code_finished_will_parse (s , compiler ):
171
131
"""Returns a tuple of whether the buffer could be complete and whether it
0 commit comments