]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-43916: _md5.md5 uses Py_TPFLAGS_DISALLOW_INSTANTIATION (GH-25753)
authorVictor Stinner <vstinner@python.org>
Fri, 30 Apr 2021 16:40:30 +0000 (18:40 +0200)
committerGitHub <noreply@github.com>
Fri, 30 Apr 2021 16:40:30 +0000 (18:40 +0200)
The following types use Py_TPFLAGS_DISALLOW_INSTANTIATION flag:

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

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

index 1236aa723b19950e4670697b661b7d29f9b30747..c7c128e75568e269f35f40763c77016fff761807 100644 (file)
@@ -901,8 +901,39 @@ class HashLibTestCase(unittest.TestCase):
         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"
index b2e65a0a5ffd227f36038736724c3fbfe5507ede..2ae94a456fdb35fda01bab3554e8994cf66d3076 100644 (file)
@@ -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
 };
 
index 7126db93b1a3fcaa4a8024a64b68576443475f90..9ac46c58a7f348273641cce28c3028bc949ada18 100644 (file)
@@ -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);
index b90e5df7826740690c8971fa1234d14e2c975933..ccb1862a99f198bcae5dce84ab104bbe9b2ee22b 100644 (file)
@@ -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
 };
 
index 0d8f51e5ae5e009b064b6bae076d76f0c13baf78..5e8572caf551847909f71c89ff17cabfddd6d351 100644 (file)
@@ -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
 };