Skip to content

Commit 0cad068

Browse files
authored
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).
1 parent 3bb0994 commit 0cad068

File tree

4 files changed

+9
-30
lines changed

4 files changed

+9
-30
lines changed

Lib/test/test_hashlib.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -905,11 +905,11 @@ def test_get_fips_mode(self):
905905
def test_internal_types(self):
906906
# internal types like _hashlib.HASH are not constructable
907907
with self.assertRaisesRegex(
908-
TypeError, "cannot create 'HASH' instance"
908+
TypeError, "cannot create '_hashlib.HASH' instance"
909909
):
910910
HASH()
911911
with self.assertRaisesRegex(
912-
TypeError, "cannot create 'HASHXOF' instance"
912+
TypeError, "cannot create '_hashlib.HASHXOF' instance"
913913
):
914914
HASHXOF()
915915

Lib/test/test_hmac.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ def test_withmodule(self):
440440
def test_internal_types(self):
441441
# internal types like _hashlib.C_HMAC are not constructable
442442
with self.assertRaisesRegex(
443-
TypeError, "cannot create 'HMAC' instance"
443+
TypeError, "cannot create '_hashlib.HMAC' instance"
444444
):
445445
C_HMAC()
446446

Modules/_hashopenssl.c

+3-15
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,6 @@ _setException(PyObject *exc)
118118
}
119119
/* LCOV_EXCL_STOP */
120120

121-
/* {Py_tp_new, NULL} doesn't block __new__ */
122-
static PyObject *
123-
_disabled_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
124-
{
125-
PyErr_Format(PyExc_TypeError,
126-
"cannot create '%.100s' instances", _PyType_Name(type));
127-
return NULL;
128-
}
129-
130121
static PyObject*
131122
py_digest_name(const EVP_MD *md)
132123
{
@@ -590,15 +581,14 @@ static PyType_Slot EVPtype_slots[] = {
590581
{Py_tp_doc, (char *)hashtype_doc},
591582
{Py_tp_methods, EVP_methods},
592583
{Py_tp_getset, EVP_getseters},
593-
{Py_tp_new, _disabled_new},
594584
{0, 0},
595585
};
596586

597587
static PyType_Spec EVPtype_spec = {
598588
"_hashlib.HASH", /*tp_name*/
599589
sizeof(EVPobject), /*tp_basicsize*/
600590
0, /*tp_itemsize*/
601-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
591+
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DISALLOW_INSTANTIATION,
602592
EVPtype_slots
603593
};
604594

@@ -740,15 +730,14 @@ static PyType_Slot EVPXOFtype_slots[] = {
740730
{Py_tp_doc, (char *)hashxoftype_doc},
741731
{Py_tp_methods, EVPXOF_methods},
742732
{Py_tp_getset, EVPXOF_getseters},
743-
{Py_tp_new, _disabled_new},
744733
{0, 0},
745734
};
746735

747736
static PyType_Spec EVPXOFtype_spec = {
748737
"_hashlib.HASHXOF", /*tp_name*/
749738
sizeof(EVPobject), /*tp_basicsize*/
750739
0, /*tp_itemsize*/
751-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
740+
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DISALLOW_INSTANTIATION,
752741
EVPXOFtype_slots
753742
};
754743

@@ -1734,14 +1723,13 @@ static PyType_Slot HMACtype_slots[] = {
17341723
{Py_tp_dealloc,(destructor)_hmac_dealloc},
17351724
{Py_tp_methods, HMAC_methods},
17361725
{Py_tp_getset, HMAC_getset},
1737-
{Py_tp_new, _disabled_new},
17381726
{0, NULL}
17391727
};
17401728

17411729
PyType_Spec HMACtype_spec = {
17421730
"_hashlib.HMAC", /* name */
17431731
sizeof(HMACobject), /* basicsize */
1744-
.flags = Py_TPFLAGS_DEFAULT,
1732+
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
17451733
.slots = HMACtype_slots,
17461734
};
17471735

Modules/posixmodule.c

+3-12
Original file line numberDiff line numberDiff line change
@@ -13415,14 +13415,6 @@ typedef struct {
1341513415
#endif
1341613416
} DirEntry;
1341713417

13418-
static PyObject *
13419-
_disabled_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
13420-
{
13421-
PyErr_Format(PyExc_TypeError,
13422-
"cannot create '%.100s' instances", _PyType_Name(type));
13423-
return NULL;
13424-
}
13425-
1342613418
static void
1342713419
DirEntry_dealloc(DirEntry *entry)
1342813420
{
@@ -13781,7 +13773,6 @@ static PyMethodDef DirEntry_methods[] = {
1378113773
};
1378213774

1378313775
static PyType_Slot DirEntryType_slots[] = {
13784-
{Py_tp_new, _disabled_new},
1378513776
{Py_tp_dealloc, DirEntry_dealloc},
1378613777
{Py_tp_repr, DirEntry_repr},
1378713778
{Py_tp_methods, DirEntry_methods},
@@ -13793,7 +13784,7 @@ static PyType_Spec DirEntryType_spec = {
1379313784
MODNAME ".DirEntry",
1379413785
sizeof(DirEntry),
1379513786
0,
13796-
Py_TPFLAGS_DEFAULT,
13787+
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
1379713788
DirEntryType_slots
1379813789
};
1379913790

@@ -14213,7 +14204,6 @@ static PyMethodDef ScandirIterator_methods[] = {
1421314204
};
1421414205

1421514206
static PyType_Slot ScandirIteratorType_slots[] = {
14216-
{Py_tp_new, _disabled_new},
1421714207
{Py_tp_dealloc, ScandirIterator_dealloc},
1421814208
{Py_tp_finalize, ScandirIterator_finalize},
1421914209
{Py_tp_iter, PyObject_SelfIter},
@@ -14228,7 +14218,8 @@ static PyType_Spec ScandirIteratorType_spec = {
1422814218
0,
1422914219
// bpo-40549: Py_TPFLAGS_BASETYPE should not be used, since
1423014220
// PyType_GetModule(Py_TYPE(self)) doesn't work on a subclass instance.
14231-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_FINALIZE,
14221+
(Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_FINALIZE
14222+
| Py_TPFLAGS_DISALLOW_INSTANTIATION),
1423214223
ScandirIteratorType_slots
1423314224
};
1423414225

0 commit comments

Comments
 (0)