File tree Expand file tree Collapse file tree 5 files changed +39
-8
lines changed
Expand file tree Collapse file tree 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):
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"
Original file line number Diff line number Diff line change @@ -484,7 +484,7 @@ static PyType_Slot md5_type_slots[] = {
484484static 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
Original file line number Diff line number Diff line change @@ -462,7 +462,7 @@ static PyType_Slot sha1_type_slots[] = {
462462static 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 );
Original file line number Diff line number Diff line change @@ -544,14 +544,14 @@ static PyType_Slot sha256_types_slots[] = {
544544static 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
551551static 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
Original file line number Diff line number Diff line change @@ -602,7 +602,7 @@ static PyType_Slot sha512_sha384_type_slots[] = {
602602static 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[] = {
619619static 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
You can’t perform that action at this time.
0 commit comments