From 2067587bfddf7b5102b8403a0cf1e8c944798f27 Mon Sep 17 00:00:00 2001 From: Tomasz Pytel Date: Wed, 9 Apr 2025 16:47:27 -0400 Subject: [PATCH 1/7] don't wrap base PyCFunction slots on class creation if not overridden --- Objects/typeobject.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 75c23ddd91b1a1..629009ca03dad4 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -11183,6 +11183,14 @@ update_one_slot(PyTypeObject *type, pytype_slotdef *p) } else { use_generic = 1; + // if (generic == NULL && Py_IS_TYPE(descr, &PyMethodDescr_Type) && + // *ptr == ((PyMethodDescrObject *)descr)->d_method->ml_meth) + // { + // generic = *ptr; + // } + // else { + // generic = p->function; + // } generic = p->function; if (p->function == slot_tp_call) { /* A generic __call__ is incompatible with vectorcall */ From 1873d83e06d0afca589fe39d09ebaad9785b33ff Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 9 Apr 2025 20:49:06 +0000 Subject: [PATCH 2/7] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2025-04-09-20-49-04.gh-issue-132284.TxTNka.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2025-04-09-20-49-04.gh-issue-132284.TxTNka.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-04-09-20-49-04.gh-issue-132284.TxTNka.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-04-09-20-49-04.gh-issue-132284.TxTNka.rst new file mode 100644 index 00000000000000..b63a75f1e7e058 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-04-09-20-49-04.gh-issue-132284.TxTNka.rst @@ -0,0 +1 @@ +Don't wrap base ``PyCFunction`` slots on class creation if not overridden. From 83652a3f2855a075769a617bd747a3c6fe5d2950 Mon Sep 17 00:00:00 2001 From: Tomasz Pytel Date: Wed, 9 Apr 2025 17:08:06 -0400 Subject: [PATCH 3/7] include the actual change --- Objects/typeobject.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 629009ca03dad4..065c737ce37ca4 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -11183,14 +11183,14 @@ update_one_slot(PyTypeObject *type, pytype_slotdef *p) } else { use_generic = 1; - // if (generic == NULL && Py_IS_TYPE(descr, &PyMethodDescr_Type) && - // *ptr == ((PyMethodDescrObject *)descr)->d_method->ml_meth) - // { - // generic = *ptr; - // } - // else { - // generic = p->function; - // } + if (generic == NULL && Py_IS_TYPE(descr, &PyMethodDescr_Type) && + *ptr == ((PyMethodDescrObject *)descr)->d_method->ml_meth) + { + generic = *ptr; + } + else { + generic = p->function; + } generic = p->function; if (p->function == slot_tp_call) { /* A generic __call__ is incompatible with vectorcall */ From ef6477795cf0f055e435324062a2b6d8d5754e28 Mon Sep 17 00:00:00 2001 From: Tomasz Pytel Date: Wed, 9 Apr 2025 17:08:40 -0400 Subject: [PATCH 4/7] stupid testing --- Objects/typeobject.c | 1 - 1 file changed, 1 deletion(-) diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 065c737ce37ca4..4a17bcc2b3a63a 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -11191,7 +11191,6 @@ update_one_slot(PyTypeObject *type, pytype_slotdef *p) else { generic = p->function; } - generic = p->function; if (p->function == slot_tp_call) { /* A generic __call__ is incompatible with vectorcall */ type_clear_flags(type, Py_TPFLAGS_HAVE_VECTORCALL); From 95976fcb390b326e75dbc58c4ed8995a98d5f033 Mon Sep 17 00:00:00 2001 From: Tomasz Pytel Date: Sat, 12 Apr 2025 12:13:09 -0400 Subject: [PATCH 5/7] add requested test --- Lib/test/test_types.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py index d80d317aab1ddc..c767ecaeb85a65 100644 --- a/Lib/test/test_types.py +++ b/Lib/test/test_types.py @@ -1838,6 +1838,23 @@ class Model(metaclass=ModelBase): with self.assertRaises(RuntimeWarning): type("SouthPonies", (Model,), {}) + def test_subclass_inherited_slot_update(self): + # gh-132284: Make sure slot update still works after fix. + # Note that after assignment to D.__getitem__ the actual C slot will + # never go back to dict_subscript as it was on class type creation but + # rather be set to slot_mp_subscript, unfortunately there is no way to + # check that here. + + class D(dict): + pass + + d = D({None: None}) + self.assertIs(d[None], None) + D.__getitem__ = lambda self, item: 42 + self.assertEqual(d[None], 42) + D.__getitem__ = dict.__getitem__ + self.assertIs(d[None], None) + class SimpleNamespaceTests(unittest.TestCase): From b1b91764265ec76ac7a22777d07c676d76309293 Mon Sep 17 00:00:00 2001 From: Tomasz Pytel Date: Tue, 15 Apr 2025 17:08:26 -0400 Subject: [PATCH 6/7] add whatsnew entry describing change --- Doc/whatsnew/3.14.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Doc/whatsnew/3.14.rst b/Doc/whatsnew/3.14.rst index 0e30500fc9b997..2975118cc8924b 100644 --- a/Doc/whatsnew/3.14.rst +++ b/Doc/whatsnew/3.14.rst @@ -464,6 +464,9 @@ Other language changes of HMAC is not available. (Contributed by Bénédikt Tran in :gh:`99108`.) +* When subclassing from a pure C type, the C slots for the new type are no + longer replaced with a wrapped version on class creation if they are not + explicitly overridden in the subclass. .. _whatsnew314-pep765: From 4772fe32c5e731ab539087472f3cf5295faa9c6c Mon Sep 17 00:00:00 2001 From: Tomasz Pytel Date: Tue, 15 Apr 2025 17:18:48 -0400 Subject: [PATCH 7/7] whatsnew tweak --- Doc/whatsnew/3.14.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/Doc/whatsnew/3.14.rst b/Doc/whatsnew/3.14.rst index 2975118cc8924b..feae936224e9db 100644 --- a/Doc/whatsnew/3.14.rst +++ b/Doc/whatsnew/3.14.rst @@ -467,6 +467,7 @@ Other language changes * When subclassing from a pure C type, the C slots for the new type are no longer replaced with a wrapped version on class creation if they are not explicitly overridden in the subclass. + (Contributed by Tomasz Pytel in :gh:`132329`.) .. _whatsnew314-pep765: