gh-41779: Allow defining any __slots__ for a class derived from tuple (GH-141763)

This commit is contained in:
Serhiy Storchaka 2026-01-06 11:36:00 +02:00 committed by GitHub
parent d6f77e6a3f
commit 4d21297d28
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 70 additions and 14 deletions

View file

@ -141,6 +141,7 @@ _PyCode_Quicken(_Py_CODEUNIT *instructions, Py_ssize_t size, int enable_counters
#define SPEC_FAIL_ATTR_METACLASS_OVERRIDDEN 34
#define SPEC_FAIL_ATTR_SPLIT_DICT 35
#define SPEC_FAIL_ATTR_DESCR_NOT_DEFERRED 36
#define SPEC_FAIL_ATTR_SLOT_AFTER_ITEMS 37
/* Binary subscr and store subscr */
@ -812,6 +813,10 @@ do_specialize_instance_load_attr(PyObject* owner, _Py_CODEUNIT* instr, PyObject*
SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_EXPECTED_ERROR);
return -1;
}
if (dmem->flags & _Py_AFTER_ITEMS) {
SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_SLOT_AFTER_ITEMS);
return -1;
}
if (dmem->flags & Py_AUDIT_READ) {
SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_AUDITED_SLOT);
return -1;
@ -1006,6 +1011,10 @@ _Py_Specialize_StoreAttr(_PyStackRef owner_st, _Py_CODEUNIT *instr, PyObject *na
SPECIALIZATION_FAIL(STORE_ATTR, SPEC_FAIL_EXPECTED_ERROR);
goto fail;
}
if (dmem->flags & _Py_AFTER_ITEMS) {
SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_SLOT_AFTER_ITEMS);
goto fail;
}
if (dmem->flags & Py_READONLY) {
SPECIALIZATION_FAIL(STORE_ATTR, SPEC_FAIL_ATTR_READ_ONLY);
goto fail;

View file

@ -3,6 +3,7 @@
#include "Python.h"
#include "pycore_abstract.h" // _PyNumber_Index()
#include "pycore_descrobject.h" // _PyMember_GetOffset()
#include "pycore_long.h" // _PyLong_IsNegative()
#include "pycore_object.h" // _Py_TryIncrefCompare(), FT_ATOMIC_*()
#include "pycore_critical_section.h"
@ -20,6 +21,17 @@ member_get_object(const char *addr, const char *obj_addr, PyMemberDef *l)
return v;
}
void *
_PyMember_GetOffset(PyObject *obj, PyMemberDef *mp)
{
unsigned char *addr = (unsigned char *)obj + mp->offset;
if (mp->flags & _Py_AFTER_ITEMS) {
PyTypeObject *type = Py_TYPE(obj);
addr += _Py_SIZE_ROUND_UP(Py_SIZE(obj) * type->tp_itemsize, SIZEOF_VOID_P);
}
return addr;
}
PyObject *
PyMember_GetOne(const char *obj_addr, PyMemberDef *l)
{
@ -31,7 +43,7 @@ PyMember_GetOne(const char *obj_addr, PyMemberDef *l)
return NULL;
}
const char* addr = obj_addr + l->offset;
const void *addr = _PyMember_GetOffset((PyObject *)obj_addr, l);
switch (l->type) {
case Py_T_BOOL:
v = PyBool_FromLong(FT_ATOMIC_LOAD_CHAR_RELAXED(*(char*)addr));
@ -80,7 +92,7 @@ PyMember_GetOne(const char *obj_addr, PyMemberDef *l)
v = PyUnicode_FromString((char*)addr);
break;
case Py_T_CHAR: {
char char_val = FT_ATOMIC_LOAD_CHAR_RELAXED(*addr);
char char_val = FT_ATOMIC_LOAD_CHAR_RELAXED(*(char*)addr);
v = PyUnicode_FromStringAndSize(&char_val, 1);
break;
}
@ -151,10 +163,8 @@ PyMember_SetOne(char *addr, PyMemberDef *l, PyObject *v)
return -1;
}
#ifdef Py_GIL_DISABLED
PyObject *obj = (PyObject *) addr;
#endif
addr += l->offset;
PyObject *obj = (PyObject *)addr;
addr = _PyMember_GetOffset(obj, l);
if ((l->flags & Py_READONLY))
{