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