Skip to content

Commit de073c6

Browse files
authored
Remove native, it's behavior is confusing (#1069)
Instead just decode stuff at the call-sites -- 100% of which were passing bytes
1 parent c175f15 commit de073c6

File tree

3 files changed

+13
-34
lines changed

3 files changed

+13
-34
lines changed

src/OpenSSL/SSL.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
ffi as _ffi,
1313
lib as _lib,
1414
make_assert as _make_assert,
15-
native as _native,
1615
path_string as _path_string,
1716
text_to_bytes_and_warn as _text_to_bytes_and_warn,
1817
no_zero_allocator as _no_zero_allocator,
@@ -2118,7 +2117,7 @@ def get_cipher_list(self):
21182117
result = _lib.SSL_get_cipher_list(self._ssl, i)
21192118
if result == _ffi.NULL:
21202119
break
2121-
ciphers.append(_native(_ffi.string(result)))
2120+
ciphers.append(_ffi.string(result).decode("utf-8"))
21222121
return ciphers
21232122

21242123
def get_client_ca_list(self):

src/OpenSSL/_util.py

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def text(charp):
2626
"""
2727
if not charp:
2828
return ""
29-
return native(ffi.string(charp))
29+
return ffi.string(charp).decode("utf-8")
3030

3131

3232
def exception_from_error_queue(exception_type):
@@ -71,23 +71,6 @@ def openssl_assert(ok):
7171
return openssl_assert
7272

7373

74-
def native(s):
75-
"""
76-
Convert :py:class:`bytes` or :py:class:`unicode` to the native
77-
:py:class:`str` type, using UTF-8 encoding if conversion is necessary.
78-
79-
:raise UnicodeError: The input string is not UTF-8 decodeable.
80-
81-
:raise TypeError: The input is neither :py:class:`bytes` nor
82-
:py:class:`unicode`.
83-
"""
84-
if not isinstance(s, (bytes, str)):
85-
raise TypeError("%r is neither bytes nor unicode" % s)
86-
if isinstance(s, bytes):
87-
return s.decode("utf-8")
88-
return s
89-
90-
9174
def path_string(s):
9275
"""
9376
Convert a Python path to a :py:class:`bytes` string identifying the same

src/OpenSSL/crypto.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
lib as _lib,
1414
exception_from_error_queue as _exception_from_error_queue,
1515
byte_string as _byte_string,
16-
native as _native,
1716
path_string as _path_string,
1817
UNSPECIFIED as _UNSPECIFIED,
1918
text_to_bytes_and_warn as _text_to_bytes_and_warn,
@@ -672,7 +671,7 @@ def __repr__(self):
672671
_openssl_assert(format_result != _ffi.NULL)
673672

674673
return "<X509Name object '%s'>" % (
675-
_native(_ffi.string(result_buffer)),
674+
_ffi.string(result_buffer).decode("utf-8"),
676675
)
677676

678677
def hash(self):
@@ -821,11 +820,11 @@ def _subjectAltNameString(self):
821820
except KeyError:
822821
bio = _new_mem_buf()
823822
_lib.GENERAL_NAME_print(bio, name)
824-
parts.append(_native(_bio_to_string(bio)))
823+
parts.append(_bio_to_string(bio).decode("utf-8"))
825824
else:
826-
value = _native(
827-
_ffi.buffer(name.d.ia5.data, name.d.ia5.length)[:]
828-
)
825+
value = _ffi.buffer(name.d.ia5.data, name.d.ia5.length)[
826+
:
827+
].decode("utf-8")
829828
parts.append(label + ":" + value)
830829
return ", ".join(parts)
831830

@@ -840,7 +839,7 @@ def __str__(self):
840839
print_result = _lib.X509V3_EXT_print(bio, self._extension, 0, 0)
841840
_openssl_assert(print_result != 0)
842841

843-
return _native(_bio_to_string(bio))
842+
return _bio_to_string(bio).decode("utf-8")
844843

845844
def get_critical(self):
846845
"""
@@ -1381,7 +1380,7 @@ def has_expired(self):
13811380
:return: ``True`` if the certificate has expired, ``False`` otherwise.
13821381
:rtype: bool
13831382
"""
1384-
time_string = _native(self.get_notAfter())
1383+
time_string = self.get_notAfter().decode("utf-8")
13851384
not_after = datetime.datetime.strptime(time_string, "%Y%m%d%H%M%SZ")
13861385

13871386
return not_after < datetime.datetime.utcnow()
@@ -1850,13 +1849,11 @@ def _exception_from_context(self):
18501849
errors = [
18511850
_lib.X509_STORE_CTX_get_error(self._store_ctx),
18521851
_lib.X509_STORE_CTX_get_error_depth(self._store_ctx),
1853-
_native(
1854-
_ffi.string(
1855-
_lib.X509_verify_cert_error_string(
1856-
_lib.X509_STORE_CTX_get_error(self._store_ctx)
1857-
)
1852+
_ffi.string(
1853+
_lib.X509_verify_cert_error_string(
1854+
_lib.X509_STORE_CTX_get_error(self._store_ctx)
18581855
)
1859-
),
1856+
).decode("utf-8"),
18601857
]
18611858
# A context error should always be associated with a certificate, so we
18621859
# expect this call to never return :class:`None`.

0 commit comments

Comments
 (0)