@@ -77,6 +77,7 @@ def log(x):
7777 f = open ('/tmp/bpython.log' , 'a' )
7878 f .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):
205206 finally :
206207 curses .raw (False )
207208
208- return buffer .encode (getpreferredencoding ())
209+ if py3 :
210+ return buffer
211+ else :
212+ return buffer .encode (getpreferredencoding ())
209213
210214 def read (self , x ):
211215 pass
@@ -259,7 +263,7 @@ def make_colors():
259263 }
260264 for i in range (63 ):
261265 if i > 7 :
262- j = i / 8
266+ j = i // 8
263267 else :
264268 j = c [OPTS .color_scheme ['background' ]]
265269 curses .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()
297301 code .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
304309 def 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 )
379384 if __getattr__ is not None :
380385 try :
@@ -795,7 +800,7 @@ def show_list(self, items, topline=None):
795800 shared .wl = 0
796801 y , x = self .scr .getyx ()
797802 h , w = self .scr .getmaxyx ()
798- down = (y < h / 2 )
803+ down = (y < h // 2 )
799804 if down :
800805 max_h = h - y
801806 else :
@@ -814,8 +819,8 @@ def lsize():
814819 wl = max (len (i ) for i in v_items ) + 1
815820 if not wl :
816821 wl = 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
820825 if cols * rows < len (v_items ):
821826 rows += 1
@@ -1188,7 +1193,10 @@ def reevaluate(self):
11881193
11891194 self .iy , self .ix = self .scr .getyx ()
11901195 for 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 '
11921200 self .print_line (line )
11931201 self .s_hist [- 1 ] += self .f_string
11941202# I decided it was easier to just do this manually
@@ -1263,7 +1271,10 @@ def repl(self):
12631271 self .h_i = 0
12641272 self .history .append (inp )
12651273 self .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:
12681279 if inp :
12691280 self .rl_hist .append (inp + '\n ' )
@@ -1303,7 +1314,7 @@ def write(self, s):
13031314 else :
13041315 t = s
13051316
1306- if isinstance (t , unicode ):
1317+ if not py3 and isinstance (t , unicode ):
13071318 t = t .encode (getpreferredencoding ())
13081319
13091320 if 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 ):
13421353 s = s .encode (getpreferredencoding ())
13431354
13441355 a = get_colpair ('output' )
@@ -1859,7 +1870,8 @@ def get_key(self):
18591870 while True :
18601871 try :
18611872 key += self .scr .getkey ()
1862- key = key .decode (getpreferredencoding ())
1873+ if not py3 :
1874+ key = key .decode (getpreferredencoding ())
18631875 self .scr .nodelay (False )
18641876 except UnicodeDecodeError :
18651877# Yes, that actually kind of sucks, but I don't see another way to get
0 commit comments