@@ -77,6 +77,7 @@ def log(x):
7777f = open ('/tmp/bpython.log' ,'a' )
7878f .write ('%s\n ' % (x ,))
7979
80+ py3 = sys .version_info [0 ]== 3
8081orig_stdout = sys .__stdout__
8182stdscr = None
8283
@@ -205,7 +206,10 @@ def readline(self):
205206finally :
206207curses .raw (False )
207208
208- return buffer .encode (getpreferredencoding ())
209+ if py3 :
210+ return buffer
211+ else :
212+ return buffer .encode (getpreferredencoding ())
209213
210214def read (self ,x ):
211215pass
@@ -259,7 +263,7 @@ def make_colors():
259263 }
260264for i in range (63 ):
261265if i > 7 :
262- j = i / 8
266+ j = i // 8
263267else :
264268j = c [OPTS .color_scheme ['background' ]]
265269curses .init_pair (i + 1 ,i % 8 ,j )
@@ -296,10 +300,11 @@ def __init__(self, locals=None, encoding=sys.getdefaultencoding()):
296300# Unfortunately code.InteractiveInterpreter is a classic class, so no super()
297301code .InteractiveInterpreter .__init__ (self ,locals )
298302
299- def runsource (self ,source ):
300- source = '# coding: %s\n %s' % (self .encoding ,
301- source .encode (self .encoding ))
302- return code .InteractiveInterpreter .runsource (self ,source )
303+ if not py3 :
304+ def runsource (self ,source ):
305+ source = '# coding: %s\n %s' % (self .encoding ,
306+ source .encode (self .encoding ))
307+ return code .InteractiveInterpreter .runsource (self ,source )
303308
304309def showsyntaxerror (self ,filename = None ):
305310"""Override the regular handler, the code's copied and pasted from
@@ -374,7 +379,7 @@ def __enter__(self):
374379# original methods. :-(
375380# The upshot being that introspecting on an object to display its
376381# attributes will avoid unwanted side-effects.
377- if type_ != types .InstanceType :
382+ if py3 or type_ != types .InstanceType :
378383__getattr__ = getattr (type_ ,'__getattr__' ,None )
379384if __getattr__ is not None :
380385try :
@@ -795,7 +800,7 @@ def show_list(self, items, topline=None):
795800shared .wl = 0
796801y ,x = self .scr .getyx ()
797802h ,w = self .scr .getmaxyx ()
798- down = (y < h / 2 )
803+ down = (y < h // 2 )
799804if down :
800805max_h = h - y
801806else :
@@ -814,8 +819,8 @@ def lsize():
814819wl = max (len (i )for i in v_items )+ 1
815820if not wl :
816821wl = 1
817- cols = ((max_w - 2 )/ wl )or 1
818- rows = len (v_items )/ cols
822+ cols = ((max_w - 2 )// wl )or 1
823+ rows = len (v_items )// cols
819824
820825if cols * rows < len (v_items ):
821826rows += 1
@@ -1188,7 +1193,10 @@ def reevaluate(self):
11881193
11891194self .iy ,self .ix = self .scr .getyx ()
11901195for line in self .history :
1191- self .stdout_hist += line .encode (getpreferredencoding ())+ '\n '
1196+ if py3 :
1197+ self .stdout_hist += line + '\n '
1198+ else :
1199+ self .stdout_hist += line .encode (getpreferredencoding ())+ '\n '
11921200self .print_line (line )
11931201self .s_hist [- 1 ]+= self .f_string
11941202# I decided it was easier to just do this manually
@@ -1263,7 +1271,10 @@ def repl(self):
12631271self .h_i = 0
12641272self .history .append (inp )
12651273self .s_hist [- 1 ]+= self .f_string
1266- self .stdout_hist += inp .encode (getpreferredencoding ())+ '\n '
1274+ if py3 :
1275+ self .stdout_hist += inp + '\n '
1276+ else :
1277+ self .stdout_hist += inp .encode (getpreferredencoding ())+ '\n '
12671278# Keep two copies so you can go up and down in the hist:
12681279if inp :
12691280self .rl_hist .append (inp + '\n ' )
@@ -1303,7 +1314,7 @@ def write(self, s):
13031314else :
13041315t = s
13051316
1306- if isinstance (t ,unicode ):
1317+ if not py3 and isinstance (t ,unicode ):
13071318t = t .encode (getpreferredencoding ())
13081319
13091320if not self .stdout_hist :
@@ -1338,7 +1349,7 @@ def echo(self, s, redraw=True):
13381349 uses the formatting method as defined in formatter.py to parse the
13391350 srings. It won't update the screen if it's reevaluating the code (as it
13401351 does with undo)."""
1341- if isinstance (s ,unicode ):
1352+ if not py3 and isinstance (s ,unicode ):
13421353s = s .encode (getpreferredencoding ())
13431354
13441355a = get_colpair ('output' )
@@ -1859,7 +1870,8 @@ def get_key(self):
18591870while True :
18601871try :
18611872key += self .scr .getkey ()
1862- key = key .decode (getpreferredencoding ())
1873+ if not py3 :
1874+ key = key .decode (getpreferredencoding ())
18631875self .scr .nodelay (False )
18641876except UnicodeDecodeError :
18651877# Yes, that actually kind of sucks, but I don't see another way to get