Skip to content

Commit 02b4d6b

Browse files
committed
Remove bpython._py3compat.py3
1 parent 1ef1776 commit 02b4d6b

File tree

16 files changed

+94
-467
lines changed

16 files changed

+94
-467
lines changed

bpython/_py3compat.py

Lines changed: 0 additions & 36 deletions
This file was deleted.

bpython/autocomplete.py

Lines changed: 13 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,9 @@
3737
from . import importcompletion
3838
from . import line as lineparts
3939
from .line import LinePart
40-
from ._py3compat import py3
4140
from .lazyre import LazyReCompile
4241
from .simpleeval import safe_eval, evaluate_current_expression, EvaluationError
4342

44-
if not py3:
45-
from types import InstanceType, ClassType
46-
4743

4844
# Autocomplete modes
4945
SIMPLE = "simple"
@@ -109,10 +105,7 @@
109105
)
110106
)
111107

112-
if py3:
113-
KEYWORDS = frozenset(keyword.kwlist)
114-
else:
115-
KEYWORDS = frozenset(name.decode("ascii") for name in keyword.kwlist)
108+
KEYWORDS = frozenset(keyword.kwlist)
116109

117110

118111
def after_last_dot(name):
@@ -253,19 +246,8 @@ class FilenameCompletion(BaseCompletionType):
253246
def __init__(self, mode=SIMPLE):
254247
super(FilenameCompletion, self).__init__(False, mode)
255248

256-
if py3:
257-
258-
def safe_glob(self, pathname):
259-
return glob.iglob(glob.escape(pathname) + "*")
260-
261-
else:
262-
263-
def safe_glob(self, pathname):
264-
try:
265-
return glob.glob(pathname + "*")
266-
except re.error:
267-
# see #491
268-
return tuple()
249+
def safe_glob(self, pathname):
250+
return glob.iglob(glob.escape(pathname) + "*")
269251

270252
def matches(self, cursor_offset, line, **kwargs):
271253
cs = lineparts.current_string(cursor_offset, line)
@@ -366,39 +348,18 @@ def attr_lookup(self, obj, expr, attr):
366348
except ValueError:
367349
pass
368350

369-
if not py3 and isinstance(obj, (InstanceType, ClassType)):
370-
# Account for the __dict__ in an old-style class.
371-
words.append("__dict__")
372-
373351
matches = []
374352
n = len(attr)
375353
for word in words:
376354
if self.method_match(word, n, attr) and word != "__builtins__":
377355
matches.append("%s.%s" % (expr, word))
378356
return matches
379357

380-
if py3:
381-
382-
def list_attributes(self, obj):
383-
# TODO: re-implement dir using getattr_static to avoid using
384-
# AttrCleaner here?
385-
with inspection.AttrCleaner(obj):
386-
return dir(obj)
387-
388-
else:
389-
390-
def list_attributes(self, obj):
391-
with inspection.AttrCleaner(obj):
392-
if isinstance(obj, InstanceType):
393-
try:
394-
return dir(obj)
395-
except Exception:
396-
# This is a case where we can not prevent user code from
397-
# running. We return a default list attributes on error
398-
# instead. (#536)
399-
return ["__doc__", "__module__"]
400-
else:
401-
return dir(obj)
358+
def list_attributes(self, obj):
359+
# TODO: re-implement dir using getattr_static to avoid using
360+
# AttrCleaner here?
361+
with inspection.AttrCleaner(obj):
362+
return dir(obj)
402363

403364

404365
class DictKeyCompletion(BaseCompletionType):
@@ -501,12 +462,11 @@ def matches(self, cursor_offset, line, **kwargs):
501462
for name in argspec[1][0]
502463
if isinstance(name, string_types) and name.startswith(r.word)
503464
)
504-
if py3:
505-
matches.update(
506-
name + "="
507-
for name in argspec[1][4]
508-
if name.startswith(r.word)
509-
)
465+
matches.update(
466+
name + "="
467+
for name in argspec[1][4]
468+
if name.startswith(r.word)
469+
)
510470
return matches if matches else None
511471

512472
def locate(self, current_offset, line):

bpython/cli.py

Lines changed: 16 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
from pygments import format
6464
from pygments.formatters import TerminalFormatter
6565
from pygments.lexers import Python3Lexer
66-
from ._py3compat import py3
6766
from pygments.token import Token
6867
from .formatter import BPythonFormatter
6968

@@ -85,9 +84,6 @@
8584
from .pager import page
8685
from .args import parse as argsparse
8786

88-
if not py3:
89-
import inspect
90-
9187

9288
# --- module globals ---
9389
stdscr = None
@@ -220,10 +216,7 @@ def readline(self, size=-1):
220216
self.buffer.append(rest)
221217
buffer = buffer[:size]
222218

223-
if py3:
224-
return buffer
225-
else:
226-
return buffer.encode(getpreferredencoding())
219+
return buffer
227220

