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"
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
};
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
};
}
Py_INCREF(st->sha1_type);
- if (PyModule_AddObject(module,
+ if (PyModule_AddObject(module,
"SHA1Type",
(PyObject *)st->sha1_type) < 0) {
Py_DECREF(st->sha1_type);
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
};
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
};
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
};