Python 3.9: use encodebytes / decodebytes

The methodes base64.encodestring and base64.decodebytes have been
marked as obsolete aliases for a long time, and in Python 3.9, they
are simply gone. WSME must switch to the encodebytes / decodebytes
to keep working in Python 3.9, and that's what this patch does.

Change-Id: Id6b70cffa2010ce84af0754342e39e1c82fc5d4d
This commit is contained in:
Thomas Goirand 2020-10-14 22:03:18 +02:00
parent a274dcada0
commit a3ce4a1542
4 changed files with 32 additions and 8 deletions

View File

@ -35,7 +35,10 @@ def prepare_value(value, datatype):
if datatype == decimal.Decimal:
return str(value)
if datatype == wsme.types.binary:
return base64.encodestring(value).decode('ascii')
if six.PY3:
return base64.encodebytes(value).decode('ascii')
else:
return base64.encodestring(value).decode('ascii')
if datatype == wsme.types.bytes:
return value.decode('ascii')
return value
@ -46,7 +49,10 @@ def prepare_result(value, datatype):
if value is None:
return None
if datatype == wsme.types.binary:
return base64.decodestring(value.encode('ascii'))
if six.PY3:
return base64.decodebytes(value.encode('ascii'))
else:
return base64.decodestring(value.encode('ascii'))
if isusertype(datatype):
datatype = datatype.basetype
if isinstance(datatype, list):

View File

@ -30,7 +30,10 @@ def dumpxml(key, obj, datatype=None):
node.append(dumpxml('key', item[0], key_type))
node.append(dumpxml('value', item[1], value_type))
elif datatype == wsme.types.binary:
el.text = base64.encodestring(obj).decode('ascii')
if six.PY3:
el.text = base64.encodebytes(obj).decode('ascii')
else:
el.text = base64.encodestring(obj).decode('ascii')
elif isinstance(obj, wsme.types.bytes):
el.text = obj.decode('ascii')
elif isinstance(obj, wsme.types.text):
@ -96,7 +99,10 @@ def loadxml(el, datatype):
return d
else:
if datatype == wsme.types.binary:
return base64.decodestring(el.text.encode('ascii'))
if six.PY3:
return base64.decodebytes(el.text.encode('ascii'))
else:
return base64.decodestring(el.text.encode('ascii'))
if isusertype(datatype):
datatype = datatype.basetype
if datatype == datetime.date:

View File

@ -464,13 +464,19 @@ Value: 'v3'. Value should be one of: v., v.",
def test_binary_to_base(self):
import base64
assert types.binary.tobasetype(None) is None
expected = base64.encodestring(six.b('abcdef'))
if six.PY3:
expected = base64.encodebytes(six.b('abcdef'))
else:
expected = base64.encodestring(six.b('abcdef'))
assert types.binary.tobasetype(six.b('abcdef')) == expected
def test_binary_from_base(self):
import base64
assert types.binary.frombasetype(None) is None
encoded = base64.encodestring(six.b('abcdef'))
if six.PY3:
encoded = base64.encodebytes(six.b('abcdef'))
else:
encoded = base64.encodestring(six.b('abcdef'))
assert types.binary.frombasetype(encoded) == six.b('abcdef')
def test_wsattr_weakref_datatype(self):

View File

@ -128,12 +128,18 @@ class BinaryType(UserType):
def tobasetype(self, value):
if value is None:
return None
return base64.encodestring(value)
if six.PY3:
return base64.encodebytes(value)
else:
return base64.encodestring(value)
def frombasetype(self, value):
if value is None:
return None
return base64.decodestring(value)
if six.PY3:
return base64.decodebytes(value)
else:
return base64.decodestring(value)
#: The binary almost-native type