Skip to content

Commit a452fce

Browse files
gh-41779: Allow defining any __slots__ for a class derived from tuple
1 parent 3149d64 commit a452fce

File tree

8 files changed

+61
-10
lines changed

8 files changed

+61
-10
lines changed

Doc/reference/datamodel.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2633,7 +2633,7 @@ Notes on using *__slots__*:
26332633
* :exc:`TypeError` will be raised if *__slots__* other than *__dict__* and
26342634
*__weakref__* are defined for a class derived from a
26352635
:c:member:`"variable-length" built-in type <PyTypeObject.tp_itemsize>` such as
2636-
:class:`int`, :class:`bytes`, and :class:`tuple`.
2636+
:class:`int`, :class:`bytes`, and :class:`type`, except :class:`tuple`.
26372637

26382638
* Any non-string :term:`iterable` may be assigned to *__slots__*.
26392639

@@ -2658,6 +2658,7 @@ Notes on using *__slots__*:
26582658

26592659
.. versionchanged:: next
26602660
Allowed defining the *__dict__* and *__weakref__* *__slots__* for any class.
2661+
Allowed defining any *__slots__* for a class derived from :class:`tuple`.
26612662

26622663

26632664
.. _class-customization:

Doc/whatsnew/3.15.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,10 @@ Other language changes
398398
for any class.
399399
(Contributed by Serhiy Storchaka in :gh:`41779`.)
400400

401+
* Allowed defining any :ref:`__slots__ <slots>` for a class derived from
402+
:class:`tuple` (including classes created by :func:`collections.namedtuple`).
403+
(Contributed by Serhiy Storchaka in :gh:`41779`.)
404+
401405

402406
New modules
403407
===========

Include/descrobject.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ struct PyMemberDef {
8484
#define Py_AUDIT_READ 2 // Added in 3.10, harmless no-op before that
8585
#define _Py_WRITE_RESTRICTED 4 // Deprecated, no-op. Do not reuse the value.
8686
#define Py_RELATIVE_OFFSET 8
87+
#define _Py_AFTER_ITEMS 16
8788

8889
PyAPI_FUNC(PyObject *) PyMember_GetOne(const char *, PyMemberDef *);
8990
PyAPI_FUNC(int) PyMember_SetOne(char *, PyMemberDef *, PyObject *);

Lib/test/test_descr.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,6 +1320,18 @@ class X(object):
13201320
with self.assertRaisesRegex(AttributeError, "'X' object has no attribute 'a'"):
13211321
X().a
13221322

1323+
def test_slots_after_items(self):
1324+
class C(tuple):
1325+
__slots__ = ['a']
1326+
x = C((1, 2, 3))
1327+
self.assertNotHasAttr(x, "__dict__")
1328+
self.assertNotHasAttr(x, "a")
1329+
x.a = 42
1330+
self.assertEqual(x.a, 42)
1331+
del x.a
1332+
self.assertNotHasAttr(x, "a")
1333+
self.assertEqual(x, (1, 2, 3))
1334+
13231335
def test_slots_special(self):
13241336
# Testing __dict__ and __weakref__ in __slots__...
13251337
class D(object):
@@ -1422,6 +1434,9 @@ class W(base):
14221434
self.assertIs(weakref.ref(a)(), a)
14231435
self.assertEqual(a, base(arg))
14241436

1437+
@support.subTests('base', [int, bytes] +
1438+
([_testcapi.HeapCCollection] if _testcapi else []))
1439+
def test_unsupported_slots(self, base):
14251440
with self.assertRaises(TypeError):
14261441
class X(base):
14271442
__slots__ = ['x']
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Allowed defining any :ref:`__slots__ <slots>` for a class derived from
2+
:class:`tuple` (including classes created by
3+
:func:`collections.namedtuple`).

Objects/typeobject.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2568,6 +2568,8 @@ _PyHeapType_GET_MEMBERS(PyHeapTypeObject* type)
25682568
return PyObject_GetItemData((PyObject *)type);
25692569
}
25702570

