mirror of
https://github.com/python/cpython.git
synced 2025-08-02 08:02:56 +00:00
Safety measures now that str and tuple are subclassable:
If tp_itemsize of the basetype is nonzero, only allow empty __slots__ (declaring that no __dict__ should be added), and don't add a weakref offset.
This commit is contained in:
parent
31bcff8815
commit
c41418751f
1 changed files with 9 additions and 1 deletions
|
@ -712,6 +712,13 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
|
||||||
if (slots == NULL)
|
if (slots == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
nslots = PyTuple_GET_SIZE(slots);
|
nslots = PyTuple_GET_SIZE(slots);
|
||||||
|
if (nslots > 0 && base->tp_itemsize != 0) {
|
||||||
|
PyErr_Format(PyExc_TypeError,
|
||||||
|
"nonempty __slots__ "
|
||||||
|
"not supported for subtype of '%s'",
|
||||||
|
base->tp_name);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
for (i = 0; i < nslots; i++) {
|
for (i = 0; i < nslots; i++) {
|
||||||
if (!PyString_Check(PyTuple_GET_ITEM(slots, i))) {
|
if (!PyString_Check(PyTuple_GET_ITEM(slots, i))) {
|
||||||
PyErr_SetString(PyExc_TypeError,
|
PyErr_SetString(PyExc_TypeError,
|
||||||
|
@ -728,7 +735,8 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
|
||||||
nslots++;
|
nslots++;
|
||||||
add_dict++;
|
add_dict++;
|
||||||
}
|
}
|
||||||
if (slots == NULL && base->tp_weaklistoffset == 0) {
|
if (slots == NULL && base->tp_weaklistoffset == 0 &&
|
||||||
|
base->tp_itemsize == 0) {
|
||||||
nslots++;
|
nslots++;
|
||||||
add_weak++;
|
add_weak++;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue