|
80 | 80 | } |
81 | 81 |
|
82 | 82 | def __get_builtin_constructor(name): |
| 83 | + if not isinstance(name, str): |
| 84 | + # Since this function is only used by new(), we use the same |
| 85 | + # exception as _hashlib.new() when 'name' is of incorrect type. |
| 86 | + err = f"new() argument 'name' must be str, not {type(name).__name__}" |
| 87 | + raise TypeError(err) |
83 | 88 | cache = __builtin_constructor_cache |
84 | 89 | constructor = cache.get(name) |
85 | 90 | if constructor is not None: |
@@ -120,10 +125,13 @@ def __get_builtin_constructor(name): |
120 | 125 | if constructor is not None: |
121 | 126 | return constructor |
122 | 127 |
|
123 | | - raise ValueError('unsupported hash type ' + name) |
| 128 | + # Keep the message in sync with hashlib.h::HASHLIB_UNSUPPORTED_ALGORITHM. |
| 129 | + raise ValueError(f'unsupported hash algorithm {name}') |
124 | 130 |
|
125 | 131 |
|
126 | 132 | def __get_openssl_constructor(name): |
| 133 | + # This function is only used until the module has been initialized. |
| 134 | + assert isinstance(name, str), "invalid call to __get_openssl_constructor()" |
127 | 135 | if name in __block_openssl_constructor: |
128 | 136 | # Prefer our builtin blake2 implementation. |
129 | 137 | return __get_builtin_constructor(name) |
@@ -154,6 +162,8 @@ def __hash_new(name, *args, **kwargs): |
154 | 162 | optionally initialized with data (which must be a bytes-like object). |
155 | 163 | """ |
156 | 164 | if name in __block_openssl_constructor: |
| 165 | + # __block_openssl_constructor is expected to contain strings only |
| 166 | + assert isinstance(name, str), f"unexpected name: {name}" |
157 | 167 | # Prefer our builtin blake2 implementation. |
158 | 168 | return __get_builtin_constructor(name)(*args, **kwargs) |
159 | 169 | try: |
|
0 commit comments