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:
parent
a274dcada0
commit
a3ce4a1542
@ -35,7 +35,10 @@ def prepare_value(value, datatype):
|
|||||||
if datatype == decimal.Decimal:
|
if datatype == decimal.Decimal:
|
||||||
return str(value)
|
return str(value)
|
||||||
if datatype == wsme.types.binary:
|
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:
|
if datatype == wsme.types.bytes:
|
||||||
return value.decode('ascii')
|
return value.decode('ascii')
|
||||||
return value
|
return value
|
||||||
@ -46,7 +49,10 @@ def prepare_result(value, datatype):
|
|||||||
if value is None:
|
if value is None:
|
||||||
return None
|
return None
|
||||||
if datatype == wsme.types.binary:
|
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):
|
if isusertype(datatype):
|
||||||
datatype = datatype.basetype
|
datatype = datatype.basetype
|
||||||
if isinstance(datatype, list):
|
if isinstance(datatype, list):
|
||||||
|
@ -30,7 +30,10 @@ def dumpxml(key, obj, datatype=None):
|
|||||||
node.append(dumpxml('key', item[0], key_type))
|
node.append(dumpxml('key', item[0], key_type))
|
||||||
node.append(dumpxml('value', item[1], value_type))
|
node.append(dumpxml('value', item[1], value_type))
|
||||||
elif datatype == wsme.types.binary:
|
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):
|
elif isinstance(obj, wsme.types.bytes):
|
||||||
el.text = obj.decode('ascii')
|
el.text = obj.decode('ascii')
|
||||||
elif isinstance(obj, wsme.types.text):
|
elif isinstance(obj, wsme.types.text):
|
||||||
@ -96,7 +99,10 @@ def loadxml(el, datatype):
|
|||||||
return d
|
return d
|
||||||
else:
|
else:
|
||||||
if datatype == wsme.types.binary:
|
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):
|
if isusertype(datatype):
|
||||||
datatype = datatype.basetype
|
datatype = datatype.basetype
|
||||||
if datatype == datetime.date:
|
if datatype == datetime.date:
|
||||||
|
@ -464,13 +464,19 @@ Value: 'v3'. Value should be one of: v., v.",
|
|||||||
def test_binary_to_base(self):
|
def test_binary_to_base(self):
|
||||||
import base64
|
import base64
|
||||||
assert types.binary.tobasetype(None) is None
|
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
|
assert types.binary.tobasetype(six.b('abcdef')) == expected
|
||||||
|
|
||||||
def test_binary_from_base(self):
|
def test_binary_from_base(self):
|
||||||
import base64
|
import base64
|
||||||
assert types.binary.frombasetype(None) is None
|
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')
|
assert types.binary.frombasetype(encoded) == six.b('abcdef')
|
||||||
|
|
||||||
def test_wsattr_weakref_datatype(self):
|
def test_wsattr_weakref_datatype(self):
|
||||||
|
@ -128,12 +128,18 @@ class BinaryType(UserType):
|
|||||||
def tobasetype(self, value):
|
def tobasetype(self, value):
|
||||||
if value is None:
|
if value is None:
|
||||||
return None
|
return None
|
||||||
return base64.encodestring(value)
|
if six.PY3:
|
||||||
|
return base64.encodebytes(value)
|
||||||
|
else:
|
||||||
|
return base64.encodestring(value)
|
||||||
|
|
||||||
def frombasetype(self, value):
|
def frombasetype(self, value):
|
||||||
if value is None:
|
if value is None:
|
||||||
return None
|
return None
|
||||||
return base64.decodestring(value)
|
if six.PY3:
|
||||||
|
return base64.decodebytes(value)
|
||||||
|
else:
|
||||||
|
return base64.decodestring(value)
|
||||||
|
|
||||||
|
|
||||||
#: The binary almost-native type
|
#: The binary almost-native type
|
||||||
|
Loading…
x
Reference in New Issue
Block a user