2571+
void *_PyMember_GetOffset(PyObject *obj, PyMemberDef *l);
2572+
25712573
static int
25722574
traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg)
25732575
{
@@ -2578,7 +2580,7 @@ traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg)
25782580
mp = _PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
25792581
for (i = 0; i < n; i++, mp++) {
25802582
if (mp->type == Py_T_OBJECT_EX) {
2581-
char *addr = (char *)self + mp->offset;
2583+
void *addr = _PyMember_GetOffset(self, mp);
25822584
PyObject *obj = *(PyObject **)addr;
25832585
if (obj != NULL) {
25842586
int err = visit(obj, arg);
@@ -2653,7 +2655,7 @@ clear_slots(PyTypeObject *type, PyObject *self)
26532655
mp = _PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
26542656
for (i = 0; i < n; i++, mp++) {
26552657
if (mp->type == Py_T_OBJECT_EX && !(mp->flags & Py_READONLY)) {
2656-
char *addr = (char *)self + mp->offset;
2658+
void *addr = _PyMember_GetOffset(self, mp);
26572659
PyObject *obj = *(PyObject **)addr;
26582660
if (obj != NULL) {
26592661
*(PyObject **)addr = NULL;
@@ -4641,7 +4643,11 @@ type_new_descriptors(const type_new_ctx *ctx, PyTypeObject *type, PyObject *dict
46414643
if (et->ht_slots != NULL) {
46424644
PyMemberDef *mp = _PyHeapType_GET_MEMBERS(et);
46434645
Py_ssize_t nslot = PyTuple_GET_SIZE(et->ht_slots);
4644-
if (ctx->base->tp_itemsize != 0) {
4646+
int after_items = (ctx->base->tp_itemsize != 0 &&
4647+
!(ctx->base->tp_flags & Py_TPFLAGS_ITEMS_AT_END));
4648+
if (ctx->base->tp_itemsize != 0 &&
4649+
!(ctx->base->tp_flags & Py_TPFLAGS_TUPLE_SUBCLASS))
4650+
{
46454651
PyErr_Format(PyExc_TypeError,
46464652
"arbitrary __slots__ not supported for subtype of '%s'",
46474653
ctx->base->tp_name);
@@ -4655,6 +4661,9 @@ type_new_descriptors(const type_new_ctx *ctx, PyTypeObject *type, PyObject *dict
46554661
}
46564662
mp->type = Py_T_OBJECT_EX;
46574663
mp->offset = slotoffset;
4664+
if (after_items) {
4665+
mp->flags |= _Py_AFTER_ITEMS;
4666+
}
46584667

46594668
/* __dict__ and __weakref__ are already filtered out */
46604669
assert(strcmp(mp->name, "__dict__") != 0);

Python/specialize.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ _PyCode_Quicken(_Py_CODEUNIT *instructions, Py_ssize_t size, int enable_counters
139139
#define SPEC_FAIL_ATTR_METACLASS_OVERRIDDEN 34
140140
#define SPEC_FAIL_ATTR_SPLIT_DICT 35
141141
#define SPEC_FAIL_ATTR_DESCR_NOT_DEFERRED 36
142+
#define SPEC_FAIL_ATTR_SLOT_AFTER_ITEMS 37
142143

143144
/* Binary subscr and store subscr */
144145

@@ -810,6 +811,10 @@ do_specialize_instance_load_attr(PyObject* owner, _Py_CODEUNIT* instr, PyObject*
810811
SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_EXPECTED_ERROR);
811812
return -1;
812813
}
814+
if (dmem->flags & _Py_AFTER_ITEMS) {
815+
SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_SLOT_AFTER_ITEMS);
816+
return -1;
817+
}
813818
if (dmem->flags & Py_AUDIT_READ) {
814819
SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_AUDITED_SLOT);
815820
return -1;
@@ -1004,6 +1009,10 @@ _Py_Specialize_StoreAttr(_PyStackRef owner_st, _Py_CODEUNIT *instr, PyObject *na
10041009
SPECIALIZATION_FAIL(STORE_ATTR, SPEC_FAIL_EXPECTED_ERROR);
10051010
goto fail;
10061011
}
1012+
if (dmem->flags & _Py_AFTER_ITEMS) {
1013+
SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_SLOT_AFTER_ITEMS);
1014+
goto fail;
1015+
}
10071016
if (dmem->flags & Py_READONLY) {
10081017
SPECIALIZATION_FAIL(STORE_ATTR, SPEC_FAIL_ATTR_READ_ONLY);
10091018
goto fail;

Python/structmember.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ member_get_object(const char *addr, const char *obj_addr, PyMemberDef *l)
2020
return v;
2121
}
2222

23+
void *
24+
_PyMember_GetOffset(PyObject *obj, PyMemberDef *mp)
25+
{
26+
unsigned char *addr = (unsigned char *)obj + mp->offset;
27+
if (mp->flags & _Py_AFTER_ITEMS) {
28+
PyTypeObject *type = Py_TYPE(obj);
29+
addr += _Py_SIZE_ROUND_UP(Py_SIZE(obj) * type->tp_itemsize, SIZEOF_VOID_P);
30+
}
31+
return addr;
32+
}
33+
2334
PyObject *
2435
PyMember_GetOne(const char *obj_addr, PyMemberDef *l)
2536
{
@@ -31,7 +42,7 @@ PyMember_GetOne(const char *obj_addr, PyMemberDef *l)
3142
return NULL;
3243
}
3344

34-
const char* addr = obj_addr + l->offset;
45+
const void *addr = _PyMember_GetOffset((PyObject *)obj_addr, l);
3546
switch (l->type) {
3647
case Py_T_BOOL:
3748
v = PyBool_FromLong(FT_ATOMIC_LOAD_CHAR_RELAXED(*(char*)addr));
@@ -80,7 +91,7 @@ PyMember_GetOne(const char *obj_addr, PyMemberDef *l)
8091
v = PyUnicode_FromString((char*)addr);
8192
break;
8293
case Py_T_CHAR: {
83-
char char_val = FT_ATOMIC_LOAD_CHAR_RELAXED(*addr);
94+
char char_val = FT_ATOMIC_LOAD_CHAR_RELAXED(*(char*)addr);
8495
v = PyUnicode_FromStringAndSize(&char_val, 1);
8596
break;
8697
}
@@ -151,10 +162,8 @@ PyMember_SetOne(char *addr, PyMemberDef *l, PyObject *v)
151162
return -1;
152163
}
153164

154-
#ifdef Py_GIL_DISABLED
155-
PyObject *obj = (PyObject *) addr;
156-
#endif
157-
addr += l->offset;
165+
PyObject *obj = (PyObject *)addr;
166+
addr = _PyMember_GetOffset(obj, l);
158167

159168
if ((l->flags & Py_READONLY))
160169
{

0 commit comments

Comments
 (0)