Skip to content

Commit 665c774

Browse files
authored
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
1 parent e374a40 commit 665c774

File tree

5 files changed

+39
-8
lines changed

5 files changed

+39
-8
lines changed

Lib/test/test_hashlib.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -901,8 +901,39 @@ def test_get_fips_mode(self):
901901
if fips_mode is not None:
902902
self.assertIsInstance(fips_mode, int)
903903

904+
def test_disallow_instanciation(self):
905+
constructors = []
906+
try:
907+
import _md5
908+
constructors.append(_md5.md5)
909+
except ImportError:
910+
pass
911+
try:
912+
import _sha1
913+
constructors.append(_sha1.sha1)
914+
except ImportError:
915+
pass
916+
try:
917+
import _sha256
918+
constructors.append(_sha256.sha224)
919+
constructors.append(_sha256.sha256)
920+
except ImportError:
921+
pass
922+
try:
923+
import _sha512
924+
constructors.append(_sha512.sha384)
925+
constructors.append(_sha512.sha512)
926+
except ImportError:
927+
pass
928+
929+
for constructor in constructors:
930+
h = constructor()
931+
with self.subTest(constructor=constructor):
932+
hash_type = type(h)
933+
self.assertRaises(TypeError, hash_type)
934+
904935
@unittest.skipUnless(HASH is not None, 'need _hashlib')
905-
def test_internal_types(self):
936+
def test_hash_disallow_instanciation(self):
906937
# internal types like _hashlib.HASH are not constructable
907938
with self.assertRaisesRegex(
908939
TypeError, "cannot create '_hashlib.HASH' instance"

Modules/md5module.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ static PyType_Slot md5_type_slots[] = {
484484
static PyType_Spec md5_type_spec = {
485485
.name = "_md5.md5",
486486
.basicsize = sizeof(MD5object),
487-
.flags = Py_TPFLAGS_DEFAULT,
487+
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
488488
.slots = md5_type_slots
489489
};
490490

Modules/sha1module.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ static PyType_Slot sha1_type_slots[] = {
462462
static PyType_Spec sha1_type_spec = {
463463
.name = "_sha1.sha1",
464464
.basicsize = sizeof(SHA1object),
465-
.flags = Py_TPFLAGS_DEFAULT,
465+
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
466466
.slots = sha1_type_slots
467467
};
468468

@@ -554,7 +554,7 @@ _sha1_exec(PyObject *module)
554554
}
555555

556556
Py_INCREF(st->sha1_type);
557-
if (PyModule_AddObject(module,
557+
if (PyModule_AddObject(module,
558558
"SHA1Type",
559559
(PyObject *)st->sha1_type) < 0) {
560560
Py_DECREF(st->sha1_type);

Modules/sha256module.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,14 +544,14 @@ static PyType_Slot sha256_types_slots[] = {
544544
static PyType_Spec sha224_type_spec = {
545545
.name = "_sha256.sha224",
546546
.basicsize = sizeof(SHAobject),
547-
.flags = Py_TPFLAGS_DEFAULT,
547+
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
548548
.slots = sha256_types_slots
549549
};
550550

551551
static PyType_Spec sha256_type_spec = {
552552
.name = "_sha256.sha256",
553553
.basicsize = sizeof(SHAobject),
554-
.flags = Py_TPFLAGS_DEFAULT,
554+
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
555555
.slots = sha256_types_slots
556556
};
557557

Modules/sha512module.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ static PyType_Slot sha512_sha384_type_slots[] = {
602602
static PyType_Spec sha512_sha384_type_spec = {
603603
.name = "_sha512.sha384",
604604
.basicsize = sizeof(SHAobject),
605-
.flags = Py_TPFLAGS_DEFAULT,
605+
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
606606
.slots = sha512_sha384_type_slots
607607
};
608608

@@ -619,7 +619,7 @@ static PyType_Slot sha512_sha512_type_slots[] = {
619619
static PyType_Spec sha512_sha512_type_spec = {
620620
.name = "_sha512.sha512",
621621
.basicsize = sizeof(SHAobject),
622-
.flags = Py_TPFLAGS_DEFAULT,
622+
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
623623
.slots = sha512_sha512_type_slots
624624
};
625625

0 commit comments

Comments
 (0)