[Python-checkins] bpo-43916: _md5.md5 uses Py_TPFLAGS_DISALLOW_INSTANTIATION (GH-25753)

vstinner webhook-mailer at python.org
Fri Apr 30 12:40:38 EDT 2021


https://github.com/python/cpython/commit/665c7746fca180266b121183c2d5136c547006e0
commit: 665c7746fca180266b121183c2d5136c547006e0
branch: master
author: Victor Stinner <vstinner at python.org>
committer: vstinner <vstinner at python.org>
date: 2021-04-30T18:40:30+02:00
summary:

bpo-43916: _md5.md5 uses Py_TPFLAGS_DISALLOW_INSTANTIATION (GH-25753)

The following types use Py_TPFLAGS_DISALLOW_INSTANTIATION flag:

* _md5.md5
* _sha1.sha1
* _sha256.sha224
* _sha256.sha256
* _sha512.sha384
* _sha512.sha512

files:
M Lib/test/test_hashlib.py
M Modules/md5module.c
M Modules/sha1module.c
M Modules/sha256module.c
M Modules/sha512module.c

diff --git a/Lib/test/test_hashlib.py b/Lib/test/test_hashlib.py
index 1236aa723b199..c7c128e75568e 100644
--- a/Lib/test/test_hashlib.py
+++ b/Lib/test/test_hashlib.py
@@ -901,8 +901,39 @@ def test_get_fips_mode(self):
         if fips_mode is not None:
             self.assertIsInstance(fips_mode, int)
 
+    def test_disallow_instanciation(self):
+        constructors = []
+        try:
+            import _md5
+            constructors.append(_md5.md5)
+        except ImportError:
+            pass
+        try:
+            import _sha1
+            constructors.append(_sha1.sha1)
+        except ImportError:
+            pass
+        try:
+            import _sha256
+            constructors.append(_sha256.sha224)
+            constructors.append(_sha256.sha256)
+        except ImportError:
+            pass
+        try:
+            import _sha512
+            constructors.append(_sha512.sha384)
+            constructors.append(_sha512.sha512)
+        except ImportError:
+            pass
+
+        for constructor in constructors:
+            h = constructor()
+            with self.subTest(constructor=constructor):
+                hash_type = type(h)
+                self.assertRaises(TypeError, hash_type)
+
     @unittest.skipUnless(HASH is not None, 'need _hashlib')
-    def test_internal_types(self):
+    def test_hash_disallow_instanciation(self):
         # internal types like _hashlib.HASH are not constructable
         with self.assertRaisesRegex(
             TypeError, "cannot create '_hashlib.HASH' instance"
diff --git a/Modules/md5module.c b/Modules/md5module.c
index b2e65a0a5ffd2..2ae94a456fdb3 100644
--- a/Modules/md5module.c
+++ b/Modules/md5module.c
@@ -484,7 +484,7 @@ static PyType_Slot md5_type_slots[] = {
 static PyType_Spec md5_type_spec = {
     .name = "_md5.md5",
     .basicsize =  sizeof(MD5object),
-    .flags = Py_TPFLAGS_DEFAULT,
+    .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
     .slots = md5_type_slots
 };
 
diff --git a/Modules/sha1module.c b/Modules/sha1module.c
index 7126db93b1a3f..9ac46c58a7f34 100644
--- a/Modules/sha1module.c
+++ b/Modules/sha1module.c
@@ -462,7 +462,7 @@ static PyType_Slot sha1_type_slots[] = {
 static PyType_Spec sha1_type_spec = {
     .name = "_sha1.sha1",
     .basicsize =  sizeof(SHA1object),
-    .flags = Py_TPFLAGS_DEFAULT,
+    .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
     .slots = sha1_type_slots
 };
 
@@ -554,7 +554,7 @@ _sha1_exec(PyObject *module)
     }
 
     Py_INCREF(st->sha1_type);
-    if (PyModule_AddObject(module, 
+    if (PyModule_AddObject(module,
                            "SHA1Type",
                            (PyObject *)st->sha1_type) < 0) {
         Py_DECREF(st->sha1_type);
diff --git a/Modules/sha256module.c b/Modules/sha256module.c
index b90e5df782674..ccb1862a99f19 100644
--- a/Modules/sha256module.c
+++ b/Modules/sha256module.c
@@ -544,14 +544,14 @@ static PyType_Slot sha256_types_slots[] = {
 static PyType_Spec sha224_type_spec = {
     .name = "_sha256.sha224",
     .basicsize = sizeof(SHAobject),
-    .flags = Py_TPFLAGS_DEFAULT,
+    .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
     .slots = sha256_types_slots
 };
 
 static PyType_Spec sha256_type_spec = {
     .name = "_sha256.sha256",
     .basicsize = sizeof(SHAobject),
-    .flags = Py_TPFLAGS_DEFAULT,
+    .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
     .slots = sha256_types_slots
 };
 
diff --git a/Modules/sha512module.c b/Modules/sha512module.c
index 0d8f51e5ae5e0..5e8572caf5518 100644
--- a/Modules/sha512module.c
+++ b/Modules/sha512module.c
@@ -602,7 +602,7 @@ static PyType_Slot sha512_sha384_type_slots[] = {
 static PyType_Spec sha512_sha384_type_spec = {
     .name = "_sha512.sha384",
     .basicsize =  sizeof(SHAobject),
-    .flags = Py_TPFLAGS_DEFAULT,
+    .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
     .slots = sha512_sha384_type_slots
 };
 
@@ -619,7 +619,7 @@ static PyType_Slot sha512_sha512_type_slots[] = {
 static PyType_Spec sha512_sha512_type_spec = {
     .name = "_sha512.sha512",
     .basicsize =  sizeof(SHAobject),
-    .flags = Py_TPFLAGS_DEFAULT,
+    .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
     .slots = sha512_sha512_type_slots
 };
 



More information about the Python-checkins mailing list