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