Remove native text converter (#38)

* Remove `native text` converter

This was only necessary with Python 2. After Python 2 removal, both
functions `to_ntext` and `to_utext` exactly did the same.

modified:   ssh-audit.py
modified:   test/test_utils.py

* Rename `to_utext` to `to_text`

... as in Python 3 there is only text (and bytes).

modified:   ssh-audit.py
modified:   test/test_utils.py
This commit is contained in:
Jürgen Gmach 2020-06-17 04:50:07 +02:00 committed by GitHub
parent ec1dda8d7f
commit 12f811cb5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 26 deletions

View File

@ -1051,7 +1051,7 @@ class SSH1:
ciphers = [] ciphers = []
for i in range(len(SSH1.CIPHERS)): for i in range(len(SSH1.CIPHERS)):
if self.__supported_ciphers_mask & (1 << i) != 0: if self.__supported_ciphers_mask & (1 << i) != 0:
ciphers.append(utils.to_utext(SSH1.CIPHERS[i])) ciphers.append(utils.to_text(SSH1.CIPHERS[i]))
return ciphers return ciphers
@property @property
@ -1065,7 +1065,7 @@ class SSH1:
auths = [] auths = []
for i in range(1, len(SSH1.AUTHS)): for i in range(1, len(SSH1.AUTHS)):
if self.__supported_authentications_mask & (1 << i) != 0: if self.__supported_authentications_mask & (1 << i) != 0:
auths.append(utils.to_utext(SSH1.AUTHS[i])) auths.append(utils.to_text(SSH1.AUTHS[i]))
return auths return auths
def write(self, wbuf): def write(self, wbuf):
@ -1753,7 +1753,7 @@ class SSH: # pylint: disable=too-few-public-methods
alg_db = alg_pair.db alg_db = alg_pair.db
for alg_type, alg_list in alg_pair.items(): for alg_type, alg_list in alg_pair.items():
for alg_name in alg_list: for alg_name in alg_list:
alg_name_native = utils.to_ntext(alg_name) alg_name_native = utils.to_text(alg_name)
alg_desc = alg_db[alg_type].get(alg_name_native) alg_desc = alg_db[alg_type].get(alg_name_native)
if alg_desc is None: if alg_desc is None:
continue continue
@ -2686,7 +2686,7 @@ def output_algorithm(alg_db, alg_type, alg_name, unknown_algs, alg_max_len=0, al
texts = [] texts = []
if len(alg_name.strip()) == 0: if len(alg_name.strip()) == 0:
return return
alg_name_native = utils.to_ntext(alg_name) alg_name_native = utils.to_text(alg_name)
if alg_name_native in alg_db[alg_type]: if alg_name_native in alg_db[alg_type]:
alg_desc = alg_db[alg_type][alg_name_native] alg_desc = alg_db[alg_type][alg_name_native]
ldesc = len(alg_desc) ldesc = len(alg_desc)
@ -3011,7 +3011,7 @@ class Utils:
raise cls._type_err(v, 'bytes') raise cls._type_err(v, 'bytes')
@classmethod @classmethod
def to_utext(cls, v, enc='utf-8'): def to_text(cls, v, enc='utf-8'):
# type: (Union[str, bytes], str) -> str # type: (Union[str, bytes], str) -> str
if isinstance(v, str): if isinstance(v, str):
return v return v
@ -3019,15 +3019,6 @@ class Utils:
return v.decode(enc) return v.decode(enc)
raise cls._type_err(v, 'unicode text') raise cls._type_err(v, 'unicode text')
@classmethod
def to_ntext(cls, v, enc='utf-8'):
# type: (Union[str, bytes], str) -> str
if isinstance(v, str):
return v
elif isinstance(v, bytes):
return v.decode(enc)
raise cls._type_err(v, 'native text')
@classmethod @classmethod
def _is_ascii(cls, v, char_filter=lambda x: x <= 127): def _is_ascii(cls, v, char_filter=lambda x: x <= 127):
# type: (str, Callable[[int], bool]) -> bool # type: (str, Callable[[int], bool]) -> bool
@ -3053,7 +3044,7 @@ class Utils:
if errors == 'ignore': if errors == 'ignore':
continue continue
r.append(63) r.append(63)
return cls.to_ntext(r.decode('ascii')) return cls.to_text(r.decode('ascii'))
raise cls._type_err(v, 'ascii') raise cls._type_err(v, 'ascii')
@classmethod @classmethod

View File

@ -14,19 +14,12 @@ class TestUtils:
with pytest.raises(TypeError): with pytest.raises(TypeError):
self.utils.to_bytes(123) self.utils.to_bytes(123)
def test_to_utext(self): def test_to_text(self):
assert self.utils.to_utext(b'fran\xc3\xa7ais') == 'fran\xe7ais' assert self.utils.to_text(b'fran\xc3\xa7ais') == 'fran\xe7ais'
assert self.utils.to_utext('fran\xe7ais') == 'fran\xe7ais' assert self.utils.to_text('fran\xe7ais') == 'fran\xe7ais'
# other # other
with pytest.raises(TypeError): with pytest.raises(TypeError):
self.utils.to_utext(123) self.utils.to_text(123)
def test_to_ntext(self):
assert self.utils.to_ntext('fran\xc3\xa7ais') == 'fran\xc3\xa7ais'
assert self.utils.to_ntext(b'fran\xc3\xa7ais') == 'fran\xe7ais'
# other
with pytest.raises(TypeError):
self.utils.to_ntext(123)
def test_is_ascii(self): def test_is_ascii(self):
assert self.utils.is_ascii('francais') is True assert self.utils.is_ascii('francais') is True