Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* Method object implementation */ |
| 3 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 5 | #include "internal/mem.h" |
| 6 | #include "internal/pystate.h" |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 7 | #include "structmember.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 9 | /* Free list for method objects to safe malloc/free overhead |
| 10 | * The m_self element is used to chain the objects. |
| 11 | */ |
Guido van Rossum | 1f39c5c | 1997-08-05 02:11:41 +0000 | [diff] [blame] | 12 | static PyCFunctionObject *free_list = NULL; |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 13 | static int numfree = 0; |
| 14 | #ifndef PyCFunction_MAXFREELIST |
| 15 | #define PyCFunction_MAXFREELIST 256 |
| 16 | #endif |
Guido van Rossum | 1f39c5c | 1997-08-05 02:11:41 +0000 | [diff] [blame] | 17 | |
Andrew Svetlov | 4de2924 | 2012-12-26 23:08:54 +0200 | [diff] [blame] | 18 | /* undefine macro trampoline to PyCFunction_NewEx */ |
| 19 | #undef PyCFunction_New |
| 20 | |
Andrew Svetlov | 9df36c9 | 2015-04-27 17:48:50 +0300 | [diff] [blame] | 21 | PyAPI_FUNC(PyObject *) |
Andrew Svetlov | 3ba3a3e | 2012-12-25 13:32:35 +0200 | [diff] [blame] | 22 | PyCFunction_New(PyMethodDef *ml, PyObject *self) |
| 23 | { |
| 24 | return PyCFunction_NewEx(ml, self, NULL); |
| 25 | } |
| 26 | |
| 27 | PyObject * |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 28 | PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 29 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 30 | PyCFunctionObject *op; |
| 31 | op = free_list; |
| 32 | if (op != NULL) { |
| 33 | free_list = (PyCFunctionObject *)(op->m_self); |
Christian Heimes | d3afe78 | 2013-12-04 09:27:47 +0100 | [diff] [blame] | 34 | (void)PyObject_INIT(op, &PyCFunction_Type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 35 | numfree--; |
| 36 | } |
| 37 | else { |
| 38 | op = PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type); |
| 39 | if (op == NULL) |
| 40 | return NULL; |
| 41 | } |
Antoine Pitrou | b349e4c | 2014-08-06 19:31:40 -0400 | [diff] [blame] | 42 | op->m_weakreflist = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 43 | op->m_ml = ml; |
| 44 | Py_XINCREF(self); |
| 45 | op->m_self = self; |
| 46 | Py_XINCREF(module); |
| 47 | op->m_module = module; |
| 48 | _PyObject_GC_TRACK(op); |
| 49 | return (PyObject *)op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 52 | PyCFunction |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 53 | PyCFunction_GetFunction(PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 54 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 55 | if (!PyCFunction_Check(op)) { |
| 56 | PyErr_BadInternalCall(); |
| 57 | return NULL; |
| 58 | } |
Antoine Pitrou | 5b62942 | 2011-12-23 12:40:16 +0100 | [diff] [blame] | 59 | return PyCFunction_GET_FUNCTION(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 62 | PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 63 | PyCFunction_GetSelf(PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 64 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 65 | if (!PyCFunction_Check(op)) { |
| 66 | PyErr_BadInternalCall(); |
| 67 | return NULL; |
| 68 | } |
Antoine Pitrou | 5b62942 | 2011-12-23 12:40:16 +0100 | [diff] [blame] | 69 | return PyCFunction_GET_SELF(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Guido van Rossum | c060229 | 1991-12-16 13:07:24 +0000 | [diff] [blame] | 72 | int |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 73 | PyCFunction_GetFlags(PyObject *op) |
Guido van Rossum | c060229 | 1991-12-16 13:07:24 +0000 | [diff] [blame] | 74 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 75 | if (!PyCFunction_Check(op)) { |
| 76 | PyErr_BadInternalCall(); |
| 77 | return -1; |
| 78 | } |
Antoine Pitrou | 5b62942 | 2011-12-23 12:40:16 +0100 | [diff] [blame] | 79 | return PyCFunction_GET_FLAGS(op); |
Guido van Rossum | c060229 | 1991-12-16 13:07:24 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 82 | /* Methods (the standard built-in methods, that is) */ |
| 83 | |
| 84 | static void |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 85 | meth_dealloc(PyCFunctionObject *m) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 86 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 87 | _PyObject_GC_UNTRACK(m); |
Antoine Pitrou | b349e4c | 2014-08-06 19:31:40 -0400 | [diff] [blame] | 88 | if (m->m_weakreflist != NULL) { |
| 89 | PyObject_ClearWeakRefs((PyObject*) m); |
| 90 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 91 | Py_XDECREF(m->m_self); |
| 92 | Py_XDECREF(m->m_module); |
| 93 | if (numfree < PyCFunction_MAXFREELIST) { |
| 94 | m->m_self = (PyObject *)free_list; |
| 95 | free_list = m; |
| 96 | numfree++; |
| 97 | } |
| 98 | else { |
| 99 | PyObject_GC_Del(m); |
| 100 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Alexandre Vassalotti | 4c05d3b | 2013-11-24 02:41:05 -0800 | [diff] [blame] | 103 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 104 | meth_reduce(PyCFunctionObject *m, PyObject *Py_UNUSED(ignored)) |
Alexandre Vassalotti | 4c05d3b | 2013-11-24 02:41:05 -0800 | [diff] [blame] | 105 | { |
| 106 | PyObject *builtins; |
| 107 | PyObject *getattr; |
| 108 | _Py_IDENTIFIER(getattr); |
| 109 | |
| 110 | if (m->m_self == NULL || PyModule_Check(m->m_self)) |
| 111 | return PyUnicode_FromString(m->m_ml->ml_name); |
| 112 | |
| 113 | builtins = PyEval_GetBuiltins(); |
| 114 | getattr = _PyDict_GetItemId(builtins, &PyId_getattr); |
| 115 | return Py_BuildValue("O(Os)", getattr, m->m_self, m->m_ml->ml_name); |
| 116 | } |
| 117 | |
| 118 | static PyMethodDef meth_methods[] = { |
| 119 | {"__reduce__", (PyCFunction)meth_reduce, METH_NOARGS, NULL}, |
| 120 | {NULL, NULL} |
| 121 | }; |
| 122 | |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 123 | static PyObject * |
| 124 | meth_get__text_signature__(PyCFunctionObject *m, void *closure) |
| 125 | { |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 126 | return _PyType_GetTextSignatureFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc); |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 129 | static PyObject * |
| 130 | meth_get__doc__(PyCFunctionObject *m, void *closure) |
| 131 | { |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 132 | return _PyType_GetDocFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | static PyObject * |
| 136 | meth_get__name__(PyCFunctionObject *m, void *closure) |
| 137 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 138 | return PyUnicode_FromString(m->m_ml->ml_name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Antoine Pitrou | 5b62942 | 2011-12-23 12:40:16 +0100 | [diff] [blame] | 141 | static PyObject * |
| 142 | meth_get__qualname__(PyCFunctionObject *m, void *closure) |
| 143 | { |
| 144 | /* If __self__ is a module or NULL, return m.__name__ |
| 145 | (e.g. len.__qualname__ == 'len') |
| 146 | |
| 147 | If __self__ is a type, return m.__self__.__qualname__ + '.' + m.__name__ |
| 148 | (e.g. dict.fromkeys.__qualname__ == 'dict.fromkeys') |
| 149 | |
| 150 | Otherwise return type(m.__self__).__qualname__ + '.' + m.__name__ |
| 151 | (e.g. [].append.__qualname__ == 'list.append') */ |
| 152 | PyObject *type, *type_qualname, *res; |
| 153 | _Py_IDENTIFIER(__qualname__); |
| 154 | |
| 155 | if (m->m_self == NULL || PyModule_Check(m->m_self)) |
| 156 | return PyUnicode_FromString(m->m_ml->ml_name); |
| 157 | |
| 158 | type = PyType_Check(m->m_self) ? m->m_self : (PyObject*)Py_TYPE(m->m_self); |
| 159 | |
| 160 | type_qualname = _PyObject_GetAttrId(type, &PyId___qualname__); |
| 161 | if (type_qualname == NULL) |
| 162 | return NULL; |
| 163 | |
| 164 | if (!PyUnicode_Check(type_qualname)) { |
| 165 | PyErr_SetString(PyExc_TypeError, "<method>.__class__." |
| 166 | "__qualname__ is not a unicode object"); |
| 167 | Py_XDECREF(type_qualname); |
| 168 | return NULL; |
| 169 | } |
| 170 | |
| 171 | res = PyUnicode_FromFormat("%S.%s", type_qualname, m->m_ml->ml_name); |
| 172 | Py_DECREF(type_qualname); |
| 173 | return res; |
| 174 | } |
| 175 | |
Neil Schemenauer | 10c6692 | 2001-07-12 13:27:35 +0000 | [diff] [blame] | 176 | static int |
| 177 | meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg) |
| 178 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 179 | Py_VISIT(m->m_self); |
| 180 | Py_VISIT(m->m_module); |
| 181 | return 0; |
Neil Schemenauer | 10c6692 | 2001-07-12 13:27:35 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 184 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 185 | meth_get__self__(PyCFunctionObject *m, void *closure) |
Guido van Rossum | cab650d | 1995-01-07 12:34:58 +0000 | [diff] [blame] | 186 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 187 | PyObject *self; |
Guido van Rossum | a8add0e | 2007-05-14 22:03:55 +0000 | [diff] [blame] | 188 | |
Antoine Pitrou | 5b62942 | 2011-12-23 12:40:16 +0100 | [diff] [blame] | 189 | self = PyCFunction_GET_SELF(m); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 190 | if (self == NULL) |
| 191 | self = Py_None; |
| 192 | Py_INCREF(self); |
| 193 | return self; |
Guido van Rossum | cab650d | 1995-01-07 12:34:58 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 196 | static PyGetSetDef meth_getsets [] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 197 | {"__doc__", (getter)meth_get__doc__, NULL, NULL}, |
| 198 | {"__name__", (getter)meth_get__name__, NULL, NULL}, |
Antoine Pitrou | 5b62942 | 2011-12-23 12:40:16 +0100 | [diff] [blame] | 199 | {"__qualname__", (getter)meth_get__qualname__, NULL, NULL}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 200 | {"__self__", (getter)meth_get__self__, NULL, NULL}, |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 201 | {"__text_signature__", (getter)meth_get__text_signature__, NULL, NULL}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 202 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 203 | }; |
| 204 | |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 205 | #define OFF(x) offsetof(PyCFunctionObject, x) |
| 206 | |
| 207 | static PyMemberDef meth_members[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 208 | {"__module__", T_OBJECT, OFF(m_module), PY_WRITE_RESTRICTED}, |
| 209 | {NULL} |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 210 | }; |
| 211 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 212 | static PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 213 | meth_repr(PyCFunctionObject *m) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 214 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 215 | if (m->m_self == NULL || PyModule_Check(m->m_self)) |
| 216 | return PyUnicode_FromFormat("<built-in function %s>", |
| 217 | m->m_ml->ml_name); |
| 218 | return PyUnicode_FromFormat("<built-in method %s of %s object at %p>", |
| 219 | m->m_ml->ml_name, |
| 220 | m->m_self->ob_type->tp_name, |
| 221 | m->m_self); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 224 | static PyObject * |
| 225 | meth_richcompare(PyObject *self, PyObject *other, int op) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 226 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 227 | PyCFunctionObject *a, *b; |
| 228 | PyObject *res; |
| 229 | int eq; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 230 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 231 | if ((op != Py_EQ && op != Py_NE) || |
| 232 | !PyCFunction_Check(self) || |
| 233 | !PyCFunction_Check(other)) |
| 234 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 235 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 236 | } |
| 237 | a = (PyCFunctionObject *)self; |
| 238 | b = (PyCFunctionObject *)other; |
| 239 | eq = a->m_self == b->m_self; |
| 240 | if (eq) |
| 241 | eq = a->m_ml->ml_meth == b->m_ml->ml_meth; |
| 242 | if (op == Py_EQ) |
| 243 | res = eq ? Py_True : Py_False; |
| 244 | else |
| 245 | res = eq ? Py_False : Py_True; |
| 246 | Py_INCREF(res); |
| 247 | return res; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 250 | static Py_hash_t |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 251 | meth_hash(PyCFunctionObject *a) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 252 | { |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 253 | Py_hash_t x, y; |
Serhiy Storchaka | ac20e0f | 2018-07-31 09:18:24 +0300 | [diff] [blame] | 254 | x = _Py_HashPointer(a->m_self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 255 | y = _Py_HashPointer((void*)(a->m_ml->ml_meth)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 256 | x ^= y; |
| 257 | if (x == -1) |
| 258 | x = -2; |
| 259 | return x; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 262 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 263 | PyTypeObject PyCFunction_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 264 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 265 | "builtin_function_or_method", |
| 266 | sizeof(PyCFunctionObject), |
| 267 | 0, |
| 268 | (destructor)meth_dealloc, /* tp_dealloc */ |
| 269 | 0, /* tp_print */ |
| 270 | 0, /* tp_getattr */ |
| 271 | 0, /* tp_setattr */ |
| 272 | 0, /* tp_reserved */ |
| 273 | (reprfunc)meth_repr, /* tp_repr */ |
| 274 | 0, /* tp_as_number */ |
| 275 | 0, /* tp_as_sequence */ |
| 276 | 0, /* tp_as_mapping */ |
| 277 | (hashfunc)meth_hash, /* tp_hash */ |
| 278 | PyCFunction_Call, /* tp_call */ |
| 279 | 0, /* tp_str */ |
| 280 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 281 | 0, /* tp_setattro */ |
| 282 | 0, /* tp_as_buffer */ |
| 283 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 284 | 0, /* tp_doc */ |
| 285 | (traverseproc)meth_traverse, /* tp_traverse */ |
| 286 | 0, /* tp_clear */ |
| 287 | meth_richcompare, /* tp_richcompare */ |
Antoine Pitrou | b349e4c | 2014-08-06 19:31:40 -0400 | [diff] [blame] | 288 | offsetof(PyCFunctionObject, m_weakreflist), /* tp_weaklistoffset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 289 | 0, /* tp_iter */ |
| 290 | 0, /* tp_iternext */ |
Alexandre Vassalotti | 4c05d3b | 2013-11-24 02:41:05 -0800 | [diff] [blame] | 291 | meth_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 292 | meth_members, /* tp_members */ |
| 293 | meth_getsets, /* tp_getset */ |
| 294 | 0, /* tp_base */ |
| 295 | 0, /* tp_dict */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 296 | }; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 297 | |
Guido van Rossum | 1f39c5c | 1997-08-05 02:11:41 +0000 | [diff] [blame] | 298 | /* Clear out the free list */ |
| 299 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 300 | int |
| 301 | PyCFunction_ClearFreeList(void) |
Guido van Rossum | 1f39c5c | 1997-08-05 02:11:41 +0000 | [diff] [blame] | 302 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 303 | int freelist_size = numfree; |
| 304 | |
| 305 | while (free_list) { |
| 306 | PyCFunctionObject *v = free_list; |
| 307 | free_list = (PyCFunctionObject *)(v->m_self); |
| 308 | PyObject_GC_Del(v); |
| 309 | numfree--; |
| 310 | } |
| 311 | assert(numfree == 0); |
| 312 | return freelist_size; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | void |
| 316 | PyCFunction_Fini(void) |
| 317 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 318 | (void)PyCFunction_ClearFreeList(); |
Guido van Rossum | 1f39c5c | 1997-08-05 02:11:41 +0000 | [diff] [blame] | 319 | } |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 320 | |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 321 | /* Print summary info about the state of the optimized allocator */ |
| 322 | void |
| 323 | _PyCFunction_DebugMallocStats(FILE *out) |
| 324 | { |
| 325 | _PyDebugAllocatorStats(out, |
Antoine Pitrou | 36b045f | 2013-04-11 21:01:40 +0200 | [diff] [blame] | 326 | "free PyCFunctionObject", |
Antoine Pitrou | 0811f98 | 2012-12-30 22:46:04 +0100 | [diff] [blame] | 327 | numfree, sizeof(PyCFunctionObject)); |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 328 | } |