Skip to content

Commit

Permalink
Revert "functools.partial.__get__"
Browse files Browse the repository at this point in the history
This reverts commit 5971fbb.
  • Loading branch information
dg-pb committed Jun 26, 2024
1 parent 5971fbb commit 9033650
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 25 deletions.
6 changes: 0 additions & 6 deletions Lib/functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
# import types, weakref # Deferred to single_dispatch()
from operator import itemgetter
from reprlib import recursive_repr
from types import MethodType
from _thread import RLock

# Avoid importing types, so we can speedup import time
Expand Down Expand Up @@ -382,11 +381,6 @@ class partial:
__slots__ = ("func", "args", "keywords", "_phcount", "_merger",
"__dict__", "__weakref__")

def __get__(self, obj, objtype=None):
if obj is None:
return self
return MethodType(self, obj)

def __new__(cls, func, /, *args, **keywords):
if not callable(func):
raise TypeError("the first argument must be callable")
Expand Down
11 changes: 4 additions & 7 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -2567,17 +2567,14 @@ def _signature_from_callable(obj, *,
skip_bound_arg=skip_bound_arg,
globals=globals, locals=locals, eval_str=eval_str)

if isinstance(obj, functools.partial):
# Must be before _signature_is_builtin
# as it uses `ismethoddescriptor`
# while partial has __get__ implemented
wrapped_sig = _get_signature_of(obj.func)
return _signature_get_partial(wrapped_sig, obj)

if _signature_is_builtin(obj):
return _signature_from_builtin(sigcls, obj,
skip_bound_arg=skip_bound_arg)

if isinstance(obj, functools.partial):
wrapped_sig = _get_signature_of(obj.func)
return _signature_get_partial(wrapped_sig, obj)

if isinstance(obj, type):
# obj is a class or a metaclass

Expand Down
6 changes: 3 additions & 3 deletions Lib/test/test_inspect/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -3681,7 +3681,7 @@ def __init__(self, b):

with self.subTest('partial'):
class CM(type):
__call__ = staticmethod(functools.partial(lambda x, a: (x, a), 2))
__call__ = functools.partial(lambda x, a: (x, a), 2)
class C(metaclass=CM):
def __init__(self, b):
pass
Expand Down Expand Up @@ -3835,7 +3835,7 @@ class C:

with self.subTest('partial'):
class C:
__init__ = staticmethod(functools.partial(lambda x, a: None, 2))
__init__ = functools.partial(lambda x, a: None, 2)

C(1) # does not raise
self.assertEqual(self.signature(C),
Expand Down Expand Up @@ -4093,7 +4093,7 @@ class C:

with self.subTest('partial'):
class C:
__call__ = staticmethod(functools.partial(lambda x, a: (x, a), 2))
__call__ = functools.partial(lambda x, a: (x, a), 2)

self.assertEqual(C()(1), (2, 1))
self.assertEqual(self.signature(C()),
Expand Down
9 changes: 0 additions & 9 deletions Modules/_functoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -331,14 +331,6 @@ partial_dealloc(partialobject *pto)
Py_DECREF(tp);
}

static PyObject *
partial_descr_get(PyObject *pto, PyObject *obj, PyObject *type)
{
if (obj == Py_None || obj == NULL) {
return Py_NewRef(pto);
}
return PyMethod_New(pto, obj);
}

/* Merging keyword arguments using the vectorcall convention is messy, so
* if we would need to do that, we stop using vectorcall and fall back
Expand Down Expand Up @@ -744,7 +736,6 @@ static PyType_Slot partial_type_slots[] = {
{Py_tp_methods, partial_methods},
{Py_tp_members, partial_memberlist},
{Py_tp_getset, partial_getsetlist},
{Py_tp_descr_get, (descrgetfunc)partial_descr_get},
{Py_tp_new, partial_new},
{Py_tp_free, PyObject_GC_Del},
{0, 0}
Expand Down

0 comments on commit 9033650

Please sign in to comment.