FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-103740: Support any slots in the type subclasses by serhiy-storchaka · Pull Request #141764 · python/cpython · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (1) .rst  (3) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
12 changes: 9 additions & 3 deletions Doc/reference/datamodel.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2616,8 +2616,11 @@ Notes on using *__slots__*:

* :exc:`TypeError` will be raised if *__slots__* other than *__dict__* and
*__weakref__* are defined for a class derived from a
:c:member:`"variable-length" built-in type <PyTypeObject.tp_itemsize>` such as
:class:`int`, :class:`bytes`, and :class:`type`, except :class:`tuple`.
:c:member:`"variable-length" built-in type <PyTypeObject.tp_itemsize>`,
except :class:`tuple`,
unless the C type is defined with :c:macro:`Py_TPFLAGS_ITEMS_AT_END`.
For example, they cannot be defined on subclasses of
:class:`int` or :class:`bytes`.

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

Expand All @@ -2642,7 +2645,10 @@ Notes on using *__slots__*:

.. versionchanged:: 3.15
Allowed defining the *__dict__* and *__weakref__* *__slots__* for any class.
Allowed defining any *__slots__* for a class derived from :class:`tuple`.
Allowed defining any *__slots__* for a class derived from :class:`tuple`,
:class:`type` or other
:c:member:`"variable-length" built-in type <PyTypeObject.tp_itemsize>`
with the :c:macro:`Py_TPFLAGS_ITEMS_AT_END` flag.


.. _class-customization:
Expand Down
6 changes: 6 additions & 0 deletions Doc/whatsnew/3.15.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,12 @@ Other language changes
(Contributed by Serhiy Storchaka in :gh:`41779`.)


* Allowed defining any *__slots__* for a class derived from :class:`type` or
other :c:member:`"variable-length" built-in type <PyTypeObject.tp_itemsize>`
with the :c:macro:`Py_TPFLAGS_ITEMS_AT_END` flag.
(Contributed by Serhiy Storchaka in :gh:`103740`.)


New modules
===========

Expand Down
37 changes: 27 additions & 10 deletions Lib/test/test_descr.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1320,6 +1320,32 @@ class X(object):
with self.assertRaisesRegex(AttributeError, "'X' object has no attribute 'a'"):
X().a

def test_slots_before_items(self):
class C(type):
__slots__ = ['a']
x = C('A', (int,), {})
self.assertNotHasAttr(x, "a")
x.a = 1
x.b = 2
self.assertEqual(x.a, 1)
self.assertEqual(x.b, 2)
self.assertNotIn('a', x.__dict__)
self.assertIn('b', x.__dict__)
del x.a
self.assertNotHasAttr(x, "a")

@unittest.skipIf(_testcapi is None, 'need the _testcapi module')
def test_slots_before_items2(self):
class D(_testcapi.HeapCCollection):
__slots__ = ['a']
x = D(1, 2, 3)
self.assertNotHasAttr(x, "a")
x.a = 42
self.assertEqual(x.a, 42)
del x.a
self.assertNotHasAttr(x, "a")
self.assertEqual(list(x), [1, 2, 3])

def test_slots_after_items(self):
class C(tuple):
__slots__ = ['a']
Expand Down Expand Up @@ -1396,14 +1422,6 @@ class W(_testcapi.HeapCCollection):
a.foo = 42
self.assertIs(weakref.ref(a)(), a)

with self.assertRaises(TypeError):
class X(_testcapi.HeapCCollection):
__slots__ = ['x']

with self.assertRaises(TypeError):
class X(_testcapi.HeapCCollection):
__slots__ = ['__dict__', 'x']

@support.subTests(('base', 'arg'), [
(tuple, (1, 2, 3)),
(int, 9876543210**2),
Expand Down Expand Up @@ -1434,8 +1452,7 @@ class W(base):
self.assertIs(weakref.ref(a)(), a)
self.assertEqual(a, base(arg))

@support.subTests('base', [int, bytes] +
([_testcapi.HeapCCollection] if _testcapi else []))
@support.subTests('base', [int, bytes])
def test_unsupported_slots(self, base):
with self.assertRaises(TypeError):
class X(base):
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Allowed defining the *__dict__* and *__weakref__* :ref:`__slots__ <slots>`
for any class. Allowed defining any *__slots__* for a class derived from
:class:`type` or other :c:member:`"variable-length" built-in type
<PyTypeObject.tp_itemsize>` with the :c:macro:`Py_TPFLAGS_ITEMS_AT_END`
flag.
2 changes: 1 addition & 1 deletion Objects/typeobject.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -4644,7 +4644,7 @@ type_new_descriptors(const type_new_ctx *ctx, PyTypeObject *type, PyObject *dict
Py_ssize_t nslot = PyTuple_GET_SIZE(et->ht_slots);
int after_items = (ctx->base->tp_itemsize != 0 &&
!(ctx->base->tp_flags & Py_TPFLAGS_ITEMS_AT_END));
if (ctx->base->tp_itemsize != 0 &&
if (after_items &&
!(ctx->base->tp_flags & Py_TPFLAGS_TUPLE_SUBCLASS))
{
PyErr_Format(PyExc_TypeError,
Expand Down
Loading

Back | FazBrowse Home | New Git URL