2006-08-17 02:42:55 -03:00
|
|
|
/* Class object implementation (dead now except for methods) */
|
1990-10-14 09:07:46 -03:00
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
#include "Python.h"
|
2021-10-14 16:53:04 -03:00
|
|
|
#include "pycore_call.h" // _PyObject_VectorcallTstate()
|
2018-11-21 17:27:47 -04:00
|
|
|
#include "pycore_object.h"
|
2019-11-08 05:05:17 -04:00
|
|
|
#include "pycore_pyerrors.h"
|
2020-04-14 21:35:41 -03:00
|
|
|
#include "pycore_pystate.h" // _PyThreadState_GET()
|
|
|
|
#include "structmember.h" // PyMemberDef
|
1992-08-12 12:35:34 -03:00
|
|
|
|
2022-04-18 23:56:53 -03:00
|
|
|
#include "clinic/classobject.c.h"
|
|
|
|
|
2006-07-27 18:53:35 -03:00
|
|
|
#define TP_DESCR_GET(t) ((t)->tp_descr_get)
|
2001-10-17 17:26:38 -03:00
|
|
|
|
2022-04-18 23:56:53 -03:00
|
|
|
/*[clinic input]
|
|
|
|
class method "PyMethodObject *" "&PyMethod_Type"
|
|
|
|
[clinic start generated code]*/
|
|
|
|
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b16e47edf6107c23]*/
|
|
|
|
|
2011-10-10 13:11:30 -03:00
|
|
|
|
2001-09-05 19:52:50 -03:00
|
|
|
PyObject *
|
|
|
|
PyMethod_Function(PyObject *im)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!PyMethod_Check(im)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ((PyMethodObject *)im)->im_func;
|
2001-09-05 19:52:50 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyMethod_Self(PyObject *im)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!PyMethod_Check(im)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ((PyMethodObject *)im)->im_self;
|
2001-09-05 19:52:50 -03:00
|
|
|
}
|
|
|
|
|
2019-05-29 15:31:52 -03:00
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
method_vectorcall(PyObject *method, PyObject *const *args,
|
|
|
|
size_t nargsf, PyObject *kwnames)
|
|
|
|
{
|
2020-02-17 06:09:15 -04:00
|
|
|
assert(Py_IS_TYPE(method, &PyMethod_Type));
|
2019-11-08 05:05:17 -04:00
|
|
|
|
|
|
|
PyThreadState *tstate = _PyThreadState_GET();
|
|
|
|
PyObject *self = PyMethod_GET_SELF(method);
|
|
|
|
PyObject *func = PyMethod_GET_FUNCTION(method);
|
2019-05-29 15:31:52 -03:00
|
|
|
Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
|
|
|
|
|
2019-11-08 05:05:17 -04:00
|
|
|
PyObject *result;
|
2019-05-29 15:31:52 -03:00
|
|
|
if (nargsf & PY_VECTORCALL_ARGUMENTS_OFFSET) {
|
|
|
|
/* PY_VECTORCALL_ARGUMENTS_OFFSET is set, so we are allowed to mutate the vector */
|
|
|
|
PyObject **newargs = (PyObject**)args - 1;
|
|
|
|
nargs += 1;
|
|
|
|
PyObject *tmp = newargs[0];
|
|
|
|
newargs[0] = self;
|
2019-11-08 05:05:17 -04:00
|
|
|
result = _PyObject_VectorcallTstate(tstate, func, newargs,
|
|
|
|
nargs, kwnames);
|
2019-05-29 15:31:52 -03:00
|
|
|
newargs[0] = tmp;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
|
|
|
|
Py_ssize_t totalargs = nargs + nkwargs;
|
2019-07-03 07:54:00 -03:00
|
|
|
if (totalargs == 0) {
|
2019-11-08 05:05:17 -04:00
|
|
|
return _PyObject_VectorcallTstate(tstate, func, &self, 1, NULL);
|
2019-07-03 07:54:00 -03:00
|
|
|
}
|
|
|
|
|
2019-06-18 05:56:53 -03:00
|
|
|
PyObject *newargs_stack[_PY_FASTCALL_SMALL_STACK];
|
2019-07-03 07:54:00 -03:00
|
|
|
PyObject **newargs;
|
2019-06-18 05:56:53 -03:00
|
|
|
if (totalargs <= (Py_ssize_t)Py_ARRAY_LENGTH(newargs_stack) - 1) {
|
|
|
|
newargs = newargs_stack;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
newargs = PyMem_Malloc((totalargs+1) * sizeof(PyObject *));
|
|
|
|
if (newargs == NULL) {
|
2019-11-08 05:05:17 -04:00
|
|
|
_PyErr_NoMemory(tstate);
|
2019-06-18 05:56:53 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2019-05-29 15:31:52 -03:00
|
|
|
}
|
|
|
|
/* use borrowed references */
|
|
|
|
newargs[0] = self;
|
2019-07-03 07:54:00 -03:00
|
|
|
/* bpo-37138: since totalargs > 0, it's impossible that args is NULL.
|
|
|
|
* We need this, since calling memcpy() with a NULL pointer is
|
|
|
|
* undefined behaviour. */
|
|
|
|
assert(args != NULL);
|
|
|
|
memcpy(newargs + 1, args, totalargs * sizeof(PyObject *));
|
2019-11-08 05:05:17 -04:00
|
|
|
result = _PyObject_VectorcallTstate(tstate, func,
|
|
|
|
newargs, nargs+1, kwnames);
|
2019-06-18 05:56:53 -03:00
|
|
|
if (newargs != newargs_stack) {
|
|
|
|
PyMem_Free(newargs);
|
|
|
|
}
|
2019-05-29 15:31:52 -03:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-27 06:40:20 -04:00
|
|
|
/* Method objects are used for bound instance methods returned by
|
|
|
|
instancename.methodname. ClassName.methodname returns an ordinary
|
|
|
|
function.
|
1993-05-20 11:24:46 -03:00
|
|
|
*/
|
1990-10-14 09:07:46 -03:00
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
2007-11-27 06:40:20 -04:00
|
|
|
PyMethod_New(PyObject *func, PyObject *self)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (self == NULL) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-07-26 03:05:50 -03:00
|
|
|
PyMethodObject *im = PyObject_GC_New(PyMethodObject, &PyMethod_Type);
|
|
|
|
if (im == NULL) {
|
|
|
|
return NULL;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
im->im_weakreflist = NULL;
|
|
|
|
Py_INCREF(func);
|
|
|
|
im->im_func = func;
|
2019-06-16 17:19:19 -03:00
|
|
|
Py_INCREF(self);
|
2010-05-09 12:52:27 -03:00
|
|
|
im->im_self = self;
|
2019-05-29 15:31:52 -03:00
|
|
|
im->vectorcall = method_vectorcall;
|
2010-05-09 12:52:27 -03:00
|
|
|
_PyObject_GC_TRACK(im);
|
|
|
|
return (PyObject *)im;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
2022-04-18 23:56:53 -03:00
|
|
|
/*[clinic input]
|
|
|
|
method.__reduce__
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
2013-11-23 13:59:12 -04:00
|
|
|
static PyObject *
|
2022-04-18 23:56:53 -03:00
|
|
|
method___reduce___impl(PyMethodObject *self)
|
|
|
|
/*[clinic end generated code: output=6c04506d0fa6fdcb input=143a0bf5e96de6e8]*/
|
2013-11-23 13:59:12 -04:00
|
|
|
{
|
2022-04-18 23:56:53 -03:00
|
|
|
PyObject *funcself = PyMethod_GET_SELF(self);
|
|
|
|
PyObject *func = PyMethod_GET_FUNCTION(self);
|
|
|
|
PyObject *funcname = PyObject_GetAttr(func, &_Py_ID(__name__));
|
2013-11-23 13:59:12 -04:00
|
|
|
if (funcname == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-02-08 16:39:07 -04:00
|
|
|
return Py_BuildValue(
|
2022-04-18 23:56:53 -03:00
|
|
|
"N(ON)", _PyEval_GetBuiltin(&_Py_ID(getattr)), funcself, funcname);
|
2013-11-23 13:59:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyMethodDef method_methods[] = {
|
2022-04-18 23:56:53 -03:00
|
|
|
METHOD___REDUCE___METHODDEF
|
2013-11-23 13:59:12 -04:00
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
2001-09-18 00:53:24 -03:00
|
|
|
/* Descriptors for PyMethod attributes */
|
|
|
|
|
2007-11-27 06:40:20 -04:00
|
|
|
/* im_func and im_self are stored in the PyMethod object */
|
1990-10-14 09:07:46 -03:00
|
|
|
|
2007-12-11 15:56:40 -04:00
|
|
|
#define MO_OFF(x) offsetof(PyMethodObject, x)
|
1990-12-20 11:06:42 -04:00
|
|
|
|
2006-08-23 21:41:19 -03:00
|
|
|
static PyMemberDef method_memberlist[] = {
|
2020-02-18 09:14:46 -04:00
|
|
|
{"__func__", T_OBJECT, MO_OFF(im_func), READONLY,
|
2010-05-09 12:52:27 -03:00
|
|
|
"the function (or other callable) implementing a method"},
|
2020-02-18 09:14:46 -04:00
|
|
|
{"__self__", T_OBJECT, MO_OFF(im_self), READONLY,
|
2010-05-09 12:52:27 -03:00
|
|
|
"the instance to which a method is bound"},
|
|
|
|
{NULL} /* Sentinel */
|
1990-12-20 11:06:42 -04:00
|
|
|
};
|
|
|
|
|
2003-11-22 19:55:50 -04:00
|
|
|
/* Christian Tismer argued convincingly that method attributes should
|
|
|
|
(nearly) always override function attributes.
|
|
|
|
The one exception is __doc__; there's a default __doc__ which
|
|
|
|
should only be used for the class, not for instances */
|
|
|
|
|
|
|
|
static PyObject *
|
2006-08-23 21:41:19 -03:00
|
|
|
method_get_doc(PyMethodObject *im, void *context)
|
2003-11-22 19:55:50 -04:00
|
|
|
{
|
2022-02-22 20:23:51 -04:00
|
|
|
return PyObject_GetAttr(im->im_func, &_Py_ID(__doc__));
|
2003-11-22 19:55:50 -04:00
|
|
|
}
|
|
|
|
|
2006-08-23 21:41:19 -03:00
|
|
|
static PyGetSetDef method_getset[] = {
|
2010-05-09 12:52:27 -03:00
|
|
|
{"__doc__", (getter)method_get_doc, NULL, NULL},
|
|
|
|
{0}
|
2003-11-22 19:55:50 -04:00
|
|
|
};
|
2001-01-15 16:40:19 -04:00
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2006-08-23 21:41:19 -03:00
|
|
|
method_getattro(PyObject *obj, PyObject *name)
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyMethodObject *im = (PyMethodObject *)obj;
|
2020-02-06 22:04:21 -04:00
|
|
|
PyTypeObject *tp = Py_TYPE(obj);
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *descr = NULL;
|
|
|
|
|
|
|
|
{
|
|
|
|
if (tp->tp_dict == NULL) {
|
|
|
|
if (PyType_Ready(tp) < 0)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
descr = _PyType_Lookup(tp, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (descr != NULL) {
|
2020-02-06 22:04:21 -04:00
|
|
|
descrgetfunc f = TP_DESCR_GET(Py_TYPE(descr));
|
2010-05-09 12:52:27 -03:00
|
|
|
if (f != NULL)
|
2020-02-06 22:04:21 -04:00
|
|
|
return f(descr, obj, (PyObject *)Py_TYPE(obj));
|
2010-05-09 12:52:27 -03:00
|
|
|
else {
|
|
|
|
Py_INCREF(descr);
|
|
|
|
return descr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return PyObject_GetAttr(im->im_func, name);
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
2022-04-18 23:56:53 -03:00
|
|
|
/*[clinic input]
|
|
|
|
@classmethod
|
|
|
|
method.__new__ as method_new
|
|
|
|
function: object
|
|
|
|
instance: object
|
|
|
|
/
|
|
|
|
|
|
|
|
Create a bound instance method object.
|
|
|
|
[clinic start generated code]*/
|
2002-06-14 17:41:17 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2022-04-18 23:56:53 -03:00
|
|
|
method_new_impl(PyTypeObject *type, PyObject *function, PyObject *instance)
|
|
|
|
/*[clinic end generated code: output=d33ef4ebf702e1f7 input=4e32facc3c3108ae]*/
|
2002-06-14 17:41:17 -03:00
|
|
|
{
|
2022-04-18 23:56:53 -03:00
|
|
|
if (!PyCallable_Check(function)) {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"first argument must be callable");
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-04-18 23:56:53 -03:00
|
|
|
if (instance == NULL || instance == Py_None) {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
2022-04-18 23:56:53 -03:00
|
|
|
"instance must not be None");
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-04-18 23:56:53 -03:00
|
|
|
return PyMethod_New(function, instance);
|
2002-06-14 17:41:17 -03:00
|
|
|
}
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
static void
|
2013-08-13 15:18:52 -03:00
|
|
|
method_dealloc(PyMethodObject *im)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
_PyObject_GC_UNTRACK(im);
|
|
|
|
if (im->im_weakreflist != NULL)
|
|
|
|
PyObject_ClearWeakRefs((PyObject *)im);
|
|
|
|
Py_DECREF(im->im_func);
|
|
|
|
Py_XDECREF(im->im_self);
|
2019-07-26 03:05:50 -03:00
|
|
|
PyObject_GC_Del(im);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
2006-08-23 21:41:19 -03:00
|
|
|
static PyObject *
|
|
|
|
method_richcompare(PyObject *self, PyObject *other, int op)
|
1992-09-03 17:39:51 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyMethodObject *a, *b;
|
|
|
|
PyObject *res;
|
|
|
|
int eq;
|
|
|
|
|
|
|
|
if ((op != Py_EQ && op != Py_NE) ||
|
|
|
|
!PyMethod_Check(self) ||
|
|
|
|
!PyMethod_Check(other))
|
|
|
|
{
|
2011-08-10 22:28:54 -03:00
|
|
|
Py_RETURN_NOTIMPLEMENTED;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
a = (PyMethodObject *)self;
|
|
|
|
b = (PyMethodObject *)other;
|
|
|
|
eq = PyObject_RichCompareBool(a->im_func, b->im_func, Py_EQ);
|
|
|
|
if (eq == 1) {
|
2018-07-31 03:18:24 -03:00
|
|
|
eq = (a->im_self == b->im_self);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2018-07-31 03:18:24 -03:00
|
|
|
else if (eq < 0)
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
|
|
|
if (op == Py_EQ)
|
|
|
|
res = eq ? Py_True : Py_False;
|
|
|
|
else
|
|
|
|
res = eq ? Py_False : Py_True;
|
|
|
|
Py_INCREF(res);
|
|
|
|
return res;
|
1992-09-03 17:39:51 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2006-08-23 21:41:19 -03:00
|
|
|
method_repr(PyMethodObject *a)
|
1993-05-19 11:50:45 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *self = a->im_self;
|
|
|
|
PyObject *func = a->im_func;
|
2018-01-25 04:49:40 -04:00
|
|
|
PyObject *funcname, *result;
|
2014-08-20 20:41:57 -03:00
|
|
|
const char *defname = "?";
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2022-02-08 16:39:07 -04:00
|
|
|
if (_PyObject_LookupAttr(func, &_Py_ID(__qualname__), &funcname) < 0 ||
|
2018-01-25 04:49:40 -04:00
|
|
|
(funcname == NULL &&
|
2022-02-08 16:39:07 -04:00
|
|
|
_PyObject_LookupAttr(func, &_Py_ID(__name__), &funcname) < 0))
|
2018-01-25 04:49:40 -04:00
|
|
|
{
|
|
|
|
return NULL;
|
2014-08-20 20:41:57 -03:00
|
|
|
}
|
2015-03-18 16:53:15 -03:00
|
|
|
|
2014-08-20 20:41:57 -03:00
|
|
|
if (funcname != NULL && !PyUnicode_Check(funcname)) {
|
|
|
|
Py_DECREF(funcname);
|
|
|
|
funcname = NULL;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX Shouldn't use repr()/%R here! */
|
2014-08-20 20:41:57 -03:00
|
|
|
result = PyUnicode_FromFormat("<bound method %V of %R>",
|
2010-05-09 12:52:27 -03:00
|
|
|
funcname, defname, self);
|
|
|
|
|
|
|
|
Py_XDECREF(funcname);
|
|
|
|
return result;
|
1993-05-19 11:50:45 -03:00
|
|
|
}
|
|
|
|
|
2010-10-17 17:54:53 -03:00
|
|
|
static Py_hash_t
|
2006-08-23 21:41:19 -03:00
|
|
|
method_hash(PyMethodObject *a)
|
1993-03-29 06:43:31 -04:00
|
|
|
{
|
2010-10-17 17:54:53 -03:00
|
|
|
Py_hash_t x, y;
|
2019-03-05 01:19:34 -04:00
|
|
|
x = _Py_HashPointer(a->im_self);
|
2010-05-09 12:52:27 -03:00
|
|
|
y = PyObject_Hash(a->im_func);
|
|
|
|
if (y == -1)
|
|
|
|
return -1;
|
|
|
|
x = x ^ y;
|
|
|
|
if (x == -1)
|
|
|
|
x = -2;
|
|
|
|
return x;
|
1993-03-29 06:43:31 -04:00
|
|
|
}
|
|
|
|
|
2000-06-23 11:18:11 -03:00
|
|
|
static int
|
2006-08-23 21:41:19 -03:00
|
|
|
method_traverse(PyMethodObject *im, visitproc visit, void *arg)
|
2000-06-23 11:18:11 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_VISIT(im->im_func);
|
|
|
|
Py_VISIT(im->im_self);
|
|
|
|
return 0;
|
2000-06-23 11:18:11 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyTypeObject PyMethod_Type = {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
2022-04-18 23:56:53 -03:00
|
|
|
.tp_name = "method",
|
|
|
|
.tp_basicsize = sizeof(PyMethodObject),
|
|
|
|
.tp_dealloc = (destructor)method_dealloc,
|
|
|
|
.tp_vectorcall_offset = offsetof(PyMethodObject, vectorcall),
|
|
|
|
.tp_repr = (reprfunc)method_repr,
|
|
|
|
.tp_hash = (hashfunc)method_hash,
|
|
|
|
.tp_call = PyVectorcall_Call,
|
|
|
|
.tp_getattro = method_getattro,
|
|
|
|
.tp_setattro = PyObject_GenericSetAttr,
|
|
|
|
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
|
|
|
|
Py_TPFLAGS_HAVE_VECTORCALL,
|
|
|
|
.tp_doc = method_new__doc__,
|
|
|
|
.tp_traverse = (traverseproc)method_traverse,
|
|
|
|
.tp_richcompare = method_richcompare,
|
|
|
|
.tp_weaklistoffset = offsetof(PyMethodObject, im_weakreflist),
|
|
|
|
.tp_methods = method_methods,
|
|
|
|
.tp_members = method_memberlist,
|
|
|
|
.tp_getset = method_getset,
|
|
|
|
.tp_new = method_new,
|
1990-10-14 09:07:46 -03:00
|
|
|
};
|
1997-08-04 23:06:53 -03:00
|
|
|
|
2007-12-11 15:56:40 -04:00
|
|
|
/* ------------------------------------------------------------------------
|
|
|
|
* instance method
|
|
|
|
*/
|
|
|
|
|
2022-04-18 23:56:53 -03:00
|
|
|
/*[clinic input]
|
|
|
|
class instancemethod "PyInstanceMethodObject *" "&PyInstanceMethod_Type"
|
|
|
|
[clinic start generated code]*/
|
|
|
|
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=28c9762a9016f4d2]*/
|
|
|
|
|
2007-12-11 15:56:40 -04:00
|
|
|
PyObject *
|
|
|
|
PyInstanceMethod_New(PyObject *func) {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyInstanceMethodObject *method;
|
|
|
|
method = PyObject_GC_New(PyInstanceMethodObject,
|
|
|
|
&PyInstanceMethod_Type);
|
|
|
|
if (method == NULL) return NULL;
|
|
|
|
Py_INCREF(func);
|
|
|
|
method->func = func;
|
|
|
|
_PyObject_GC_TRACK(method);
|
|
|
|
return (PyObject *)method;
|
2007-12-11 15:56:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyInstanceMethod_Function(PyObject *im)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!PyInstanceMethod_Check(im)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return PyInstanceMethod_GET_FUNCTION(im);
|
2007-12-11 15:56:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#define IMO_OFF(x) offsetof(PyInstanceMethodObject, x)
|
|
|
|
|
|
|
|
static PyMemberDef instancemethod_memberlist[] = {
|
2020-02-18 09:14:46 -04:00
|
|
|
{"__func__", T_OBJECT, IMO_OFF(func), READONLY,
|
2010-05-09 12:52:27 -03:00
|
|
|
"the function (or other callable) implementing a method"},
|
|
|
|
{NULL} /* Sentinel */
|
2007-12-11 15:56:40 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
instancemethod_get_doc(PyObject *self, void *context)
|
|
|
|
{
|
2022-02-22 20:23:51 -04:00
|
|
|
return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self),
|
|
|
|
&_Py_ID(__doc__));
|
2007-12-11 15:56:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyGetSetDef instancemethod_getset[] = {
|
2010-05-09 12:52:27 -03:00
|
|
|
{"__doc__", (getter)instancemethod_get_doc, NULL, NULL},
|
|
|
|
{0}
|
2007-12-11 15:56:40 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
instancemethod_getattro(PyObject *self, PyObject *name)
|
|
|
|
{
|
2020-02-06 22:04:21 -04:00
|
|
|
PyTypeObject *tp = Py_TYPE(self);
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *descr = NULL;
|
|
|
|
|
|
|
|
if (tp->tp_dict == NULL) {
|
|
|
|
if (PyType_Ready(tp) < 0)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
descr = _PyType_Lookup(tp, name);
|
|
|
|
|
|
|
|
if (descr != NULL) {
|
2020-02-06 22:04:21 -04:00
|
|
|
descrgetfunc f = TP_DESCR_GET(Py_TYPE(descr));
|
2010-05-09 12:52:27 -03:00
|
|
|
if (f != NULL)
|
2020-02-06 22:04:21 -04:00
|
|
|
return f(descr, self, (PyObject *)Py_TYPE(self));
|
2010-05-09 12:52:27 -03:00
|
|
|
else {
|
|
|
|
Py_INCREF(descr);
|
|
|
|
return descr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), name);
|
2007-12-11 15:56:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
instancemethod_dealloc(PyObject *self) {
|
2010-05-09 12:52:27 -03:00
|
|
|
_PyObject_GC_UNTRACK(self);
|
|
|
|
Py_DECREF(PyInstanceMethod_GET_FUNCTION(self));
|
|
|
|
PyObject_GC_Del(self);
|
2007-12-11 15:56:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
instancemethod_traverse(PyObject *self, visitproc visit, void *arg) {
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_VISIT(PyInstanceMethod_GET_FUNCTION(self));
|
|
|
|
return 0;
|
2007-12-11 15:56:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
instancemethod_call(PyObject *self, PyObject *arg, PyObject *kw)
|
|
|
|
{
|
2021-07-28 10:10:36 -03:00
|
|
|
return PyObject_Call(PyInstanceMethod_GET_FUNCTION(self), arg, kw);
|
2007-12-11 15:56:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
instancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) {
|
2013-08-13 15:18:52 -03:00
|
|
|
PyObject *func = PyInstanceMethod_GET_FUNCTION(descr);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (obj == NULL) {
|
|
|
|
Py_INCREF(func);
|
|
|
|
return func;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return PyMethod_New(func, obj);
|
2007-12-11 15:56:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
instancemethod_richcompare(PyObject *self, PyObject *other, int op)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyInstanceMethodObject *a, *b;
|
|
|
|
PyObject *res;
|
|
|
|
int eq;
|
|
|
|
|
|
|
|
if ((op != Py_EQ && op != Py_NE) ||
|
|
|
|
!PyInstanceMethod_Check(self) ||
|
|
|
|
!PyInstanceMethod_Check(other))
|
|
|
|
{
|
2011-08-10 22:28:54 -03:00
|
|
|
Py_RETURN_NOTIMPLEMENTED;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
a = (PyInstanceMethodObject *)self;
|
|
|
|
b = (PyInstanceMethodObject *)other;
|
|
|
|
eq = PyObject_RichCompareBool(a->func, b->func, Py_EQ);
|
|
|
|
if (eq < 0)
|
|
|
|
return NULL;
|
|
|
|
if (op == Py_EQ)
|
|
|
|
res = eq ? Py_True : Py_False;
|
|
|
|
else
|
|
|
|
res = eq ? Py_False : Py_True;
|
|
|
|
Py_INCREF(res);
|
|
|
|
return res;
|
2007-12-11 15:56:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
instancemethod_repr(PyObject *self)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *func = PyInstanceMethod_Function(self);
|
2018-01-25 04:49:40 -04:00
|
|
|
PyObject *funcname, *result;
|
2017-11-11 07:06:26 -04:00
|
|
|
const char *defname = "?";
|
2010-05-09 12:52:27 -03:00
|
|
|
|
|
|
|
if (func == NULL) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-02-08 16:39:07 -04:00
|
|
|
if (_PyObject_LookupAttr(func, &_Py_ID(__name__), &funcname) < 0) {
|
2018-01-25 04:49:40 -04:00
|
|
|
return NULL;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2018-01-25 04:49:40 -04:00
|
|
|
if (funcname != NULL && !PyUnicode_Check(funcname)) {
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_DECREF(funcname);
|
|
|
|
funcname = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = PyUnicode_FromFormat("<instancemethod %V at %p>",
|
|
|
|
funcname, defname, self);
|
|
|
|
|
|
|
|
Py_XDECREF(funcname);
|
|
|
|
return result;
|
2007-12-11 15:56:40 -04:00
|
|
|
}
|
|
|
|
|
2022-04-18 23:56:53 -03:00
|
|
|
/*[clinic input]
|
|
|
|
@classmethod
|
|
|
|
instancemethod.__new__ as instancemethod_new
|
|
|
|
function: object
|
|
|
|
/
|
|
|
|
|
|
|
|
Bind a function to a class.
|
|
|
|
[clinic start generated code]*/
|
2007-12-11 15:56:40 -04:00
|
|
|
|
|
|
|
static PyObject *
|
2022-04-18 23:56:53 -03:00
|
|
|
instancemethod_new_impl(PyTypeObject *type, PyObject *function)
|
|
|
|
/*[clinic end generated code: output=5e0397b2bdb750be input=cfc54e8b973664a8]*/
|
2007-12-11 15:56:40 -04:00
|
|
|
{
|
2022-04-18 23:56:53 -03:00
|
|
|
if (!PyCallable_Check(function)) {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"first argument must be callable");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-04-18 23:56:53 -03:00
|
|
|
return PyInstanceMethod_New(function);
|
2007-12-11 15:56:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
PyTypeObject PyInstanceMethod_Type = {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
2022-04-18 23:56:53 -03:00
|
|
|
.tp_name = "instancemethod",
|
|
|
|
.tp_basicsize = sizeof(PyInstanceMethodObject),
|
|
|
|
.tp_dealloc = instancemethod_dealloc,
|
|
|
|
.tp_repr = (reprfunc)instancemethod_repr,
|
|
|
|
.tp_call = instancemethod_call,
|
|
|
|
.tp_getattro = instancemethod_getattro,
|
|
|
|
.tp_setattro = PyObject_GenericSetAttr,
|
|
|
|
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
|
|
|
|
.tp_doc = instancemethod_new__doc__,
|
|
|
|
.tp_traverse = instancemethod_traverse,
|
|
|
|
.tp_richcompare = instancemethod_richcompare,
|
|
|
|
.tp_members = instancemethod_memberlist,
|
|
|
|
.tp_getset = instancemethod_getset,
|
|
|
|
.tp_descr_get = instancemethod_descr_get,
|
|
|
|
.tp_new = instancemethod_new,
|
2007-12-11 15:56:40 -04:00
|
|
|
};
|