_compat: Change the to_native_str into a to_unicode_str function.

The to_native_str function was supposed to produce Unicode decoded
native strings for python2 and python3.

Upon further consideration this doesn't make much sense since under
python2 it would need to decode the bytes into a Unicode string and turn
it back into a python2 str.

The ability to use the replacement character requires us to use a
Unicode string under python2 as well.
This commit is contained in:
Damir Jelić 2019-06-19 14:45:20 +02:00
parent ba65551d5f
commit 5e24c605d2
4 changed files with 12 additions and 12 deletions

View file

@ -48,8 +48,11 @@ def to_bytes(string):
raise TypeError("Invalid type {}".format(type(string)))
def to_native_str(byte_string, errors="replace"):
"""Turn a byte string into a native string decoding it as UTF-8.
def to_unicode_str(byte_string, errors="replace"):
"""Turn a byte string into a unicode string.
Should be used everywhere where the input byte string might not be trusted
and may contain invalid unicode values.
Args:
byte_string (bytes): The bytestring that will be converted to a native
@ -63,7 +66,4 @@ def to_native_str(byte_string, errors="replace"):
Returns the decoded native string.
"""
try:
return native_str(byte_string, errors=errors)
except TypeError:
return bytes(byte_string).decode(errors=errors)
return byte_string.decode(errors=errors)

View file

@ -33,7 +33,7 @@ from future.utils import bytes_to_native_str
# pylint: disable=no-name-in-module
from _libolm import ffi, lib # type: ignore
from ._compat import URANDOM, to_bytearray, to_bytes, to_native_str
from ._compat import URANDOM, to_bytearray, to_bytes, to_unicode_str
from ._finalize import track_for_finalization
@ -230,7 +230,7 @@ class InboundGroupSession(object):
self._check_error(plaintext_length)
plaintext = to_native_str(
plaintext = to_unicode_str(
ffi.unpack(plaintext_buffer, plaintext_length),
errors=errors
)

View file

@ -40,7 +40,7 @@ from future.utils import bytes_to_native_str
from _libolm import ffi, lib # type: ignore
from ._compat import URANDOM, to_bytearray, to_native_str
from ._compat import URANDOM, to_bytearray, to_unicode_str
from ._finalize import track_for_finalization
@ -361,7 +361,7 @@ class PkDecryption(object):
# clear out copies of the plaintext
lib.memset(plaintext_buffer, 0, max_plaintext_length)
return to_native_str(plaintext, errors=errors)
return to_unicode_str(plaintext, errors=errors)
def _clear_pk_signing(pk_struct):

View file

@ -40,7 +40,7 @@ from future.utils import bytes_to_native_str
# pylint: disable=no-name-in-module
from _libolm import ffi, lib # type: ignore
from ._compat import URANDOM, to_bytearray, to_bytes, to_native_str
from ._compat import URANDOM, to_bytearray, to_bytes, to_unicode_str
from ._finalize import track_for_finalization
# This is imported only for type checking purposes
@ -318,7 +318,7 @@ class Session(object):
plaintext_buffer, max_plaintext_length
)
self._check_error(plaintext_length)
plaintext = to_native_str(
plaintext = to_unicode_str(
ffi.unpack(plaintext_buffer, plaintext_length),
errors=errors
)