Skip to content

Commit dce2f6b

Browse files
committed
set tp_new from the class in the hierarchy that actually owns the descriptor (closes #25731)
Debugging by Eryk Sun.
1 parent af29c4f commit dce2f6b

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

Lib/test/test_descr.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4564,6 +4564,14 @@ def __call__(self, arg):
45644564
self.assertRegex(repr(method),
45654565
r"<bound method qualname of <object object at .*>>")
45664566

4567+
def test_deleting_new_in_subclasses(self):
4568+
class X:
4569+
def __init__(self, a):
4570+
pass
4571+
X.__new__ = None
4572+
del X.__new__
4573+
X(1) # should work
4574+
45674575

45684576
class DictProxyTests(unittest.TestCase):
45694577
def setUp(self):

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Release date: tba
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #25731: Fix set and deleting __new__ on a class.
14+
1315
- Issue #22995: [UPDATE] Comment out the one of the pickleability tests in
1416
_PyObject_GetState() due to regressions observed in Cython-based projects.
1517

Objects/typeobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6777,7 +6777,7 @@ update_one_slot(PyTypeObject *type, slotdef *p)
67776777
sanity checks and constructing a new argument
67786778
list. Cut all that nonsense short -- this speeds
67796779
up instance creation tremendously. */
6780-
specific = (void *)type->tp_new;
6780+
specific = (void *)((PyTypeObject *)PyCFunction_GET_SELF(descr))->tp_new;
67816781
/* XXX I'm not 100% sure that there isn't a hole
67826782
in this reasoning that requires additional
67836783
sanity checks. I'll buy the first person to

0 commit comments

Comments
 (0)