Skip to content

Commit

Permalink
Remove native, it's behavior is confusing (#1069)
Browse files Browse the repository at this point in the history
Instead just decode stuff at the call-sites -- 100% of which were passing bytes
  • Loading branch information
alex authored Dec 19, 2021
1 parent c175f15 commit de073c6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 34 deletions.
3 changes: 1 addition & 2 deletions src/OpenSSL/SSL.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
ffi as _ffi,
lib as _lib,
make_assert as _make_assert,
native as _native,
path_string as _path_string,
text_to_bytes_and_warn as _text_to_bytes_and_warn,
no_zero_allocator as _no_zero_allocator,
Expand Down Expand Up @@ -2118,7 +2117,7 @@ def get_cipher_list(self):
result = _lib.SSL_get_cipher_list(self._ssl, i)
if result == _ffi.NULL:
break
ciphers.append(_native(_ffi.string(result)))
ciphers.append(_ffi.string(result).decode("utf-8"))
return ciphers

def get_client_ca_list(self):
Expand Down
19 changes: 1 addition & 18 deletions src/OpenSSL/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def text(charp):
"""
if not charp:
return ""
return native(ffi.string(charp))
return ffi.string(charp).decode("utf-8")


def exception_from_error_queue(exception_type):
Expand Down Expand Up @@ -71,23 +71,6 @@ def openssl_assert(ok):
return openssl_assert


def native(s):
"""
Convert :py:class:`bytes` or :py:class:`unicode` to the native
:py:class:`str` type, using UTF-8 encoding if conversion is necessary.
:raise UnicodeError: The input string is not UTF-8 decodeable.
:raise TypeError: The input is neither :py:class:`bytes` nor
:py:class:`unicode`.
"""
if not isinstance(s, (bytes, str)):
raise TypeError("%r is neither bytes nor unicode" % s)
if isinstance(s, bytes):
return s.decode("utf-8")
return s


def path_string(s):
"""
Convert a Python path to a :py:class:`bytes` string identifying the same
Expand Down
25 changes: 11 additions & 14 deletions src/OpenSSL/crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
lib as _lib,
exception_from_error_queue as _exception_from_error_queue,
byte_string as _byte_string,
native as _native,
path_string as _path_string,
UNSPECIFIED as _UNSPECIFIED,
text_to_bytes_and_warn as _text_to_bytes_and_warn,
Expand Down Expand Up @@ -672,7 +671,7 @@ def __repr__(self):
_openssl_assert(format_result != _ffi.NULL)

return "<X509Name object '%s'>" % (
_native(_ffi.string(result_buffer)),
_ffi.string(result_buffer).decode("utf-8"),
)

def hash(self):
Expand Down Expand Up @@ -821,11 +820,11 @@ def _subjectAltNameString(self):
except KeyError:
bio = _new_mem_buf()
_lib.GENERAL_NAME_print(bio, name)
parts.append(_native(_bio_to_string(bio)))
parts.append(_bio_to_string(bio).decode("utf-8"))
else:
value = _native(
_ffi.buffer(name.d.ia5.data, name.d.ia5.length)[:]
)
value = _ffi.buffer(name.d.ia5.data, name.d.ia5.length)[
:
].decode("utf-8")
parts.append(label + ":" + value)
return ", ".join(parts)

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

return _native(_bio_to_string(bio))
return _bio_to_string(bio).decode("utf-8")

def get_critical(self):
"""
Expand Down Expand Up @@ -1381,7 +1380,7 @@ def has_expired(self):
:return: ``True`` if the certificate has expired, ``False`` otherwise.
:rtype: bool
"""
time_string = _native(self.get_notAfter())
time_string = self.get_notAfter().decode("utf-8")
not_after = datetime.datetime.strptime(time_string, "%Y%m%d%H%M%SZ")

return not_after < datetime.datetime.utcnow()
Expand Down Expand Up @@ -1850,13 +1849,11 @@ def _exception_from_context(self):
errors = [
_lib.X509_STORE_CTX_get_error(self._store_ctx),
_lib.X509_STORE_CTX_get_error_depth(self._store_ctx),
_native(
_ffi.string(
_lib.X509_verify_cert_error_string(
_lib.X509_STORE_CTX_get_error(self._store_ctx)
)
_ffi.string(
_lib.X509_verify_cert_error_string(
_lib.X509_STORE_CTX_get_error(self._store_ctx)
)
),
).decode("utf-8"),
]
# A context error should always be associated with a certificate, so we
# expect this call to never return :class:`None`.
Expand Down

0 comments on commit de073c6

Please sign in to comment.