Skip to content

Commit b727239

Browse files
mjpietersbenjaminp
authored andcommitted
closes bpo-36188: Clean up 'unbound' method left-overs. (GH-12169)
Methods are always bound, and `__self__` can no longer be `NULL` (`method_new()` and `PyMethod_New()` both explicitly check for this). Moreover, once a bound method is bound, it *stays* bound and won't be re-bound to something else, so the section in the datamodel that talks about accessing an methods in a different descriptor-binding context doesn't apply any more in Python 3.
1 parent 0983fcd commit b727239

File tree

3 files changed

+5
-24
lines changed

3 files changed

+5
-24
lines changed

Doc/reference/datamodel.rst

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -578,12 +578,6 @@ Callable types
578578
to be bound. The new method's :attr:`__func__` attribute is the original
579579
function object.
580580

581-
When a user-defined method object is created by retrieving another method
582-
object from a class or instance, the behaviour is the same as for a
583-
function object, except that the :attr:`__func__` attribute of the new
584-
instance is not the original method object but its :attr:`__func__`
585-
attribute.
586-
587581
When an instance method object is created by retrieving a class method
588582
object from a class or instance, its :attr:`__self__` attribute is the
589583
class itself, and its :attr:`__func__` attribute is the function object
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Cleaned up left-over vestiges of Python 2 unbound method handling in method objects and documentation.
2+
Patch by Martijn Pieters

Objects/classobject.c

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,7 @@ static Py_hash_t
267267
method_hash(PyMethodObject *a)
268268
{
269269
Py_hash_t x, y;
270-
if (a->im_self == NULL)
271-
x = _Py_HashPointer(Py_None);
272-
else
273-
x = _Py_HashPointer(a->im_self);
270+
x = _Py_HashPointer(a->im_self);
274271
y = PyObject_Hash(a->im_func);
275272
if (y == -1)
276273
return -1;
@@ -294,11 +291,6 @@ method_call(PyObject *method, PyObject *args, PyObject *kwargs)
294291
PyObject *self, *func;
295292

296293
self = PyMethod_GET_SELF(method);
297-
if (self == NULL) {
298-
PyErr_BadInternalCall();
299-
return NULL;
300-
}
301-
302294
func = PyMethod_GET_FUNCTION(method);
303295

304296
return _PyObject_Call_Prepend(func, self, args, kwargs);
@@ -307,15 +299,8 @@ method_call(PyObject *method, PyObject *args, PyObject *kwargs)
307299
static PyObject *
308300
method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
309301
{
310-
/* Don't rebind an already bound method of a class that's not a base
311-
class of cls. */
312-
if (PyMethod_GET_SELF(meth) != NULL) {
313-
/* Already bound */
314-
Py_INCREF(meth);
315-
return meth;
316-
}
317-
/* Bind it to obj */
318-
return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj);
302+
Py_INCREF(meth);
303+
return meth;
319304
}
320305

321306
PyTypeObject PyMethod_Type = {

0 commit comments

Comments
 (0)