File tree 5 files changed +39
-8
lines changed 5 files changed +39
-8
lines changed Original file line number Diff line number Diff line change @@ -901,8 +901,39 @@ def test_get_fips_mode(self):
901
901
if fips_mode is not None :
902
902
self .assertIsInstance (fips_mode , int )
903
903
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
+
904
935
@unittest .skipUnless (HASH is not None , 'need _hashlib' )
905
- def test_internal_types (self ):
936
+ def test_hash_disallow_instanciation (self ):
906
937
# internal types like _hashlib.HASH are not constructable
907
938
with self .assertRaisesRegex (
908
939
TypeError , "cannot create '_hashlib.HASH' instance"
Original file line number Diff line number Diff line change @@ -484,7 +484,7 @@ static PyType_Slot md5_type_slots[] = {
484
484
static PyType_Spec md5_type_spec = {
485
485
.name = "_md5.md5" ,
486
486
.basicsize = sizeof (MD5object ),
487
- .flags = Py_TPFLAGS_DEFAULT ,
487
+ .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION ,
488
488
.slots = md5_type_slots
489
489
};
490
490
Original file line number Diff line number Diff line change @@ -462,7 +462,7 @@ static PyType_Slot sha1_type_slots[] = {
462
462
static PyType_Spec sha1_type_spec = {
463
463
.name = "_sha1.sha1" ,
464
464
.basicsize = sizeof (SHA1object ),
465
- .flags = Py_TPFLAGS_DEFAULT ,
465
+ .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION ,
466
466
.slots = sha1_type_slots
467
467
};
468
468
@@ -554,7 +554,7 @@ _sha1_exec(PyObject *module)
554
554
}
555
555
556
556
Py_INCREF (st -> sha1_type );
557
- if (PyModule_AddObject (module ,
557
+ if (PyModule_AddObject (module ,
558
558
"SHA1Type" ,
559
559
(PyObject * )st -> sha1_type ) < 0 ) {
560
560
Py_DECREF (st -> sha1_type );
Original file line number Diff line number Diff line change @@ -544,14 +544,14 @@ static PyType_Slot sha256_types_slots[] = {
544
544
static PyType_Spec sha224_type_spec = {
545
545
.name = "_sha256.sha224" ,
546
546
.basicsize = sizeof (SHAobject ),
547
- .flags = Py_TPFLAGS_DEFAULT ,
547
+ .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION ,
548
548
.slots = sha256_types_slots
549
549
};
550
550
551
551
static PyType_Spec sha256_type_spec = {
552
552
.name = "_sha256.sha256" ,
553
553
.basicsize = sizeof (SHAobject ),
554
- .flags = Py_TPFLAGS_DEFAULT ,
554
+ .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION ,
555
555
.slots = sha256_types_slots
556
556
};
557
557
Original file line number Diff line number Diff line change @@ -602,7 +602,7 @@ static PyType_Slot sha512_sha384_type_slots[] = {
602
602
static PyType_Spec sha512_sha384_type_spec = {
603
603
.name = "_sha512.sha384" ,
604
604
.basicsize = sizeof (SHAobject ),
605
- .flags = Py_TPFLAGS_DEFAULT ,
605
+ .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION ,
606
606
.slots = sha512_sha384_type_slots
607
607
};
608
608
@@ -619,7 +619,7 @@ static PyType_Slot sha512_sha512_type_slots[] = {
619
619
static PyType_Spec sha512_sha512_type_spec = {
620
620
.name = "_sha512.sha512" ,
621
621
.basicsize = sizeof (SHAobject ),
622
- .flags = Py_TPFLAGS_DEFAULT ,
622
+ .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION ,
623
623
.slots = sha512_sha512_type_slots
624
624
};
625
625
You can’t perform that action at this time.
0 commit comments