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 | |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 8 | |
Guido van Rossum | b479dc5 | 2001-09-05 22:52:50 +0000 | [diff] [blame] | 9 | PyObject * |
| 10 | PyMethod_Function(PyObject *im) |
| 11 | { |
| 12 | if (!PyMethod_Check(im)) { |
| 13 | PyErr_BadInternalCall(); |
| 14 | return NULL; |
| 15 | } |
| 16 | return ((PyMethodObject *)im)->im_func; |
| 17 | } |
| 18 | |
| 19 | PyObject * |
| 20 | PyMethod_Self(PyObject *im) |
| 21 | { |
| 22 | if (!PyMethod_Check(im)) { |
| 23 | PyErr_BadInternalCall(); |
| 24 | return NULL; |
| 25 | } |
| 26 | return ((PyMethodObject *)im)->im_self; |
| 27 | } |
| 28 | |
| 29 | PyObject * |
| 30 | PyMethod_Class(PyObject *im) |
| 31 | { |
| 32 | if (!PyMethod_Check(im)) { |
| 33 | PyErr_BadInternalCall(); |
| 34 | return NULL; |
| 35 | } |
| 36 | return ((PyMethodObject *)im)->im_class; |
| 37 | } |
| 38 | |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 39 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 40 | /* Method objects are used for two purposes: |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 41 | (a) as bound instance methods (returned by instancename.methodname) |
| 42 | (b) as unbound methods (returned by ClassName.methodname) |
| 43 | In case (b), im_self is NULL |
| 44 | */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 45 | |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 46 | static PyMethodObject *free_list; |
| 47 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 48 | PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 49 | PyMethod_New(PyObject *func, PyObject *self, PyObject *klass) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 50 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 51 | register PyMethodObject *im; |
Guido van Rossum | 7859f87 | 1998-07-08 14:58:16 +0000 | [diff] [blame] | 52 | if (!PyCallable_Check(func)) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 53 | PyErr_BadInternalCall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 54 | return NULL; |
| 55 | } |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 56 | im = free_list; |
| 57 | if (im != NULL) { |
| 58 | free_list = (PyMethodObject *)(im->im_self); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 59 | PyObject_INIT(im, &PyMethod_Type); |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 60 | } |
| 61 | else { |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 62 | im = PyObject_GC_New(PyMethodObject, &PyMethod_Type); |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 63 | if (im == NULL) |
| 64 | return NULL; |
| 65 | } |
Fred Drake | db81e8d | 2001-03-23 04:19:27 +0000 | [diff] [blame] | 66 | im->im_weakreflist = NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 67 | Py_INCREF(func); |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 68 | im->im_func = func; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 69 | Py_XINCREF(self); |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 70 | im->im_self = self; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 71 | Py_XINCREF(klass); |
| 72 | im->im_class = klass; |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 73 | _PyObject_GC_TRACK(im); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 74 | return (PyObject *)im; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 77 | /* Descriptors for PyMethod attributes */ |
| 78 | |
| 79 | /* im_class, im_func and im_self are stored in the PyMethod object */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 80 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 81 | #define OFF(x) offsetof(PyMethodObject, x) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 82 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 83 | static PyMemberDef method_memberlist[] = { |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 84 | {"im_class", T_OBJECT, OFF(im_class), READONLY|RESTRICTED, |
| 85 | "the class associated with a method"}, |
| 86 | {"im_func", T_OBJECT, OFF(im_func), READONLY|RESTRICTED, |
| 87 | "the function (or other callable) implementing a method"}, |
| 88 | {"im_self", T_OBJECT, OFF(im_self), READONLY|RESTRICTED, |
| 89 | "the instance to which a method is bound; None for unbound methods"}, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 90 | {NULL} /* Sentinel */ |
| 91 | }; |
| 92 | |
Guido van Rossum | baf0f8f | 2003-11-22 23:55:50 +0000 | [diff] [blame] | 93 | /* Christian Tismer argued convincingly that method attributes should |
| 94 | (nearly) always override function attributes. |
| 95 | The one exception is __doc__; there's a default __doc__ which |
| 96 | should only be used for the class, not for instances */ |
| 97 | |
| 98 | static PyObject * |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 99 | method_get_doc(PyMethodObject *im, void *context) |
Guido van Rossum | baf0f8f | 2003-11-22 23:55:50 +0000 | [diff] [blame] | 100 | { |
| 101 | static PyObject *docstr; |
| 102 | if (docstr == NULL) { |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 103 | docstr= PyUnicode_InternFromString("__doc__"); |
Guido van Rossum | baf0f8f | 2003-11-22 23:55:50 +0000 | [diff] [blame] | 104 | if (docstr == NULL) |
| 105 | return NULL; |
| 106 | } |
| 107 | return PyObject_GetAttr(im->im_func, docstr); |
| 108 | } |
| 109 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 110 | static PyGetSetDef method_getset[] = { |
| 111 | {"__doc__", (getter)method_get_doc, NULL, NULL}, |
Guido van Rossum | baf0f8f | 2003-11-22 23:55:50 +0000 | [diff] [blame] | 112 | {0} |
| 113 | }; |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 114 | |
| 115 | static PyObject * |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 116 | method_getattro(PyObject *obj, PyObject *name) |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 117 | { |
| 118 | PyMethodObject *im = (PyMethodObject *)obj; |
| 119 | PyTypeObject *tp = obj->ob_type; |
Guido van Rossum | baf0f8f | 2003-11-22 23:55:50 +0000 | [diff] [blame] | 120 | PyObject *descr = NULL; |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 121 | |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 122 | { |
Guido van Rossum | 915f0eb | 2001-10-17 20:26:38 +0000 | [diff] [blame] | 123 | if (tp->tp_dict == NULL) { |
| 124 | if (PyType_Ready(tp) < 0) |
| 125 | return NULL; |
| 126 | } |
| 127 | descr = _PyType_Lookup(tp, name); |
Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 128 | } |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 129 | |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 130 | if (descr != NULL) { |
Guido van Rossum | baf0f8f | 2003-11-22 23:55:50 +0000 | [diff] [blame] | 131 | descrgetfunc f = TP_DESCR_GET(descr->ob_type); |
| 132 | if (f != NULL) |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 133 | return f(descr, obj, (PyObject *)obj->ob_type); |
Guido van Rossum | baf0f8f | 2003-11-22 23:55:50 +0000 | [diff] [blame] | 134 | else { |
| 135 | Py_INCREF(descr); |
| 136 | return descr; |
| 137 | } |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Guido van Rossum | baf0f8f | 2003-11-22 23:55:50 +0000 | [diff] [blame] | 140 | return PyObject_GetAttr(im->im_func, name); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 143 | PyDoc_STRVAR(method_doc, |
| 144 | "method(function, instance, class)\n\ |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 145 | \n\ |
| 146 | Create an instance method object."); |
| 147 | |
| 148 | static PyObject * |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 149 | method_new(PyTypeObject* type, PyObject* args, PyObject *kw) |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 150 | { |
| 151 | PyObject *func; |
| 152 | PyObject *self; |
Guido van Rossum | 2fb9fdc | 2003-04-09 19:35:08 +0000 | [diff] [blame] | 153 | PyObject *classObj = NULL; |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 154 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 155 | if (!_PyArg_NoKeywords("instancemethod", kw)) |
| 156 | return NULL; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 157 | if (!PyArg_UnpackTuple(args, "method", 2, 3, |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 158 | &func, &self, &classObj)) |
| 159 | return NULL; |
| 160 | if (!PyCallable_Check(func)) { |
| 161 | PyErr_SetString(PyExc_TypeError, |
| 162 | "first argument must be callable"); |
| 163 | return NULL; |
| 164 | } |
| 165 | if (self == Py_None) |
| 166 | self = NULL; |
Michael W. Hudson | e2749cb | 2005-03-30 16:32:10 +0000 | [diff] [blame] | 167 | if (self == NULL && classObj == NULL) { |
| 168 | PyErr_SetString(PyExc_TypeError, |
| 169 | "unbound methods must have non-NULL im_class"); |
| 170 | return NULL; |
| 171 | } |
| 172 | |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 173 | return PyMethod_New(func, self, classObj); |
| 174 | } |
| 175 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 176 | static void |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 177 | method_dealloc(register PyMethodObject *im) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 178 | { |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 179 | _PyObject_GC_UNTRACK(im); |
Fred Drake | c916f5a | 2001-10-26 17:56:51 +0000 | [diff] [blame] | 180 | if (im->im_weakreflist != NULL) |
| 181 | PyObject_ClearWeakRefs((PyObject *)im); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 182 | Py_DECREF(im->im_func); |
| 183 | Py_XDECREF(im->im_self); |
Guido van Rossum | cdf0d75 | 2001-08-17 12:07:34 +0000 | [diff] [blame] | 184 | Py_XDECREF(im->im_class); |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 185 | im->im_self = (PyObject *)free_list; |
| 186 | free_list = im; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 189 | static PyObject * |
| 190 | method_richcompare(PyObject *self, PyObject *other, int op) |
Guido van Rossum | ebc8c51 | 1992-09-03 20:39:51 +0000 | [diff] [blame] | 191 | { |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 192 | PyMethodObject *a, *b; |
| 193 | PyObject *res; |
| 194 | int eq; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 195 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 196 | if ((op != Py_EQ && op != Py_NE) || |
| 197 | !PyMethod_Check(self) || |
| 198 | !PyMethod_Check(other)) |
| 199 | { |
| 200 | Py_INCREF(Py_NotImplemented); |
| 201 | return Py_NotImplemented; |
| 202 | } |
| 203 | a = (PyMethodObject *)self; |
| 204 | b = (PyMethodObject *)other; |
| 205 | eq = PyObject_RichCompareBool(a->im_func, b->im_func, Py_EQ); |
| 206 | if (eq == 1) { |
| 207 | if (a->im_self == NULL || b->im_self == NULL) |
| 208 | eq = a->im_self == b->im_self; |
| 209 | else |
| 210 | eq = PyObject_RichCompareBool(a->im_self, b->im_self, |
| 211 | Py_EQ); |
| 212 | } |
| 213 | if (eq < 0) |
| 214 | return NULL; |
| 215 | if (op == Py_EQ) |
| 216 | res = eq ? Py_True : Py_False; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 217 | else |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 218 | res = eq ? Py_False : Py_True; |
| 219 | Py_INCREF(res); |
| 220 | return res; |
Guido van Rossum | ebc8c51 | 1992-09-03 20:39:51 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 223 | static PyObject * |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 224 | method_repr(PyMethodObject *a) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 225 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 226 | PyObject *self = a->im_self; |
Guido van Rossum | 7859f87 | 1998-07-08 14:58:16 +0000 | [diff] [blame] | 227 | PyObject *func = a->im_func; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 228 | PyObject *klass = a->im_class; |
| 229 | PyObject *funcname = NULL, *klassname = NULL, *result = NULL; |
Walter Dörwald | d7fb764 | 2007-06-11 16:36:59 +0000 | [diff] [blame] | 230 | char *defname = "?"; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 231 | |
| 232 | funcname = PyObject_GetAttrString(func, "__name__"); |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 233 | if (funcname == NULL) { |
| 234 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 235 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 236 | PyErr_Clear(); |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 237 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 238 | else if (!PyUnicode_Check(funcname)) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 239 | Py_DECREF(funcname); |
| 240 | funcname = NULL; |
Guido van Rossum | 7859f87 | 1998-07-08 14:58:16 +0000 | [diff] [blame] | 241 | } |
Guido van Rossum | 4066769 | 2001-08-17 13:59:27 +0000 | [diff] [blame] | 242 | if (klass == NULL) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 243 | klassname = NULL; |
Guido van Rossum | 4066769 | 2001-08-17 13:59:27 +0000 | [diff] [blame] | 244 | else { |
| 245 | klassname = PyObject_GetAttrString(klass, "__name__"); |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 246 | if (klassname == NULL) { |
| 247 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 248 | return NULL; |
Guido van Rossum | 4066769 | 2001-08-17 13:59:27 +0000 | [diff] [blame] | 249 | PyErr_Clear(); |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 250 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 251 | else if (!PyUnicode_Check(klassname)) { |
Guido van Rossum | 4066769 | 2001-08-17 13:59:27 +0000 | [diff] [blame] | 252 | Py_DECREF(klassname); |
| 253 | klassname = NULL; |
| 254 | } |
Guido van Rossum | 7859f87 | 1998-07-08 14:58:16 +0000 | [diff] [blame] | 255 | } |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 256 | if (self == NULL) |
Walter Dörwald | d7fb764 | 2007-06-11 16:36:59 +0000 | [diff] [blame] | 257 | result = PyUnicode_FromFormat("<unbound method %V.%V>", |
| 258 | klassname, defname, |
| 259 | funcname, defname); |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 260 | else { |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 261 | /* XXX Shouldn't use repr()/%R here! */ |
Walter Dörwald | d7fb764 | 2007-06-11 16:36:59 +0000 | [diff] [blame] | 262 | result = PyUnicode_FromFormat("<bound method %V.%V of %R>", |
| 263 | klassname, defname, |
| 264 | funcname, defname, self); |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 265 | } |
Guido van Rossum | 42636dc | 1999-10-11 14:03:12 +0000 | [diff] [blame] | 266 | Py_XDECREF(funcname); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 267 | Py_XDECREF(klassname); |
| 268 | return result; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 271 | static long |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 272 | method_hash(PyMethodObject *a) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 273 | { |
| 274 | long x, y; |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 275 | if (a->im_self == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 276 | x = PyObject_Hash(Py_None); |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 277 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 278 | x = PyObject_Hash(a->im_self); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 279 | if (x == -1) |
| 280 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 281 | y = PyObject_Hash(a->im_func); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 282 | if (y == -1) |
| 283 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 284 | x = x ^ y; |
| 285 | if (x == -1) |
| 286 | x = -2; |
| 287 | return x; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 290 | static int |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 291 | method_traverse(PyMethodObject *im, visitproc visit, void *arg) |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 292 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 293 | Py_VISIT(im->im_func); |
| 294 | Py_VISIT(im->im_self); |
| 295 | Py_VISIT(im->im_class); |
Jeremy Hylton | d22162b | 2000-06-23 17:14:56 +0000 | [diff] [blame] | 296 | return 0; |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 299 | static void |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 300 | getclassname(PyObject *klass, char *buf, int bufsize) |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 301 | { |
| 302 | PyObject *name; |
| 303 | |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 304 | assert(bufsize > 1); |
| 305 | strcpy(buf, "?"); /* Default outcome */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 306 | if (klass == NULL) |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 307 | return; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 308 | name = PyObject_GetAttrString(klass, "__name__"); |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 309 | if (name == NULL) { |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 310 | /* This function cannot return an exception */ |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 311 | PyErr_Clear(); |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 312 | return; |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 313 | } |
Martin v. Löwis | 826b9dd | 2007-06-10 21:09:40 +0000 | [diff] [blame] | 314 | if (PyUnicode_Check(name)) { |
| 315 | strncpy(buf, PyUnicode_AsString(name), bufsize); |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 316 | buf[bufsize-1] = '\0'; |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 317 | } |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 318 | Py_DECREF(name); |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 321 | static void |
| 322 | getinstclassname(PyObject *inst, char *buf, int bufsize) |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 323 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 324 | PyObject *klass; |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 325 | |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 326 | if (inst == NULL) { |
Tim Peters | 75585d4 | 2002-08-20 14:31:35 +0000 | [diff] [blame] | 327 | assert(bufsize > 0 && (size_t)bufsize > strlen("nothing")); |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 328 | strcpy(buf, "nothing"); |
| 329 | return; |
| 330 | } |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 331 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 332 | klass = PyObject_GetAttrString(inst, "__class__"); |
| 333 | if (klass == NULL) { |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 334 | /* This function cannot return an exception */ |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 335 | PyErr_Clear(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 336 | klass = (PyObject *)(inst->ob_type); |
| 337 | Py_INCREF(klass); |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 338 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 339 | getclassname(klass, buf, bufsize); |
| 340 | Py_XDECREF(klass); |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 343 | static PyObject * |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 344 | method_call(PyObject *func, PyObject *arg, PyObject *kw) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 345 | { |
| 346 | PyObject *self = PyMethod_GET_SELF(func); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 347 | PyObject *klass = PyMethod_GET_CLASS(func); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 348 | PyObject *result; |
| 349 | |
| 350 | func = PyMethod_GET_FUNCTION(func); |
| 351 | if (self == NULL) { |
| 352 | /* Unbound methods must be called with an instance of |
| 353 | the class (or a derived class) as first argument */ |
| 354 | int ok; |
| 355 | if (PyTuple_Size(arg) >= 1) |
| 356 | self = PyTuple_GET_ITEM(arg, 0); |
| 357 | if (self == NULL) |
| 358 | ok = 0; |
| 359 | else { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 360 | ok = PyObject_IsInstance(self, klass); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 361 | if (ok < 0) |
| 362 | return NULL; |
| 363 | } |
| 364 | if (!ok) { |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 365 | char clsbuf[256]; |
| 366 | char instbuf[256]; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 367 | getclassname(klass, clsbuf, sizeof(clsbuf)); |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 368 | getinstclassname(self, instbuf, sizeof(instbuf)); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 369 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 370 | "unbound method %s%s must be called with " |
| 371 | "%s instance as first argument " |
| 372 | "(got %s%s instead)", |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 373 | PyEval_GetFuncName(func), |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 374 | PyEval_GetFuncDesc(func), |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 375 | clsbuf, |
| 376 | instbuf, |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 377 | self == NULL ? "" : " instance"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 378 | return NULL; |
| 379 | } |
| 380 | Py_INCREF(arg); |
| 381 | } |
| 382 | else { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 383 | Py_ssize_t argcount = PyTuple_Size(arg); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 384 | PyObject *newarg = PyTuple_New(argcount + 1); |
| 385 | int i; |
| 386 | if (newarg == NULL) |
| 387 | return NULL; |
| 388 | Py_INCREF(self); |
| 389 | PyTuple_SET_ITEM(newarg, 0, self); |
| 390 | for (i = 0; i < argcount; i++) { |
| 391 | PyObject *v = PyTuple_GET_ITEM(arg, i); |
| 392 | Py_XINCREF(v); |
| 393 | PyTuple_SET_ITEM(newarg, i+1, v); |
| 394 | } |
| 395 | arg = newarg; |
| 396 | } |
| 397 | result = PyObject_Call((PyObject *)func, arg, kw); |
| 398 | Py_DECREF(arg); |
| 399 | return result; |
| 400 | } |
| 401 | |
Guido van Rossum | 23cc2b4 | 2001-08-15 17:52:31 +0000 | [diff] [blame] | 402 | static PyObject * |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 403 | method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls) |
Guido van Rossum | 23cc2b4 | 2001-08-15 17:52:31 +0000 | [diff] [blame] | 404 | { |
Guido van Rossum | f23c41d | 2001-08-17 13:43:27 +0000 | [diff] [blame] | 405 | /* Don't rebind an already bound method, or an unbound method |
Guido van Rossum | 6bae46d | 2003-02-11 18:43:00 +0000 | [diff] [blame] | 406 | of a class that's not a base class of cls. */ |
| 407 | |
| 408 | if (PyMethod_GET_SELF(meth) != NULL) { |
| 409 | /* Already bound */ |
Guido van Rossum | 501c7c7 | 2001-08-16 20:41:56 +0000 | [diff] [blame] | 410 | Py_INCREF(meth); |
| 411 | return meth; |
| 412 | } |
Guido van Rossum | 6bae46d | 2003-02-11 18:43:00 +0000 | [diff] [blame] | 413 | /* No, it is an unbound method */ |
| 414 | if (PyMethod_GET_CLASS(meth) != NULL && cls != NULL) { |
| 415 | /* Do subclass test. If it fails, return meth unchanged. */ |
| 416 | int ok = PyObject_IsSubclass(cls, PyMethod_GET_CLASS(meth)); |
| 417 | if (ok < 0) |
| 418 | return NULL; |
| 419 | if (!ok) { |
| 420 | Py_INCREF(meth); |
| 421 | return meth; |
| 422 | } |
| 423 | } |
| 424 | /* Bind it to obj */ |
| 425 | return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj, cls); |
Guido van Rossum | 23cc2b4 | 2001-08-15 17:52:31 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 428 | PyTypeObject PyMethod_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 429 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 430 | "method", |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 431 | sizeof(PyMethodObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 432 | 0, |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 433 | (destructor)method_dealloc, /* tp_dealloc */ |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 434 | 0, /* tp_print */ |
| 435 | 0, /* tp_getattr */ |
| 436 | 0, /* tp_setattr */ |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 437 | 0, /* tp_compare */ |
| 438 | (reprfunc)method_repr, /* tp_repr */ |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 439 | 0, /* tp_as_number */ |
| 440 | 0, /* tp_as_sequence */ |
| 441 | 0, /* tp_as_mapping */ |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 442 | (hashfunc)method_hash, /* tp_hash */ |
| 443 | method_call, /* tp_call */ |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 444 | 0, /* tp_str */ |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 445 | method_getattro, /* tp_getattro */ |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 446 | PyObject_GenericSetAttr, /* tp_setattro */ |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 447 | 0, /* tp_as_buffer */ |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 448 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 449 | method_doc, /* tp_doc */ |
| 450 | (traverseproc)method_traverse, /* tp_traverse */ |
Fred Drake | db81e8d | 2001-03-23 04:19:27 +0000 | [diff] [blame] | 451 | 0, /* tp_clear */ |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 452 | method_richcompare, /* tp_richcompare */ |
Guido van Rossum | 23cc2b4 | 2001-08-15 17:52:31 +0000 | [diff] [blame] | 453 | offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */ |
| 454 | 0, /* tp_iter */ |
| 455 | 0, /* tp_iternext */ |
| 456 | 0, /* tp_methods */ |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 457 | method_memberlist, /* tp_members */ |
| 458 | method_getset, /* tp_getset */ |
Guido van Rossum | 23cc2b4 | 2001-08-15 17:52:31 +0000 | [diff] [blame] | 459 | 0, /* tp_base */ |
| 460 | 0, /* tp_dict */ |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 461 | method_descr_get, /* tp_descr_get */ |
Guido van Rossum | 23cc2b4 | 2001-08-15 17:52:31 +0000 | [diff] [blame] | 462 | 0, /* tp_descr_set */ |
| 463 | 0, /* tp_dictoffset */ |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 464 | 0, /* tp_init */ |
| 465 | 0, /* tp_alloc */ |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 466 | method_new, /* tp_new */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 467 | }; |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 468 | |
| 469 | /* Clear out the free list */ |
| 470 | |
| 471 | void |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 472 | PyMethod_Fini(void) |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 473 | { |
| 474 | while (free_list) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 475 | PyMethodObject *im = free_list; |
| 476 | free_list = (PyMethodObject *)(im->im_self); |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 477 | PyObject_GC_Del(im); |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 478 | } |
| 479 | } |