codekingpro/portable-devtools
115k
1from __future__ import annotations2 3import os4import sys5import warnings6from typing import Any, Callable, NoReturn, Union7 8from cryptography.hazmat.bindings.openssl.binding import Binding9 10if sys.version_info >= (3, 9):11 StrOrBytesPath = Union[str, bytes, os.PathLike[str], os.PathLike[bytes]]12else:13 StrOrBytesPath = Union[str, bytes, os.PathLike]14 15binding = Binding()16ffi = binding.ffi17lib: Any = binding.lib18 19 20# This is a special CFFI allocator that does not bother to zero its memory21# after allocation. This has vastly better performance on large allocations and22# so should be used whenever we don't need the memory zeroed out.23no_zero_allocator = ffi.new_allocator(should_clear_after_alloc=False)24 25 26def text(charp: Any) -> str:27 """28 Get a native string type representing of the given CFFI ``char*`` object.29 30 :param charp: A C-style string represented using CFFI.31 32 :return: :class:`str`33 """34 if not charp:35 return ""36 return ffi.string(charp).decode("utf-8")37 38 39def exception_from_error_queue(exception_type: type[Exception]) -> NoReturn:40 """41 Convert an OpenSSL library failure into a Python exception.42 43 When a call to the native OpenSSL library fails, this is usually signalled44 by the return value, and an error code is stored in an error queue45 associated with the current thread. The err library provides functions to46 obtain these error codes and textual error messages.47 """48 errors = []49 50 while True:51 error = lib.ERR_get_error()52 if error == 0:53 break54 errors.append(55 (56 text(lib.ERR_lib_error_string(error)),57 text(lib.ERR_func_error_string(error)),58 text(lib.ERR_reason_error_string(error)),59 )60 )61 62 raise exception_type(errors)63 64 65def make_assert(error: type[Exception]) -> Callable[[bool], Any]:66 """67 Create an assert function that uses :func:`exception_from_error_queue` to68 raise an exception wrapped by *error*.69 """70 71 def openssl_assert(ok: bool) -> None:72 """73 If *ok* is not True, retrieve the error from OpenSSL and raise it.74 """75 if ok is not True:76 exception_from_error_queue(error)77 78 return openssl_assert79 80 81def path_bytes(s: StrOrBytesPath) -> bytes:82 """83 Convert a Python path to a :py:class:`bytes` for the path which can be84 passed into an OpenSSL API accepting a filename.85 86 :param s: A path (valid for os.fspath).87 88 :return: An instance of :py:class:`bytes`.89 """90 b = os.fspath(s)91 92 if isinstance(b, str):93 return b.encode(sys.getfilesystemencoding())94 else:95 return b96 97 98def byte_string(s: str) -> bytes:99 return s.encode("charmap")100 101 102# A marker object to observe whether some optional arguments are passed any103# value or not.104UNSPECIFIED = object()105 106_TEXT_WARNING = "str for {0} is no longer accepted, use bytes"107 108 109def text_to_bytes_and_warn(label: str, obj: Any) -> Any:110 """111 If ``obj`` is text, emit a warning that it should be bytes instead and try112 to convert it to bytes automatically.113 114 :param str label: The name of the parameter from which ``obj`` was taken115 (so a developer can easily find the source of the problem and correct116 it).117 118 :return: If ``obj`` is the text string type, a ``bytes`` object giving the119 UTF-8 encoding of that text is returned. Otherwise, ``obj`` itself is120 returned.121 """122 if isinstance(obj, str):123 warnings.warn(124 _TEXT_WARNING.format(label),125 category=DeprecationWarning,126 stacklevel=3,127 )128 return obj.encode("utf-8")129 return obj130 