Skip to content

bpo-43916: Remove _disabled_new() function #25745

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Lib/test/test_hashlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -905,11 +905,11 @@ def test_get_fips_mode(self):
def test_internal_types(self):
# internal types like _hashlib.HASH are not constructable
with self.assertRaisesRegex(
TypeError, "cannot create 'HASH' instance"
TypeError, "cannot create '_hashlib.HASH' instance"
):
HASH()
with self.assertRaisesRegex(
TypeError, "cannot create 'HASHXOF' instance"
TypeError, "cannot create '_hashlib.HASHXOF' instance"
):
HASHXOF()

Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_hmac.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ def test_withmodule(self):
def test_internal_types(self):
# internal types like _hashlib.C_HMAC are not constructable
with self.assertRaisesRegex(
TypeError, "cannot create 'HMAC' instance"
TypeError, "cannot create '_hashlib.HMAC' instance"
):
C_HMAC()

Expand Down
18 changes: 3 additions & 15 deletions Modules/_hashopenssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,6 @@ _setException(PyObject *exc)
}
/* 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*
py_digest_name(const EVP_MD *md)
{
Expand Down Expand Up @@ -590,15 +581,14 @@ static PyType_Slot EVPtype_slots[] = {
{Py_tp_doc, (char *)hashtype_doc},
{Py_tp_methods, EVP_methods},
{Py_tp_getset, EVP_getseters},
{Py_tp_new, _disabled_new},
{0, 0},
};

static PyType_Spec EVPtype_spec = {
"_hashlib.HASH", /*tp_name*/
sizeof(EVPobject), /*tp_basicsize*/
0, /*tp_itemsize*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DISALLOW_INSTANTIATION,
EVPtype_slots
};

Expand Down Expand Up @@ -740,15 +730,14 @@ static PyType_Slot EVPXOFtype_slots[] = {
{Py_tp_doc, (char *)hashxoftype_doc},
{Py_tp_methods, EVPXOF_methods},
{Py_tp_getset, EVPXOF_getseters},
{Py_tp_new, _disabled_new},
{0, 0},
};

static PyType_Spec EVPXOFtype_spec = {
"_hashlib.HASHXOF", /*tp_name*/
sizeof(EVPobject), /*tp_basicsize*/
0, /*tp_itemsize*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DISALLOW_INSTANTIATION,
EVPXOFtype_slots
};

Expand Down Expand Up @@ -1734,14 +1723,13 @@ static PyType_Slot HMACtype_slots[] = {
{Py_tp_dealloc,(destructor)_hmac_dealloc},
{Py_tp_methods, HMAC_methods},
{Py_tp_getset, HMAC_getset},
{Py_tp_new, _disabled_new},
{0, NULL}
};

PyType_Spec HMACtype_spec = {
"_hashlib.HMAC", /* name */
sizeof(HMACobject), /* basicsize */
.flags = Py_TPFLAGS_DEFAULT,
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
.slots = HMACtype_slots,
};

Expand Down
15 changes: 3 additions & 12 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -13415,14 +13415,6 @@ typedef struct {
#endif
} 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
DirEntry_dealloc(DirEntry *entry)
{
Expand Down Expand Up @@ -13781,7 +13773,6 @@ static PyMethodDef DirEntry_methods[] = {
};

static PyType_Slot DirEntryType_slots[] = {
{Py_tp_new, _disabled_new},
{Py_tp_dealloc, DirEntry_dealloc},
{Py_tp_repr, DirEntry_repr},
{Py_tp_methods, DirEntry_methods},
Expand All @@ -13793,7 +13784,7 @@ static PyType_Spec DirEntryType_spec = {
MODNAME ".DirEntry",
sizeof(DirEntry),
0,
Py_TPFLAGS_DEFAULT,
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
DirEntryType_slots
};

Expand Down Expand Up @@ -14213,7 +14204,6 @@ static PyMethodDef ScandirIterator_methods[] = {
};

static PyType_Slot ScandirIteratorType_slots[] = {
{Py_tp_new, _disabled_new},
{Py_tp_dealloc, ScandirIterator_dealloc},
{Py_tp_finalize, ScandirIterator_finalize},
{Py_tp_iter, PyObject_SelfIter},
Expand All @@ -14228,7 +14218,8 @@ static PyType_Spec ScandirIteratorType_spec = {
0,
// bpo-40549: Py_TPFLAGS_BASETYPE should not be used, since
// 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
};

Expand Down