Fix uses of retired builtins and builtin methods

In Python 3.x

- int and long types are unified. The unified type is called int.
- the text string type (unicode) is renamed to str.
- the byte string type (str) is renamed to bytes.
- chr returns a text string (i.e. str)
- xrange is renamed to range.
- dict.has_key() is removed
-
This commit is contained in:
Alex Willmer 2019-05-10 21:28:35 +01:00
parent 0f3cadcc3e
commit 287f71d561
4 changed files with 33 additions and 20 deletions

View file

@ -25,6 +25,20 @@
}
%pythoncode %{
try:
INTEGER_TYPES = (int, long)
except NameError:
INTEGER_TYPES = (int,)
try:
TEXT_TYPE = unicode
def bchr(i):
return chr(i)
except NameError:
TEXT_TYPE = str
def bchr(i):
return bytes([i])
class Placeholder(object):
"""The python equivalent of TT_NONE"""
def __str__(self):
@ -250,36 +264,35 @@
}
%pythoncode %{
def action(p, act):
return _h_action(p, act)
def attr_bool(p, pred):
return _h_attr_bool(p, pred)
def ch(ch):
if isinstance(ch, str) or isinstance(ch, unicode):
if isinstance(ch, (bytes, TEXT_TYPE)):
return token(ch)
else:
return _h_ch(ch)
def ch_range(c1, c2):
dostr = isinstance(c1, str)
dostr2 = isinstance(c2, str)
if isinstance(c1, unicode) or isinstance(c2, unicode):
dostr = isinstance(c1, bytes)
dostr2 = isinstance(c2, bytes)
if isinstance(c1, TEXT_TYPE) or isinstance(c2, TEXT_TYPE):
raise TypeError("ch_range only works on bytes")
if dostr != dostr2:
raise TypeError("Both arguments to ch_range must be the same type")
if dostr:
return action(_h_ch_range(c1, c2), chr)
return action(_h_ch_range(c1, c2), bchr)
else:
return _h_ch_range(c1, c2)
def epsilon_p(): return _h_epsilon_p()
def end_p():
return _h_end_p()
def in_(charset):
return action(_h_in(charset), chr)
return action(_h_in(charset), bchr)
def not_in(charset):
return action(_h_not_in(charset), chr)
return action(_h_not_in(charset), bchr)
def not_(p): return _h_not(p)
def int_range(p, i1, i2):
return _h_int_range(p, i1, i2)