228221
def read(self, size=None):
229222
if size == 0:
@@ -536,9 +529,6 @@ def echo(self, s, redraw=True):
536529
uses the formatting method as defined in formatter.py to parse the
537530
srings. It won't update the screen if it's reevaluating the code (as it
538531
does with undo)."""
539-
if not py3 and isinstance(s, unicode):
540-
s = s.encode(getpreferredencoding())
541-
542532
a = get_colpair(self.config, "output")
543533
if "\x01" in s:
544534
rx = re.search("\x01([A-Za-z])([A-Za-z]?)", s)
@@ -629,14 +619,11 @@ def get_key(self):
629619
while True:
630620
try:
631621
key += self.scr.getkey()
632-
if py3:
633-
# Seems like we get a in the locale's encoding
634-
# encoded string in Python 3 as well, but of
635-
# type str instead of bytes, hence convert it to
636-
# bytes first and decode then
637-
key = key.encode("latin-1").decode(getpreferredencoding())
638-
else:
639-
key = key.decode(getpreferredencoding())
622+
# Seems like we get a in the locale's encoding
623+
# encoded string in Python 3 as well, but of
624+
# type str instead of bytes, hence convert it to
625+
# bytes first and decode then
626+
key = key.encode("latin-1").decode(getpreferredencoding())
640627
self.scr.nodelay(False)
641628
except UnicodeDecodeError:
642629
# Yes, that actually kind of sucks, but I don't see another way to get
@@ -726,9 +713,8 @@ def mkargspec(self, topline, in_arg, down):
726713
_args = topline.argspec.varargs
727714
_kwargs = topline.argspec.varkwargs
728715
is_bound_method = topline.is_bound_method
729-
if py3:
730-
kwonly = topline.argspec.kwonly
731-
kwonly_defaults = topline.argspec.kwonly_defaults or dict()
716+
kwonly = topline.argspec.kwonly
717+
kwonly_defaults = topline.argspec.kwonly_defaults or dict()
732718
max_w = int(self.scr.getmaxyx()[1] * 0.6)
733719
self.list_win.erase()
734720
self.list_win.resize(3, max_w)
@@ -776,13 +762,7 @@ def mkargspec(self, topline, in_arg, down):
776762
if k == in_arg or i == in_arg:
777763
color |= curses.A_BOLD
778764

779-
if not py3:
780-
# See issue #138: We need to format tuple unpacking correctly
781-
# We use the undocumented function inspection.strseq() for
782-
# that. Fortunately, that madness is gone in Python 3.
783-
self.list_win.addstr(inspect.strseq(i, str), color)
784-
else:
785-
self.list_win.addstr(str(i), color)
765+
self.list_win.addstr(str(i), color)
786766
if kw is not None:
787767
self.list_win.addstr("=", punctuation_colpair)
788768
self.list_win.addstr(kw, get_colpair(self.config, "token"))
@@ -796,7 +776,7 @@ def mkargspec(self, topline, in_arg, down):
796776
"*%s" % (_args,), get_colpair(self.config, "token")
797777
)
798778

799-
if py3 and kwonly:
779+
if kwonly:
800780
if not _args:
801781
if args:
802782
self.list_win.addstr(", ", punctuation_colpair)
@@ -816,7 +796,7 @@ def mkargspec(self, topline, in_arg, down):
816796
)
817797

818798
if _kwargs:
819-
if args or _args or (py3 and kwonly):
799+
if args or _args or kwonly:
820800
self.list_win.addstr(", ", punctuation_colpair)
821801
self.list_win.addstr(
822802
"**%s" % (_kwargs,), get_colpair(self.config, "token")
@@ -1094,21 +1074,15 @@ def prompt(self, more):
10941074
self.echo(
10951075
"\x01%s\x03%s" % (self.config.color_scheme["prompt"], self.ps1)
10961076
)
1097-
if py3:
1098-
self.stdout_hist += self.ps1
1099-
else:
1100-
self.stdout_hist += self.ps1.encode(getpreferredencoding())
1077+
self.stdout_hist += self.ps1
11011078
self.screen_hist.append(
11021079
"\x01%s\x03%s\x04"
11031080
% (self.config.color_scheme["prompt"], self.ps1)
11041081
)
11051082
else:
11061083
prompt_more_color = self.config.color_scheme["prompt_more"]
11071084
self.echo("\x01%s\x03%s" % (prompt_more_color, self.ps2))
1108-
if py3:
1109-
self.stdout_hist += self.ps2
1110-
else:
1111-
self.stdout_hist += self.ps2.encode(getpreferredencoding())
1085+
self.stdout_hist += self.ps2
11121086
self.screen_hist.append(
11131087
"\x01%s\x03%s\x04" % (prompt_more_color, self.ps2)
11141088
)
@@ -1175,10 +1149,7 @@ def repl(self):
11751149

11761150
self.history.append(inp)
11771151
self.screen_hist[-1] += self.f_string
1178-
if py3:
1179-
self.stdout_hist += inp + "\n"
1180-
else:
1181-
self.stdout_hist += inp.encode(getpreferredencoding()) + "\n"
1152+
self.stdout_hist += inp + "\n"
11821153
stdout_position = len(self.stdout_hist)
11831154
self.more = self.push(inp)
11841155
if not self.more:
@@ -1241,10 +1212,7 @@ def reevaluate(self):
12411212

12421213
self.iy, self.ix = self.scr.getyx()
12431214
for line in self.history:
1244-
if py3:
1245-
self.stdout_hist += line + "\n"
1246-
else:
1247-
self.stdout_hist += line.encode(getpreferredencoding()) + "\n"
1215+
self.stdout_hist += line + "\n"
12481216
self.print_line(line)
12491217
self.screen_hist[-1] += self.f_string
12501218
# I decided it was easier to just do this manually
@@ -1278,9 +1246,6 @@ def write(self, s):
12781246
else:
12791247
t = s
12801248

1281-
if not py3 and isinstance(t, unicode):
1282-
t = t.encode(getpreferredencoding())
1283-
12841249
if not self.stdout_hist:
12851250
self.stdout_hist = t
12861251
else:
@@ -1390,25 +1355,19 @@ def lsize():
13901355
if v_items:
13911356
self.list_win.addstr("\n ")
13921357

1393-
if not py3:
1394-
encoding = getpreferredencoding()
13951358
for ix, i in enumerate(v_items):
13961359
padding = (wl - len(i)) * " "
13971360
if i == current_item:
13981361
color = get_colpair(self.config, "operator")
13991362
else:
14001363
color = get_colpair(self.config, "main")
1401-
if not py3:
1402-
i = i.encode(encoding)
14031364
self.list_win.addstr(i + padding, color)
14041365
if (cols == 1 or (ix and not (ix + 1) % cols)) and ix + 1 < len(
14051366
v_items
14061367
):
14071368
self.list_win.addstr("\n ")
14081369

14091370
if self.docstring is not None:
1410-
if not py3 and isinstance(docstring_string, unicode):
1411-
docstring_string = docstring_string.encode(encoding, "ignore")
14121371
self.list_win.addstr(
14131372
"\n" + docstring_string, get_colpair(self.config, "comment")
14141373
)
@@ -1544,10 +1503,7 @@ def send_current_line_to_editor(self):
15441503
self.iy, self.ix = self.scr.getyx()
15451504
self.evaluating = True
15461505
for line in lines:
1547-
if py3:
1548-
self.stdout_hist += line + "\n"
1549-
else:
1550-
self.stdout_hist += line.encode(getpreferredencoding()) + "\n"
1506+
self.stdout_hist += line + "\n"
15511507
self.history.append(line)
15521508
self.print_line(line)
15531509
self.screen_hist[-1] += self.f_string
@@ -1705,9 +1661,6 @@ def settext(self, s, c=None, p=False):
17051661
self.c = c
17061662

17071663
if s:
1708-
if not py3 and isinstance(s, unicode):
1709-
s = s.encode(getpreferredencoding())
1710-
17111664
if self.c:
17121665
self.win.addstr(s, self.c)
17131666
else:
@@ -1996,15 +1949,6 @@ def main_curses(scr, args, config, interactive=True, locals_=None, banner=None):
19961949
)
19971950
clirepl.write("\n")
19981951

1999-
if not py3:
2000-
# XXX these deprecation warnings need to go at some point
2001-
clirepl.write(
2002-
_(
2003-
"WARNING: You are using `bpython` on Python 2. Support for Python 2 has been deprecated in version 0.19 and might disappear in a future version."
2004-
)
2005-
)
2006-
clirepl.write("\n")
2007-
20081952
exit_value = clirepl.repl()
20091953
if hasattr(sys, "exitfunc"):
20101954
sys.exitfunc()

bpython/curtsies.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
from .curtsiesfrontend import events as bpythonevents
2020
from . import inspection
2121
from .repl import extract_exit_value
22-
from ._py3compat import py3
2322

2423
logger = logging.getLogger(__name__)
2524

@@ -199,14 +198,6 @@ def main(args=None, locals_=None, banner=None, welcome_message=None):
199198
if banner is not None:
200199
print(banner)
201200

202-
if not py3:
203-
# XXX these deprecation warnings need to go at some point
204-
print(
205-
_(
206-
"WARNING: You are using `bpython` on Python 2. Support for Python 2 has been deprecated in version 0.19 and might disappear in a future version."
207-
)
208-
)
209-
210201
global repl
211202
repl = FullCurtsiesRepl(config, locals_, welcome_message, interp)
212203
try:

0 commit comments

Comments
 (0)