bpo-43908: Mark ssl, hash, and hmac types as immutable (GH-25792)
Signed-off-by: Christian Heimes <christian@python.org>
diff --git a/Lib/test/test_hashlib.py b/Lib/test/test_hashlib.py
index a515d3a..ad2ed69 100644
--- a/Lib/test/test_hashlib.py
+++ b/Lib/test/test_hashlib.py
@@ -926,6 +926,15 @@ def test_hash_disallow_instanciation(self):
):
HASHXOF()
+ def test_readonly_types(self):
+ for algorithm, constructors in self.constructors_to_test.items():
+ # all other types have DISALLOW_INSTANTIATION
+ for constructor in constructors:
+ hash_type = type(constructor())
+ with self.subTest(hash_type=hash_type):
+ with self.assertRaisesRegex(TypeError, "immutable type"):
+ hash_type.value = False
+
class KDFTests(unittest.TestCase):
diff --git a/Lib/test/test_hmac.py b/Lib/test/test_hmac.py
index 22d74e9..964acd0 100644
--- a/Lib/test/test_hmac.py
+++ b/Lib/test/test_hmac.py
@@ -444,6 +444,9 @@ def test_internal_types(self):
):
C_HMAC()
+ with self.assertRaisesRegex(TypeError, "immutable type"):
+ C_HMAC.value = None
+
@unittest.skipUnless(sha256_module is not None, 'need _sha256')
def test_with_sha256_module(self):
h = hmac.HMAC(b"key", b"hash this!", digestmod=sha256_module.sha256)
diff --git a/Lib/test/test_ssl.py b/Lib/test/test_ssl.py
index f2b26c4..acb64f1 100644
--- a/Lib/test/test_ssl.py
+++ b/Lib/test/test_ssl.py
@@ -345,6 +345,25 @@ def test_constants(self):
ssl.OP_NO_TLSv1_2
self.assertEqual(ssl.PROTOCOL_TLS, ssl.PROTOCOL_SSLv23)
+ def test_ssl_types(self):
+ ssl_types = [
+ _ssl._SSLContext,
+ _ssl._SSLSocket,
+ _ssl.MemoryBIO,
+ _ssl.Certificate,
+ _ssl.SSLSession,
+ _ssl.SSLError,
+ ]
+ for ssl_type in ssl_types:
+ with self.subTest(ssl_type=ssl_type):
+ with self.assertRaisesRegex(TypeError, "immutable type"):
+ ssl_type.value = None
+ with self.assertRaisesRegex(
+ TypeError,
+ "cannot create '_ssl.Certificate' instances"
+ ):
+ _ssl.Certificate()
+
def test_private_init(self):
with self.assertRaisesRegex(TypeError, "public constructor"):
with socket.socket() as s: