X Tutup
Skip to content

Commit 52d05f1

Browse files
committed
A first step towards Python 3 support.
Please note that it requires Pygments > 1.0 and some things might not work yet.
1 parent 171eb8e commit 52d05f1

File tree

4 files changed

+52
-19
lines changed

4 files changed

+52
-19
lines changed

bpython/cli.py

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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
8081
orig_stdout = sys.__stdout__
8182
stdscr = 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

bpython/keys.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ def __setitem__(self, key, value):
4141
key_dispatch = KeyMap()
4242

4343
# fill dispatch with letters
44-
for c in string.lowercase:
45-
key_dispatch['C-%s' % c] = (chr(string.lowercase.index(c)+1), '^%s' % c.upper())
44+
for c in string.ascii_lowercase:
45+
key_dispatch['C-%s' % c] = (chr(string.ascii_lowercase.index(c)+1),
46+
'^%s' % c.upper())
4647

4748
# fill dispatch with cool characters
4849
key_dispatch['C-['] = (chr(27), '^[')

data/bpython

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env python
2+
3+
import sys
4+
from bpython.cli import main
5+
6+
sys.exit(main())

setup.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4-
from setuptools import setup
4+
55
import glob
66
import os
77
import platform
88
import re
99
import sys
10+
try:
11+
from setuptools import setup
12+
using_setuptools = True
13+
except ImportError:
14+
from distutils.core import setup
15+
using_setuptools = False
16+
17+
try:
18+
from distutils.command.build_py import build_py_2to3 as build_py
19+
except ImportError:
20+
from distutils.command.build_py import build_py
1021

1122
from bpython import __version__
1223

24+
1325
if platform.system() == 'FreeBSD':
1426
man_dir = 'man'
1527
else:
@@ -38,7 +50,9 @@
3850
'console_scripts': [
3951
'bpython = bpython.cli:main',
4052
],
41-
}
53+
},
54+
scripts = ([] if using_setuptools else ['data/bpython']),
55+
cmdclass=dict(build_py=build_py)
4256
)
4357

4458
# vim: encoding=utf-8 sw=4 ts=4 sts=4 ai et sta

0 commit comments

Comments
 (0)
X Tutup