Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 1 | /* Class object implementation (dead now except for methods) */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3 | #include "Python.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4 | #include "structmember.h" |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 5 | |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 6 | #define TP_DESCR_GET(t) ((t)->tp_descr_get) |
Guido van Rossum | 915f0eb | 2001-10-17 20:26:38 +0000 | [diff] [blame] | 7 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 8 | /* Free list for method objects to safe malloc/free overhead |
| 9 | * The im_self element is used to chain the elements. |
| 10 | */ |
| 11 | static PyMethodObject *free_list; |
| 12 | static int numfree = 0; |
| 13 | #ifndef PyMethod_MAXFREELIST |
| 14 | #define PyMethod_MAXFREELIST 256 |
| 15 | #endif |
| 16 | |
Guido van Rossum | b479dc5 | 2001-09-05 22:52:50 +0000 | [diff] [blame] | 17 | PyObject * |
| 18 | PyMethod_Function(PyObject *im) |
| 19 | { |
| 20 | if (!PyMethod_Check(im)) { |
| 21 | PyErr_BadInternalCall(); |
| 22 | return NULL; |
| 23 | } |
| 24 | return ((PyMethodObject *)im)->im_func; |
| 25 | } |
| 26 | |
| 27 | PyObject * |
| 28 | PyMethod_Self(PyObject *im) |
| 29 | { |
| 30 | if (!PyMethod_Check(im)) { |
| 31 | PyErr_BadInternalCall(); |
| 32 | return NULL; |
| 33 | } |
| 34 | return ((PyMethodObject *)im)->im_self; |
| 35 | } |
| 36 | |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 37 | /* Method objects are used for bound instance methods returned by |
| 38 | instancename.methodname. ClassName.methodname returns an ordinary |
| 39 | function. |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 40 | */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 41 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 42 | PyObject * |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 43 | PyMethod_New(PyObject *func, PyObject *self) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 44 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 45 | register PyMethodObject *im; |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 46 | if (self == NULL) { |
| 47 | PyErr_BadInternalCall(); |
| 48 | return NULL; |
| 49 | } |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 50 | im = free_list; |
| 51 | if (im != NULL) { |
| 52 | free_list = (PyMethodObject *)(im->im_self); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 53 | PyObject_INIT(im, &PyMethod_Type); |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 54 | numfree--; |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 55 | } |
| 56 | else { |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 57 | im = PyObject_GC_New(PyMethodObject, &PyMethod_Type); |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 58 | if (im == NULL) |
| 59 | return NULL; |
| 60 | } |
Fred Drake | db81e8d | 2001-03-23 04:19:27 +0000 | [diff] [blame] | 61 | im->im_weakreflist = NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 62 | Py_INCREF(func); |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 63 | im->im_func = func; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 64 | Py_XINCREF(self); |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 65 | im->im_self = self; |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 66 | _PyObject_GC_TRACK(im); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 67 | return (PyObject *)im; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 70 | /* Descriptors for PyMethod attributes */ |
| 71 | |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 72 | /* im_func and im_self are stored in the PyMethod object */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 73 | |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 74 | #define MO_OFF(x) offsetof(PyMethodObject, x) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 75 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 76 | static PyMemberDef method_memberlist[] = { |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 77 | {"__func__", T_OBJECT, MO_OFF(im_func), READONLY|RESTRICTED, |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 78 | "the function (or other callable) implementing a method"}, |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 79 | {"__self__", T_OBJECT, MO_OFF(im_self), READONLY|RESTRICTED, |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 80 | "the instance to which a method is bound"}, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 81 | {NULL} /* Sentinel */ |
| 82 | }; |
| 83 | |
Guido van Rossum | baf0f8f | 2003-11-22 23:55:50 +0000 | [diff] [blame] | 84 | /* Christian Tismer argued convincingly that method attributes should |
| 85 | (nearly) always override function attributes. |
| 86 | The one exception is __doc__; there's a default __doc__ which |
| 87 | should only be used for the class, not for instances */ |
| 88 | |
| 89 | static PyObject * |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 90 | method_get_doc(PyMethodObject *im, void *context) |
Guido van Rossum | baf0f8f | 2003-11-22 23:55:50 +0000 | [diff] [blame] | 91 | { |
| 92 | static PyObject *docstr; |
| 93 | if (docstr == NULL) { |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 94 | docstr= PyUnicode_InternFromString("__doc__"); |
Guido van Rossum | baf0f8f | 2003-11-22 23:55:50 +0000 | [diff] [blame] | 95 | if (docstr == NULL) |
| 96 | return NULL; |
| 97 | } |
| 98 | return PyObject_GetAttr(im->im_func, docstr); |
| 99 | } |
| 100 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 101 | static PyGetSetDef method_getset[] = { |
| 102 | {"__doc__", (getter)method_get_doc, NULL, NULL}, |
Guido van Rossum | baf0f8f | 2003-11-22 23:55:50 +0000 | [diff] [blame] | 103 | {0} |
| 104 | }; |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 105 | |
| 106 | static PyObject * |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 107 | method_getattro(PyObject *obj, PyObject *name) |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 108 | { |
| 109 | PyMethodObject *im = (PyMethodObject *)obj; |
| 110 | PyTypeObject *tp = obj->ob_type; |
Guido van Rossum | baf0f8f | 2003-11-22 23:55:50 +0000 | [diff] [blame] | 111 | PyObject *descr = NULL; |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 112 | |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 113 | { |
Guido van Rossum | 915f0eb | 2001-10-17 20:26:38 +0000 | [diff] [blame] | 114 | if (tp->tp_dict == NULL) { |
| 115 | if (PyType_Ready(tp) < 0) |
| 116 | return NULL; |
| 117 | } |
| 118 | descr = _PyType_Lookup(tp, name); |
Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 119 | } |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 120 | |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 121 | if (descr != NULL) { |
Guido van Rossum | baf0f8f | 2003-11-22 23:55:50 +0000 | [diff] [blame] | 122 | descrgetfunc f = TP_DESCR_GET(descr->ob_type); |
| 123 | if (f != NULL) |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 124 | return f(descr, obj, (PyObject *)obj->ob_type); |
Guido van Rossum | baf0f8f | 2003-11-22 23:55:50 +0000 | [diff] [blame] | 125 | else { |
| 126 | Py_INCREF(descr); |
| 127 | return descr; |
| 128 | } |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Guido van Rossum | baf0f8f | 2003-11-22 23:55:50 +0000 | [diff] [blame] | 131 | return PyObject_GetAttr(im->im_func, name); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 134 | PyDoc_STRVAR(method_doc, |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 135 | "method(function, instance)\n\ |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 136 | \n\ |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 137 | Create a bound instance method object."); |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 138 | |
| 139 | static PyObject * |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 140 | method_new(PyTypeObject* type, PyObject* args, PyObject *kw) |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 141 | { |
| 142 | PyObject *func; |
| 143 | PyObject *self; |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 144 | |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 145 | if (!_PyArg_NoKeywords("method", kw)) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 146 | return NULL; |
Christian Heimes | 9bbac50 | 2007-12-08 18:27:16 +0000 | [diff] [blame] | 147 | if (!PyArg_UnpackTuple(args, "method", 2, 2, |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 148 | &func, &self)) |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 149 | return NULL; |
| 150 | if (!PyCallable_Check(func)) { |
| 151 | PyErr_SetString(PyExc_TypeError, |
| 152 | "first argument must be callable"); |
| 153 | return NULL; |
| 154 | } |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 155 | if (self == NULL || self == Py_None) { |
Michael W. Hudson | e2749cb | 2005-03-30 16:32:10 +0000 | [diff] [blame] | 156 | PyErr_SetString(PyExc_TypeError, |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 157 | "self must not be None"); |
Michael W. Hudson | e2749cb | 2005-03-30 16:32:10 +0000 | [diff] [blame] | 158 | return NULL; |
| 159 | } |
| 160 | |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 161 | return PyMethod_New(func, self); |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 164 | static void |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 165 | method_dealloc(register PyMethodObject *im) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 166 | { |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 167 | _PyObject_GC_UNTRACK(im); |
Fred Drake | c916f5a | 2001-10-26 17:56:51 +0000 | [diff] [blame] | 168 | if (im->im_weakreflist != NULL) |
| 169 | PyObject_ClearWeakRefs((PyObject *)im); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 170 | Py_DECREF(im->im_func); |
| 171 | Py_XDECREF(im->im_self); |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 172 | if (numfree < PyMethod_MAXFREELIST) { |
| 173 | im->im_self = (PyObject *)free_list; |
| 174 | free_list = im; |
| 175 | numfree++; |
| 176 | } |
| 177 | else { |
| 178 | PyObject_GC_Del(im); |
| 179 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 182 | static PyObject * |
| 183 | method_richcompare(PyObject *self, PyObject *other, int op) |
Guido van Rossum | ebc8c51 | 1992-09-03 20:39:51 +0000 | [diff] [blame] | 184 | { |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 185 | PyMethodObject *a, *b; |
| 186 | PyObject *res; |
| 187 | int eq; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 188 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 189 | if ((op != Py_EQ && op != Py_NE) || |
| 190 | !PyMethod_Check(self) || |
| 191 | !PyMethod_Check(other)) |
| 192 | { |
| 193 | Py_INCREF(Py_NotImplemented); |
| 194 | return Py_NotImplemented; |
| 195 | } |
| 196 | a = (PyMethodObject *)self; |
| 197 | b = (PyMethodObject *)other; |
| 198 | eq = PyObject_RichCompareBool(a->im_func, b->im_func, Py_EQ); |
| 199 | if (eq == 1) { |
| 200 | if (a->im_self == NULL || b->im_self == NULL) |
| 201 | eq = a->im_self == b->im_self; |
| 202 | else |
| 203 | eq = PyObject_RichCompareBool(a->im_self, b->im_self, |
| 204 | Py_EQ); |
| 205 | } |
| 206 | if (eq < 0) |
| 207 | return NULL; |
| 208 | if (op == Py_EQ) |
| 209 | res = eq ? Py_True : Py_False; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 210 | else |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 211 | res = eq ? Py_False : Py_True; |
| 212 | Py_INCREF(res); |
| 213 | return res; |
Guido van Rossum | ebc8c51 | 1992-09-03 20:39:51 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 216 | static PyObject * |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 217 | method_repr(PyMethodObject *a) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 218 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 219 | PyObject *self = a->im_self; |
Guido van Rossum | 7859f87 | 1998-07-08 14:58:16 +0000 | [diff] [blame] | 220 | PyObject *func = a->im_func; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 221 | PyObject *klass = (PyObject*)Py_TYPE(self); |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 222 | PyObject *funcname = NULL ,*klassname = NULL, *result = NULL; |
Walter Dörwald | d7fb764 | 2007-06-11 16:36:59 +0000 | [diff] [blame] | 223 | char *defname = "?"; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 224 | |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 225 | if (self == NULL) { |
| 226 | PyErr_BadInternalCall(); |
| 227 | return NULL; |
| 228 | } |
| 229 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 230 | funcname = PyObject_GetAttrString(func, "__name__"); |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 231 | if (funcname == NULL) { |
| 232 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 233 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 234 | PyErr_Clear(); |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 235 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 236 | else if (!PyUnicode_Check(funcname)) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 237 | Py_DECREF(funcname); |
| 238 | funcname = NULL; |
Guido van Rossum | 7859f87 | 1998-07-08 14:58:16 +0000 | [diff] [blame] | 239 | } |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 240 | |
Guido van Rossum | 4066769 | 2001-08-17 13:59:27 +0000 | [diff] [blame] | 241 | if (klass == NULL) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 242 | klassname = NULL; |
Guido van Rossum | 4066769 | 2001-08-17 13:59:27 +0000 | [diff] [blame] | 243 | else { |
| 244 | klassname = PyObject_GetAttrString(klass, "__name__"); |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 245 | if (klassname == NULL) { |
| 246 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 247 | return NULL; |
Guido van Rossum | 4066769 | 2001-08-17 13:59:27 +0000 | [diff] [blame] | 248 | PyErr_Clear(); |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 249 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 250 | else if (!PyUnicode_Check(klassname)) { |
Guido van Rossum | 4066769 | 2001-08-17 13:59:27 +0000 | [diff] [blame] | 251 | Py_DECREF(klassname); |
| 252 | klassname = NULL; |
| 253 | } |
Guido van Rossum | 7859f87 | 1998-07-08 14:58:16 +0000 | [diff] [blame] | 254 | } |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 255 | |
| 256 | /* XXX Shouldn't use repr()/%R here! */ |
| 257 | result = PyUnicode_FromFormat("<bound method %V.%V of %R>", |
| 258 | klassname, defname, |
| 259 | funcname, defname, self); |
| 260 | |
Guido van Rossum | 42636dc | 1999-10-11 14:03:12 +0000 | [diff] [blame] | 261 | Py_XDECREF(funcname); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 262 | Py_XDECREF(klassname); |
| 263 | return result; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 266 | static long |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 267 | method_hash(PyMethodObject *a) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 268 | { |
| 269 | long x, y; |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 270 | if (a->im_self == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 271 | x = PyObject_Hash(Py_None); |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 272 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 273 | x = PyObject_Hash(a->im_self); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 274 | if (x == -1) |
| 275 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 276 | y = PyObject_Hash(a->im_func); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 277 | if (y == -1) |
| 278 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 279 | x = x ^ y; |
| 280 | if (x == -1) |
| 281 | x = -2; |
| 282 | return x; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 285 | static int |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 286 | method_traverse(PyMethodObject *im, visitproc visit, void *arg) |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 287 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 288 | Py_VISIT(im->im_func); |
| 289 | Py_VISIT(im->im_self); |
Jeremy Hylton | d22162b | 2000-06-23 17:14:56 +0000 | [diff] [blame] | 290 | return 0; |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 293 | static PyObject * |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 294 | method_call(PyObject *func, PyObject *arg, PyObject *kw) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 295 | { |
| 296 | PyObject *self = PyMethod_GET_SELF(func); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 297 | PyObject *result; |
| 298 | |
| 299 | func = PyMethod_GET_FUNCTION(func); |
| 300 | if (self == NULL) { |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 301 | PyErr_BadInternalCall(); |
| 302 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 303 | } |
| 304 | else { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 305 | Py_ssize_t argcount = PyTuple_Size(arg); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 306 | PyObject *newarg = PyTuple_New(argcount + 1); |
| 307 | int i; |
| 308 | if (newarg == NULL) |
| 309 | return NULL; |
| 310 | Py_INCREF(self); |
| 311 | PyTuple_SET_ITEM(newarg, 0, self); |
| 312 | for (i = 0; i < argcount; i++) { |
| 313 | PyObject *v = PyTuple_GET_ITEM(arg, i); |
| 314 | Py_XINCREF(v); |
| 315 | PyTuple_SET_ITEM(newarg, i+1, v); |
| 316 | } |
| 317 | arg = newarg; |
| 318 | } |
| 319 | result = PyObject_Call((PyObject *)func, arg, kw); |
| 320 | Py_DECREF(arg); |
| 321 | return result; |
| 322 | } |
| 323 | |
Guido van Rossum | 23cc2b4 | 2001-08-15 17:52:31 +0000 | [diff] [blame] | 324 | static PyObject * |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 325 | method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls) |
Guido van Rossum | 23cc2b4 | 2001-08-15 17:52:31 +0000 | [diff] [blame] | 326 | { |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 327 | /* Don't rebind an already bound method of a class that's not a base |
| 328 | class of cls. */ |
Guido van Rossum | 6bae46d | 2003-02-11 18:43:00 +0000 | [diff] [blame] | 329 | if (PyMethod_GET_SELF(meth) != NULL) { |
| 330 | /* Already bound */ |
Guido van Rossum | 501c7c7 | 2001-08-16 20:41:56 +0000 | [diff] [blame] | 331 | Py_INCREF(meth); |
| 332 | return meth; |
| 333 | } |
Guido van Rossum | 6bae46d | 2003-02-11 18:43:00 +0000 | [diff] [blame] | 334 | /* Bind it to obj */ |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 335 | return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj); |
Guido van Rossum | 23cc2b4 | 2001-08-15 17:52:31 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 338 | PyTypeObject PyMethod_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 339 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 340 | "method", |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 341 | sizeof(PyMethodObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 342 | 0, |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 343 | (destructor)method_dealloc, /* tp_dealloc */ |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 344 | 0, /* tp_print */ |
| 345 | 0, /* tp_getattr */ |
| 346 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 347 | 0, /* tp_reserved */ |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 348 | (reprfunc)method_repr, /* tp_repr */ |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 349 | 0, /* tp_as_number */ |
| 350 | 0, /* tp_as_sequence */ |
| 351 | 0, /* tp_as_mapping */ |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 352 | (hashfunc)method_hash, /* tp_hash */ |
| 353 | method_call, /* tp_call */ |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 354 | 0, /* tp_str */ |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 355 | method_getattro, /* tp_getattro */ |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 356 | PyObject_GenericSetAttr, /* tp_setattro */ |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 357 | 0, /* tp_as_buffer */ |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 358 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 359 | method_doc, /* tp_doc */ |
| 360 | (traverseproc)method_traverse, /* tp_traverse */ |
Fred Drake | db81e8d | 2001-03-23 04:19:27 +0000 | [diff] [blame] | 361 | 0, /* tp_clear */ |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 362 | method_richcompare, /* tp_richcompare */ |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 363 | offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */ |
Guido van Rossum | 23cc2b4 | 2001-08-15 17:52:31 +0000 | [diff] [blame] | 364 | 0, /* tp_iter */ |
| 365 | 0, /* tp_iternext */ |
| 366 | 0, /* tp_methods */ |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 367 | method_memberlist, /* tp_members */ |
| 368 | method_getset, /* tp_getset */ |
Guido van Rossum | 23cc2b4 | 2001-08-15 17:52:31 +0000 | [diff] [blame] | 369 | 0, /* tp_base */ |
| 370 | 0, /* tp_dict */ |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 371 | method_descr_get, /* tp_descr_get */ |
Guido van Rossum | 23cc2b4 | 2001-08-15 17:52:31 +0000 | [diff] [blame] | 372 | 0, /* tp_descr_set */ |
| 373 | 0, /* tp_dictoffset */ |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 374 | 0, /* tp_init */ |
| 375 | 0, /* tp_alloc */ |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 376 | method_new, /* tp_new */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 377 | }; |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 378 | |
| 379 | /* Clear out the free list */ |
| 380 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 381 | int |
| 382 | PyMethod_ClearFreeList(void) |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 383 | { |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 384 | int freelist_size = numfree; |
| 385 | |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 386 | while (free_list) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 387 | PyMethodObject *im = free_list; |
| 388 | free_list = (PyMethodObject *)(im->im_self); |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 389 | PyObject_GC_Del(im); |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 390 | numfree--; |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 391 | } |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 392 | assert(numfree == 0); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 393 | return freelist_size; |
| 394 | } |
| 395 | |
| 396 | void |
| 397 | PyMethod_Fini(void) |
| 398 | { |
| 399 | (void)PyMethod_ClearFreeList(); |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 400 | } |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 401 | |
| 402 | /* ------------------------------------------------------------------------ |
| 403 | * instance method |
| 404 | */ |
| 405 | |
| 406 | PyObject * |
| 407 | PyInstanceMethod_New(PyObject *func) { |
| 408 | PyInstanceMethodObject *method; |
| 409 | method = PyObject_GC_New(PyInstanceMethodObject, |
| 410 | &PyInstanceMethod_Type); |
| 411 | if (method == NULL) return NULL; |
| 412 | Py_INCREF(func); |
| 413 | method->func = func; |
| 414 | _PyObject_GC_TRACK(method); |
| 415 | return (PyObject *)method; |
| 416 | } |
| 417 | |
| 418 | PyObject * |
| 419 | PyInstanceMethod_Function(PyObject *im) |
| 420 | { |
| 421 | if (!PyInstanceMethod_Check(im)) { |
| 422 | PyErr_BadInternalCall(); |
| 423 | return NULL; |
| 424 | } |
| 425 | return PyInstanceMethod_GET_FUNCTION(im); |
| 426 | } |
| 427 | |
| 428 | #define IMO_OFF(x) offsetof(PyInstanceMethodObject, x) |
| 429 | |
| 430 | static PyMemberDef instancemethod_memberlist[] = { |
| 431 | {"__func__", T_OBJECT, IMO_OFF(func), READONLY|RESTRICTED, |
| 432 | "the function (or other callable) implementing a method"}, |
| 433 | {NULL} /* Sentinel */ |
| 434 | }; |
| 435 | |
| 436 | static PyObject * |
| 437 | instancemethod_get_doc(PyObject *self, void *context) |
| 438 | { |
| 439 | static PyObject *docstr; |
| 440 | if (docstr == NULL) { |
| 441 | docstr = PyUnicode_InternFromString("__doc__"); |
| 442 | if (docstr == NULL) |
| 443 | return NULL; |
| 444 | } |
| 445 | return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), docstr); |
| 446 | } |
| 447 | |
| 448 | static PyGetSetDef instancemethod_getset[] = { |
| 449 | {"__doc__", (getter)instancemethod_get_doc, NULL, NULL}, |
| 450 | {0} |
| 451 | }; |
| 452 | |
| 453 | static PyObject * |
| 454 | instancemethod_getattro(PyObject *self, PyObject *name) |
| 455 | { |
| 456 | PyTypeObject *tp = self->ob_type; |
| 457 | PyObject *descr = NULL; |
| 458 | |
| 459 | if (tp->tp_dict == NULL) { |
| 460 | if (PyType_Ready(tp) < 0) |
| 461 | return NULL; |
| 462 | } |
| 463 | descr = _PyType_Lookup(tp, name); |
| 464 | |
| 465 | if (descr != NULL) { |
| 466 | descrgetfunc f = TP_DESCR_GET(descr->ob_type); |
| 467 | if (f != NULL) |
| 468 | return f(descr, self, (PyObject *)self->ob_type); |
| 469 | else { |
| 470 | Py_INCREF(descr); |
| 471 | return descr; |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), name); |
| 476 | } |
| 477 | |
| 478 | static void |
| 479 | instancemethod_dealloc(PyObject *self) { |
| 480 | _PyObject_GC_UNTRACK(self); |
| 481 | Py_DECREF(PyInstanceMethod_GET_FUNCTION(self)); |
| 482 | PyObject_GC_Del(self); |
| 483 | } |
| 484 | |
| 485 | static int |
| 486 | instancemethod_traverse(PyObject *self, visitproc visit, void *arg) { |
| 487 | Py_VISIT(PyInstanceMethod_GET_FUNCTION(self)); |
| 488 | return 0; |
| 489 | } |
| 490 | |
| 491 | static PyObject * |
| 492 | instancemethod_call(PyObject *self, PyObject *arg, PyObject *kw) |
| 493 | { |
| 494 | return PyObject_Call(PyMethod_GET_FUNCTION(self), arg, kw); |
| 495 | } |
| 496 | |
| 497 | static PyObject * |
| 498 | instancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) { |
| 499 | register PyObject *func = PyInstanceMethod_GET_FUNCTION(descr); |
Georg Brandl | c9b0953 | 2008-05-25 09:24:38 +0000 | [diff] [blame] | 500 | if (obj == NULL) { |
| 501 | Py_INCREF(func); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 502 | return func; |
Georg Brandl | c9b0953 | 2008-05-25 09:24:38 +0000 | [diff] [blame] | 503 | } |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 504 | else |
| 505 | return PyMethod_New(func, obj); |
| 506 | } |
| 507 | |
| 508 | static PyObject * |
| 509 | instancemethod_richcompare(PyObject *self, PyObject *other, int op) |
| 510 | { |
| 511 | PyInstanceMethodObject *a, *b; |
| 512 | PyObject *res; |
| 513 | int eq; |
| 514 | |
| 515 | if ((op != Py_EQ && op != Py_NE) || |
| 516 | !PyInstanceMethod_Check(self) || |
| 517 | !PyInstanceMethod_Check(other)) |
| 518 | { |
| 519 | Py_INCREF(Py_NotImplemented); |
| 520 | return Py_NotImplemented; |
| 521 | } |
| 522 | a = (PyInstanceMethodObject *)self; |
| 523 | b = (PyInstanceMethodObject *)other; |
| 524 | eq = PyObject_RichCompareBool(a->func, b->func, Py_EQ); |
| 525 | if (eq < 0) |
| 526 | return NULL; |
| 527 | if (op == Py_EQ) |
| 528 | res = eq ? Py_True : Py_False; |
| 529 | else |
| 530 | res = eq ? Py_False : Py_True; |
| 531 | Py_INCREF(res); |
| 532 | return res; |
| 533 | } |
| 534 | |
| 535 | static PyObject * |
| 536 | instancemethod_repr(PyObject *self) |
| 537 | { |
| 538 | PyObject *func = PyInstanceMethod_Function(self); |
| 539 | PyObject *funcname = NULL , *result = NULL; |
| 540 | char *defname = "?"; |
| 541 | |
| 542 | if (func == NULL) { |
| 543 | PyErr_BadInternalCall(); |
| 544 | return NULL; |
| 545 | } |
| 546 | |
| 547 | funcname = PyObject_GetAttrString(func, "__name__"); |
| 548 | if (funcname == NULL) { |
| 549 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 550 | return NULL; |
| 551 | PyErr_Clear(); |
| 552 | } |
| 553 | else if (!PyUnicode_Check(funcname)) { |
| 554 | Py_DECREF(funcname); |
| 555 | funcname = NULL; |
| 556 | } |
| 557 | |
| 558 | result = PyUnicode_FromFormat("<instancemethod %V at %p>", |
| 559 | funcname, defname, self); |
| 560 | |
| 561 | Py_XDECREF(funcname); |
| 562 | return result; |
| 563 | } |
| 564 | |
| 565 | /* |
| 566 | static long |
| 567 | instancemethod_hash(PyObject *self) |
| 568 | { |
| 569 | long x, y; |
| 570 | x = (long)self; |
| 571 | y = PyObject_Hash(PyInstanceMethod_GET_FUNCTION(self)); |
| 572 | if (y == -1) |
| 573 | return -1; |
| 574 | x = x ^ y; |
| 575 | if (x == -1) |
| 576 | x = -2; |
| 577 | return x; |
| 578 | } |
| 579 | */ |
| 580 | |
| 581 | PyDoc_STRVAR(instancemethod_doc, |
| 582 | "instancemethod(function)\n\ |
| 583 | \n\ |
| 584 | Bind a function to a class."); |
| 585 | |
| 586 | static PyObject * |
| 587 | instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw) |
| 588 | { |
| 589 | PyObject *func; |
| 590 | |
| 591 | if (!_PyArg_NoKeywords("instancemethod", kw)) |
| 592 | return NULL; |
| 593 | if (!PyArg_UnpackTuple(args, "instancemethod", 1, 1, &func)) |
| 594 | return NULL; |
| 595 | if (!PyCallable_Check(func)) { |
| 596 | PyErr_SetString(PyExc_TypeError, |
| 597 | "first argument must be callable"); |
| 598 | return NULL; |
| 599 | } |
| 600 | |
| 601 | return PyInstanceMethod_New(func); |
| 602 | } |
| 603 | |
| 604 | PyTypeObject PyInstanceMethod_Type = { |
| 605 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 606 | "instancemethod", /* tp_name */ |
| 607 | sizeof(PyInstanceMethodObject), /* tp_basicsize */ |
| 608 | 0, /* tp_itemsize */ |
| 609 | instancemethod_dealloc, /* tp_dealloc */ |
| 610 | 0, /* tp_print */ |
| 611 | 0, /* tp_getattr */ |
| 612 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 613 | 0, /* tp_reserved */ |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 614 | (reprfunc)instancemethod_repr, /* tp_repr */ |
| 615 | 0, /* tp_as_number */ |
| 616 | 0, /* tp_as_sequence */ |
| 617 | 0, /* tp_as_mapping */ |
| 618 | 0, /*(hashfunc)instancemethod_hash, tp_hash */ |
| 619 | instancemethod_call, /* tp_call */ |
| 620 | 0, /* tp_str */ |
| 621 | instancemethod_getattro, /* tp_getattro */ |
| 622 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 623 | 0, /* tp_as_buffer */ |
| 624 | Py_TPFLAGS_DEFAULT |
| 625 | | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 626 | instancemethod_doc, /* tp_doc */ |
| 627 | instancemethod_traverse, /* tp_traverse */ |
| 628 | 0, /* tp_clear */ |
| 629 | instancemethod_richcompare, /* tp_richcompare */ |
| 630 | 0, /* tp_weaklistoffset */ |
| 631 | 0, /* tp_iter */ |
| 632 | 0, /* tp_iternext */ |
| 633 | 0, /* tp_methods */ |
| 634 | instancemethod_memberlist, /* tp_members */ |
| 635 | instancemethod_getset, /* tp_getset */ |
| 636 | 0, /* tp_base */ |
| 637 | 0, /* tp_dict */ |
| 638 | instancemethod_descr_get, /* tp_descr_get */ |
| 639 | 0, /* tp_descr_set */ |
| 640 | 0, /* tp_dictoffset */ |
| 641 | 0, /* tp_init */ |
| 642 | 0, /* tp_alloc */ |
| 643 | instancemethod_new, /* tp_new */ |
| 644 | }; |