mirror of
https://github.com/python/cpython.git
synced 2025-11-10 22:36:18 +00:00
bpo-43916: Remove _disabled_new() function (GH-25745)
posix and _hashlib use the new Py_TPFLAGS_DISALLOW_INSTANTIATION flag on their heap types, rather than using a custom tp_new function (_disabled_new).
This commit is contained in:
parent
3bb09947ec
commit
0cad068ec1
4 changed files with 9 additions and 30 deletions
|
|
@ -905,11 +905,11 @@ class HashLibTestCase(unittest.TestCase):
|
||||||
def test_internal_types(self):
|
def test_internal_types(self):
|
||||||
# internal types like _hashlib.HASH are not constructable
|
# internal types like _hashlib.HASH are not constructable
|
||||||
with self.assertRaisesRegex(
|
with self.assertRaisesRegex(
|
||||||
TypeError, "cannot create 'HASH' instance"
|
TypeError, "cannot create '_hashlib.HASH' instance"
|
||||||
):
|
):
|
||||||
HASH()
|
HASH()
|
||||||
with self.assertRaisesRegex(
|
with self.assertRaisesRegex(
|
||||||
TypeError, "cannot create 'HASHXOF' instance"
|
TypeError, "cannot create '_hashlib.HASHXOF' instance"
|
||||||
):
|
):
|
||||||
HASHXOF()
|
HASHXOF()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -440,7 +440,7 @@ class ConstructorTestCase(unittest.TestCase):
|
||||||
def test_internal_types(self):
|
def test_internal_types(self):
|
||||||
# internal types like _hashlib.C_HMAC are not constructable
|
# internal types like _hashlib.C_HMAC are not constructable
|
||||||
with self.assertRaisesRegex(
|
with self.assertRaisesRegex(
|
||||||
TypeError, "cannot create 'HMAC' instance"
|
TypeError, "cannot create '_hashlib.HMAC' instance"
|
||||||
):
|
):
|
||||||
C_HMAC()
|
C_HMAC()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -118,15 +118,6 @@ _setException(PyObject *exc)
|
||||||
}
|
}
|
||||||
/* LCOV_EXCL_STOP */
|
/* LCOV_EXCL_STOP */
|
||||||
|
|
||||||
/* {Py_tp_new, NULL} doesn't block __new__ */
|
|
||||||
static PyObject *
|
|
||||||
_disabled_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
|
|
||||||
{
|
|
||||||
PyErr_Format(PyExc_TypeError,
|
|
||||||
"cannot create '%.100s' instances", _PyType_Name(type));
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
static PyObject*
|
static PyObject*
|
||||||
py_digest_name(const EVP_MD *md)
|
py_digest_name(const EVP_MD *md)
|
||||||
{
|
{
|
||||||
|
|
@ -590,7 +581,6 @@ static PyType_Slot EVPtype_slots[] = {
|
||||||
{Py_tp_doc, (char *)hashtype_doc},
|
{Py_tp_doc, (char *)hashtype_doc},
|
||||||
{Py_tp_methods, EVP_methods},
|
{Py_tp_methods, EVP_methods},
|
||||||
{Py_tp_getset, EVP_getseters},
|
{Py_tp_getset, EVP_getseters},
|
||||||
{Py_tp_new, _disabled_new},
|
|
||||||
{0, 0},
|
{0, 0},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -598,7 +588,7 @@ static PyType_Spec EVPtype_spec = {
|
||||||
"_hashlib.HASH", /*tp_name*/
|
"_hashlib.HASH", /*tp_name*/
|
||||||
sizeof(EVPobject), /*tp_basicsize*/
|
sizeof(EVPobject), /*tp_basicsize*/
|
||||||
0, /*tp_itemsize*/
|
0, /*tp_itemsize*/
|
||||||
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DISALLOW_INSTANTIATION,
|
||||||
EVPtype_slots
|
EVPtype_slots
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -740,7 +730,6 @@ static PyType_Slot EVPXOFtype_slots[] = {
|
||||||
{Py_tp_doc, (char *)hashxoftype_doc},
|
{Py_tp_doc, (char *)hashxoftype_doc},
|
||||||
{Py_tp_methods, EVPXOF_methods},
|
{Py_tp_methods, EVPXOF_methods},
|
||||||
{Py_tp_getset, EVPXOF_getseters},
|
{Py_tp_getset, EVPXOF_getseters},
|
||||||
{Py_tp_new, _disabled_new},
|
|
||||||
{0, 0},
|
{0, 0},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -748,7 +737,7 @@ static PyType_Spec EVPXOFtype_spec = {
|
||||||
"_hashlib.HASHXOF", /*tp_name*/
|
"_hashlib.HASHXOF", /*tp_name*/
|
||||||
sizeof(EVPobject), /*tp_basicsize*/
|
sizeof(EVPobject), /*tp_basicsize*/
|
||||||
0, /*tp_itemsize*/
|
0, /*tp_itemsize*/
|
||||||
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DISALLOW_INSTANTIATION,
|
||||||
EVPXOFtype_slots
|
EVPXOFtype_slots
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -1734,14 +1723,13 @@ static PyType_Slot HMACtype_slots[] = {
|
||||||
{Py_tp_dealloc,(destructor)_hmac_dealloc},
|
{Py_tp_dealloc,(destructor)_hmac_dealloc},
|
||||||
{Py_tp_methods, HMAC_methods},
|
{Py_tp_methods, HMAC_methods},
|
||||||
{Py_tp_getset, HMAC_getset},
|
{Py_tp_getset, HMAC_getset},
|
||||||
{Py_tp_new, _disabled_new},
|
|
||||||
{0, NULL}
|
{0, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
PyType_Spec HMACtype_spec = {
|
PyType_Spec HMACtype_spec = {
|
||||||
"_hashlib.HMAC", /* name */
|
"_hashlib.HMAC", /* name */
|
||||||
sizeof(HMACobject), /* basicsize */
|
sizeof(HMACobject), /* basicsize */
|
||||||
.flags = Py_TPFLAGS_DEFAULT,
|
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
|
||||||
.slots = HMACtype_slots,
|
.slots = HMACtype_slots,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -13415,14 +13415,6 @@ typedef struct {
|
||||||
#endif
|
#endif
|
||||||
} DirEntry;
|
} DirEntry;
|
||||||
|
|
||||||
static PyObject *
|
|
||||||
_disabled_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
|
|
||||||
{
|
|
||||||
PyErr_Format(PyExc_TypeError,
|
|
||||||
"cannot create '%.100s' instances", _PyType_Name(type));
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
DirEntry_dealloc(DirEntry *entry)
|
DirEntry_dealloc(DirEntry *entry)
|
||||||
{
|
{
|
||||||
|
|
@ -13781,7 +13773,6 @@ static PyMethodDef DirEntry_methods[] = {
|
||||||
};
|
};
|
||||||
|
|
||||||
static PyType_Slot DirEntryType_slots[] = {
|
static PyType_Slot DirEntryType_slots[] = {
|
||||||
{Py_tp_new, _disabled_new},
|
|
||||||
{Py_tp_dealloc, DirEntry_dealloc},
|
{Py_tp_dealloc, DirEntry_dealloc},
|
||||||
{Py_tp_repr, DirEntry_repr},
|
{Py_tp_repr, DirEntry_repr},
|
||||||
{Py_tp_methods, DirEntry_methods},
|
{Py_tp_methods, DirEntry_methods},
|
||||||
|
|
@ -13793,7 +13784,7 @@ static PyType_Spec DirEntryType_spec = {
|
||||||
MODNAME ".DirEntry",
|
MODNAME ".DirEntry",
|
||||||
sizeof(DirEntry),
|
sizeof(DirEntry),
|
||||||
0,
|
0,
|
||||||
Py_TPFLAGS_DEFAULT,
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
|
||||||
DirEntryType_slots
|
DirEntryType_slots
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -14213,7 +14204,6 @@ static PyMethodDef ScandirIterator_methods[] = {
|
||||||
};
|
};
|
||||||
|
|
||||||
static PyType_Slot ScandirIteratorType_slots[] = {
|
static PyType_Slot ScandirIteratorType_slots[] = {
|
||||||
{Py_tp_new, _disabled_new},
|
|
||||||
{Py_tp_dealloc, ScandirIterator_dealloc},
|
{Py_tp_dealloc, ScandirIterator_dealloc},
|
||||||
{Py_tp_finalize, ScandirIterator_finalize},
|
{Py_tp_finalize, ScandirIterator_finalize},
|
||||||
{Py_tp_iter, PyObject_SelfIter},
|
{Py_tp_iter, PyObject_SelfIter},
|
||||||
|
|
@ -14228,7 +14218,8 @@ static PyType_Spec ScandirIteratorType_spec = {
|
||||||
0,
|
0,
|
||||||
// bpo-40549: Py_TPFLAGS_BASETYPE should not be used, since
|
// bpo-40549: Py_TPFLAGS_BASETYPE should not be used, since
|
||||||
// PyType_GetModule(Py_TYPE(self)) doesn't work on a subclass instance.
|
// PyType_GetModule(Py_TYPE(self)) doesn't work on a subclass instance.
|
||||||
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_FINALIZE,
|
(Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_FINALIZE
|
||||||
|
| Py_TPFLAGS_DISALLOW_INSTANTIATION),
|
||||||
ScandirIteratorType_slots
|
ScandirIteratorType_slots
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue