1
1
import os
2
2
import sys
3
3
import warnings
4
+ from typing import Any , Callable , NoReturn , Type , Union
4
5
5
6
from cryptography .hazmat .bindings .openssl .binding import Binding
6
7
8
+ StrOrBytesPath = Union [str , bytes , os .PathLike ]
7
9
8
10
binding = Binding ()
9
11
ffi = binding .ffi
16
18
no_zero_allocator = ffi .new_allocator (should_clear_after_alloc = False )
17
19
18
20
19
- def text (charp ) :
21
+ def text (charp : Any ) -> str :
20
22
"""
21
23
Get a native string type representing of the given CFFI ``char*`` object.
22
24
@@ -29,7 +31,7 @@ def text(charp):
29
31
return ffi .string (charp ).decode ("utf-8" )
30
32
31
33
32
- def exception_from_error_queue (exception_type ) :
34
+ def exception_from_error_queue (exception_type : Type [ Exception ]) -> NoReturn :
33
35
"""
34
36
Convert an OpenSSL library failure into a Python exception.
35
37
@@ -55,13 +57,13 @@ def exception_from_error_queue(exception_type):
55
57
raise exception_type (errors )
56
58
57
59
58
- def make_assert (error ) :
60
+ def make_assert (error : Type [ Exception ]) -> Callable [[ bool ], Any ] :
59
61
"""
60
62
Create an assert function that uses :func:`exception_from_error_queue` to
61
63
raise an exception wrapped by *error*.
62
64
"""
63
65
64
- def openssl_assert (ok ) :
66
+ def openssl_assert (ok : bool ) -> None :
65
67
"""
66
68
If *ok* is not True, retrieve the error from OpenSSL and raise it.
67
69
"""
@@ -71,7 +73,7 @@ def openssl_assert(ok):
71
73
return openssl_assert
72
74
73
75
74
- def path_bytes (s ) :
76
+ def path_bytes (s : StrOrBytesPath ) -> bytes :
75
77
"""
76
78
Convert a Python path to a :py:class:`bytes` for the path which can be
77
79
passed into an OpenSSL API accepting a filename.
@@ -88,7 +90,7 @@ def path_bytes(s):
88
90
return b
89
91
90
92
91
- def byte_string (s ) :
93
+ def byte_string (s : str ) -> bytes :
92
94
return s .encode ("charmap" )
93
95
94
96
@@ -99,7 +101,7 @@ def byte_string(s):
99
101
_TEXT_WARNING = "str for {0} is no longer accepted, use bytes"
100
102
101
103
102
- def text_to_bytes_and_warn (label , obj ) :
104
+ def text_to_bytes_and_warn (label : str , obj : Any ) -> Any :
103
105
"""
104
106
If ``obj`` is text, emit a warning that it should be bytes instead and try
105
107
to convert it to bytes automatically.
0 commit comments