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" |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 5 | #include "structmember.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 7 | /* Free list for method objects to safe malloc/free overhead |
| 8 | * The m_self element is used to chain the objects. |
| 9 | */ |
Guido van Rossum | 1f39c5c | 1997-08-05 02:11:41 +0000 | [diff] [blame] | 10 | static PyCFunctionObject *free_list = NULL; |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 11 | static int numfree = 0; |
| 12 | #ifndef PyCFunction_MAXFREELIST |
| 13 | #define PyCFunction_MAXFREELIST 256 |
| 14 | #endif |
Guido van Rossum | 1f39c5c | 1997-08-05 02:11:41 +0000 | [diff] [blame] | 15 | |
Andrew Svetlov | 4de2924 | 2012-12-26 23:08:54 +0200 | [diff] [blame] | 16 | /* undefine macro trampoline to PyCFunction_NewEx */ |
| 17 | #undef PyCFunction_New |
| 18 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 19 | PyObject * |
Andrew Svetlov | 3ba3a3e | 2012-12-25 13:32:35 +0200 | [diff] [blame] | 20 | PyCFunction_New(PyMethodDef *ml, PyObject *self) |
| 21 | { |
| 22 | return PyCFunction_NewEx(ml, self, NULL); |
| 23 | } |
| 24 | |
| 25 | PyObject * |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 26 | PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 27 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 28 | PyCFunctionObject *op; |
| 29 | op = free_list; |
| 30 | if (op != NULL) { |
| 31 | free_list = (PyCFunctionObject *)(op->m_self); |
| 32 | PyObject_INIT(op, &PyCFunction_Type); |
| 33 | numfree--; |
| 34 | } |
| 35 | else { |
| 36 | op = PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type); |
| 37 | if (op == NULL) |
| 38 | return NULL; |
| 39 | } |
| 40 | op->m_ml = ml; |
| 41 | Py_XINCREF(self); |
| 42 | op->m_self = self; |
| 43 | Py_XINCREF(module); |
| 44 | op->m_module = module; |
| 45 | _PyObject_GC_TRACK(op); |
| 46 | return (PyObject *)op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 49 | PyCFunction |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 50 | PyCFunction_GetFunction(PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 51 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 52 | if (!PyCFunction_Check(op)) { |
| 53 | PyErr_BadInternalCall(); |
| 54 | return NULL; |
| 55 | } |
Antoine Pitrou | 5b62942 | 2011-12-23 12:40:16 +0100 | [diff] [blame] | 56 | return PyCFunction_GET_FUNCTION(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 59 | PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 60 | PyCFunction_GetSelf(PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 61 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 62 | if (!PyCFunction_Check(op)) { |
| 63 | PyErr_BadInternalCall(); |
| 64 | return NULL; |
| 65 | } |
Antoine Pitrou | 5b62942 | 2011-12-23 12:40:16 +0100 | [diff] [blame] | 66 | return PyCFunction_GET_SELF(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Guido van Rossum | c060229 | 1991-12-16 13:07:24 +0000 | [diff] [blame] | 69 | int |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 70 | PyCFunction_GetFlags(PyObject *op) |
Guido van Rossum | c060229 | 1991-12-16 13:07:24 +0000 | [diff] [blame] | 71 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 72 | if (!PyCFunction_Check(op)) { |
| 73 | PyErr_BadInternalCall(); |
| 74 | return -1; |
| 75 | } |
Antoine Pitrou | 5b62942 | 2011-12-23 12:40:16 +0100 | [diff] [blame] | 76 | return PyCFunction_GET_FLAGS(op); |
Guido van Rossum | c060229 | 1991-12-16 13:07:24 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Jeremy Hylton | 910d7d4 | 2001-08-12 21:52:24 +0000 | [diff] [blame] | 79 | PyObject * |
| 80 | PyCFunction_Call(PyObject *func, PyObject *arg, PyObject *kw) |
| 81 | { |
Victor Stinner | 9035ad9 | 2013-07-11 23:44:46 +0200 | [diff] [blame] | 82 | #define CHECK_RESULT(res) assert(res != NULL || PyErr_Occurred()) |
| 83 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 84 | PyCFunctionObject* f = (PyCFunctionObject*)func; |
| 85 | PyCFunction meth = PyCFunction_GET_FUNCTION(func); |
| 86 | PyObject *self = PyCFunction_GET_SELF(func); |
Victor Stinner | 9035ad9 | 2013-07-11 23:44:46 +0200 | [diff] [blame] | 87 | PyObject *res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 88 | Py_ssize_t size; |
Jeremy Hylton | 910d7d4 | 2001-08-12 21:52:24 +0000 | [diff] [blame] | 89 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 90 | switch (PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST)) { |
| 91 | case METH_VARARGS: |
Victor Stinner | 9035ad9 | 2013-07-11 23:44:46 +0200 | [diff] [blame] | 92 | if (kw == NULL || PyDict_Size(kw) == 0) { |
| 93 | res = (*meth)(self, arg); |
| 94 | CHECK_RESULT(res); |
| 95 | return res; |
| 96 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 97 | break; |
| 98 | case METH_VARARGS | METH_KEYWORDS: |
Victor Stinner | 9035ad9 | 2013-07-11 23:44:46 +0200 | [diff] [blame] | 99 | res = (*(PyCFunctionWithKeywords)meth)(self, arg, kw); |
| 100 | CHECK_RESULT(res); |
| 101 | return res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 102 | case METH_NOARGS: |
| 103 | if (kw == NULL || PyDict_Size(kw) == 0) { |
| 104 | size = PyTuple_GET_SIZE(arg); |
Victor Stinner | 9035ad9 | 2013-07-11 23:44:46 +0200 | [diff] [blame] | 105 | if (size == 0) { |
| 106 | res = (*meth)(self, NULL); |
| 107 | CHECK_RESULT(res); |
| 108 | return res; |
| 109 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 110 | PyErr_Format(PyExc_TypeError, |
| 111 | "%.200s() takes no arguments (%zd given)", |
| 112 | f->m_ml->ml_name, size); |
| 113 | return NULL; |
| 114 | } |
| 115 | break; |
| 116 | case METH_O: |
| 117 | if (kw == NULL || PyDict_Size(kw) == 0) { |
| 118 | size = PyTuple_GET_SIZE(arg); |
Victor Stinner | 9035ad9 | 2013-07-11 23:44:46 +0200 | [diff] [blame] | 119 | if (size == 1) { |
| 120 | res = (*meth)(self, PyTuple_GET_ITEM(arg, 0)); |
| 121 | CHECK_RESULT(res); |
| 122 | return res; |
| 123 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 124 | PyErr_Format(PyExc_TypeError, |
| 125 | "%.200s() takes exactly one argument (%zd given)", |
| 126 | f->m_ml->ml_name, size); |
| 127 | return NULL; |
| 128 | } |
| 129 | break; |
| 130 | default: |
| 131 | PyErr_SetString(PyExc_SystemError, "Bad call flags in " |
| 132 | "PyCFunction_Call. METH_OLDARGS is no " |
| 133 | "longer supported!"); |
| 134 | |
| 135 | return NULL; |
| 136 | } |
| 137 | PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments", |
| 138 | f->m_ml->ml_name); |
| 139 | return NULL; |
Victor Stinner | 9035ad9 | 2013-07-11 23:44:46 +0200 | [diff] [blame] | 140 | |
| 141 | #undef CHECK_RESULT |
Jeremy Hylton | 910d7d4 | 2001-08-12 21:52:24 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 144 | /* Methods (the standard built-in methods, that is) */ |
| 145 | |
| 146 | static void |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 147 | meth_dealloc(PyCFunctionObject *m) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 148 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 149 | _PyObject_GC_UNTRACK(m); |
| 150 | Py_XDECREF(m->m_self); |
| 151 | Py_XDECREF(m->m_module); |
| 152 | if (numfree < PyCFunction_MAXFREELIST) { |
| 153 | m->m_self = (PyObject *)free_list; |
| 154 | free_list = m; |
| 155 | numfree++; |
| 156 | } |
| 157 | else { |
| 158 | PyObject_GC_Del(m); |
| 159 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame^] | 162 | /* |
| 163 | * finds the docstring's introspection signature. |
| 164 | * if present, returns a pointer pointing to the first '('. |
| 165 | * otherwise returns NULL. |
| 166 | */ |
| 167 | static const char *find_signature(PyCFunctionObject *m) |
| 168 | { |
| 169 | const char *trace = m->m_ml->ml_doc; |
| 170 | const char *name = m->m_ml->ml_name; |
| 171 | size_t length; |
| 172 | if (!trace || !name) |
| 173 | return NULL; |
| 174 | length = strlen(name); |
| 175 | if (strncmp(trace, name, length)) |
| 176 | return NULL; |
| 177 | trace += length; |
| 178 | if (*trace != '(') |
| 179 | return NULL; |
| 180 | return trace; |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * skips to the end of the docstring's instrospection signature. |
| 185 | */ |
| 186 | static const char *skip_signature(const char *trace) |
| 187 | { |
| 188 | while (*trace && *trace != '\n') |
| 189 | trace++; |
| 190 | return trace; |
| 191 | } |
| 192 | |
| 193 | static const char *skip_eols(const char *trace) |
| 194 | { |
| 195 | while (*trace == '\n') |
| 196 | trace++; |
| 197 | return trace; |
| 198 | } |
| 199 | |
| 200 | static PyObject * |
| 201 | meth_get__text_signature__(PyCFunctionObject *m, void *closure) |
| 202 | { |
| 203 | const char *start = find_signature(m); |
| 204 | const char *trace; |
| 205 | |
| 206 | if (!start) { |
| 207 | Py_INCREF(Py_None); |
| 208 | return Py_None; |
| 209 | } |
| 210 | |
| 211 | trace = skip_signature(start); |
| 212 | return PyUnicode_FromStringAndSize(start, trace - start); |
| 213 | } |
| 214 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 215 | static PyObject * |
| 216 | meth_get__doc__(PyCFunctionObject *m, void *closure) |
| 217 | { |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame^] | 218 | const char *doc = find_signature(m); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 219 | |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame^] | 220 | if (doc) |
| 221 | doc = skip_eols(skip_signature(doc)); |
| 222 | else |
| 223 | doc = m->m_ml->ml_doc; |
| 224 | |
| 225 | if (!doc) { |
| 226 | Py_INCREF(Py_None); |
| 227 | return Py_None; |
| 228 | } |
| 229 | |
| 230 | return PyUnicode_FromString(doc); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | static PyObject * |
| 234 | meth_get__name__(PyCFunctionObject *m, void *closure) |
| 235 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 236 | return PyUnicode_FromString(m->m_ml->ml_name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Antoine Pitrou | 5b62942 | 2011-12-23 12:40:16 +0100 | [diff] [blame] | 239 | static PyObject * |
| 240 | meth_get__qualname__(PyCFunctionObject *m, void *closure) |
| 241 | { |
| 242 | /* If __self__ is a module or NULL, return m.__name__ |
| 243 | (e.g. len.__qualname__ == 'len') |
| 244 | |
| 245 | If __self__ is a type, return m.__self__.__qualname__ + '.' + m.__name__ |
| 246 | (e.g. dict.fromkeys.__qualname__ == 'dict.fromkeys') |
| 247 | |
| 248 | Otherwise return type(m.__self__).__qualname__ + '.' + m.__name__ |
| 249 | (e.g. [].append.__qualname__ == 'list.append') */ |
| 250 | PyObject *type, *type_qualname, *res; |
| 251 | _Py_IDENTIFIER(__qualname__); |
| 252 | |
| 253 | if (m->m_self == NULL || PyModule_Check(m->m_self)) |
| 254 | return PyUnicode_FromString(m->m_ml->ml_name); |
| 255 | |
| 256 | type = PyType_Check(m->m_self) ? m->m_self : (PyObject*)Py_TYPE(m->m_self); |
| 257 | |
| 258 | type_qualname = _PyObject_GetAttrId(type, &PyId___qualname__); |
| 259 | if (type_qualname == NULL) |
| 260 | return NULL; |
| 261 | |
| 262 | if (!PyUnicode_Check(type_qualname)) { |
| 263 | PyErr_SetString(PyExc_TypeError, "<method>.__class__." |
| 264 | "__qualname__ is not a unicode object"); |
| 265 | Py_XDECREF(type_qualname); |
| 266 | return NULL; |
| 267 | } |
| 268 | |
| 269 | res = PyUnicode_FromFormat("%S.%s", type_qualname, m->m_ml->ml_name); |
| 270 | Py_DECREF(type_qualname); |
| 271 | return res; |
| 272 | } |
| 273 | |
Neil Schemenauer | 10c6692 | 2001-07-12 13:27:35 +0000 | [diff] [blame] | 274 | static int |
| 275 | meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg) |
| 276 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 277 | Py_VISIT(m->m_self); |
| 278 | Py_VISIT(m->m_module); |
| 279 | return 0; |
Neil Schemenauer | 10c6692 | 2001-07-12 13:27:35 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 282 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 283 | meth_get__self__(PyCFunctionObject *m, void *closure) |
Guido van Rossum | cab650d | 1995-01-07 12:34:58 +0000 | [diff] [blame] | 284 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 285 | PyObject *self; |
Guido van Rossum | a8add0e | 2007-05-14 22:03:55 +0000 | [diff] [blame] | 286 | |
Antoine Pitrou | 5b62942 | 2011-12-23 12:40:16 +0100 | [diff] [blame] | 287 | self = PyCFunction_GET_SELF(m); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 288 | if (self == NULL) |
| 289 | self = Py_None; |
| 290 | Py_INCREF(self); |
| 291 | return self; |
Guido van Rossum | cab650d | 1995-01-07 12:34:58 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 294 | static PyGetSetDef meth_getsets [] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 295 | {"__doc__", (getter)meth_get__doc__, NULL, NULL}, |
| 296 | {"__name__", (getter)meth_get__name__, NULL, NULL}, |
Antoine Pitrou | 5b62942 | 2011-12-23 12:40:16 +0100 | [diff] [blame] | 297 | {"__qualname__", (getter)meth_get__qualname__, NULL, NULL}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 298 | {"__self__", (getter)meth_get__self__, NULL, NULL}, |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame^] | 299 | {"__text_signature__", (getter)meth_get__text_signature__, NULL, NULL}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 300 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 301 | }; |
| 302 | |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 303 | #define OFF(x) offsetof(PyCFunctionObject, x) |
| 304 | |
| 305 | static PyMemberDef meth_members[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 306 | {"__module__", T_OBJECT, OFF(m_module), PY_WRITE_RESTRICTED}, |
| 307 | {NULL} |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 308 | }; |
| 309 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 310 | static PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 311 | meth_repr(PyCFunctionObject *m) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 312 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 313 | if (m->m_self == NULL || PyModule_Check(m->m_self)) |
| 314 | return PyUnicode_FromFormat("<built-in function %s>", |
| 315 | m->m_ml->ml_name); |
| 316 | return PyUnicode_FromFormat("<built-in method %s of %s object at %p>", |
| 317 | m->m_ml->ml_name, |
| 318 | m->m_self->ob_type->tp_name, |
| 319 | m->m_self); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 322 | static PyObject * |
| 323 | meth_richcompare(PyObject *self, PyObject *other, int op) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 324 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 325 | PyCFunctionObject *a, *b; |
| 326 | PyObject *res; |
| 327 | int eq; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 328 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 329 | if ((op != Py_EQ && op != Py_NE) || |
| 330 | !PyCFunction_Check(self) || |
| 331 | !PyCFunction_Check(other)) |
| 332 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 333 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 334 | } |
| 335 | a = (PyCFunctionObject *)self; |
| 336 | b = (PyCFunctionObject *)other; |
| 337 | eq = a->m_self == b->m_self; |
| 338 | if (eq) |
| 339 | eq = a->m_ml->ml_meth == b->m_ml->ml_meth; |
| 340 | if (op == Py_EQ) |
| 341 | res = eq ? Py_True : Py_False; |
| 342 | else |
| 343 | res = eq ? Py_False : Py_True; |
| 344 | Py_INCREF(res); |
| 345 | return res; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 348 | static Py_hash_t |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 349 | meth_hash(PyCFunctionObject *a) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 350 | { |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 351 | Py_hash_t x, y; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 352 | if (a->m_self == NULL) |
| 353 | x = 0; |
| 354 | else { |
| 355 | x = PyObject_Hash(a->m_self); |
| 356 | if (x == -1) |
| 357 | return -1; |
| 358 | } |
| 359 | y = _Py_HashPointer((void*)(a->m_ml->ml_meth)); |
| 360 | if (y == -1) |
| 361 | return -1; |
| 362 | x ^= y; |
| 363 | if (x == -1) |
| 364 | x = -2; |
| 365 | return x; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 368 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 369 | PyTypeObject PyCFunction_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 370 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 371 | "builtin_function_or_method", |
| 372 | sizeof(PyCFunctionObject), |
| 373 | 0, |
| 374 | (destructor)meth_dealloc, /* tp_dealloc */ |
| 375 | 0, /* tp_print */ |
| 376 | 0, /* tp_getattr */ |
| 377 | 0, /* tp_setattr */ |
| 378 | 0, /* tp_reserved */ |
| 379 | (reprfunc)meth_repr, /* tp_repr */ |
| 380 | 0, /* tp_as_number */ |
| 381 | 0, /* tp_as_sequence */ |
| 382 | 0, /* tp_as_mapping */ |
| 383 | (hashfunc)meth_hash, /* tp_hash */ |
| 384 | PyCFunction_Call, /* tp_call */ |
| 385 | 0, /* tp_str */ |
| 386 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 387 | 0, /* tp_setattro */ |
| 388 | 0, /* tp_as_buffer */ |
| 389 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 390 | 0, /* tp_doc */ |
| 391 | (traverseproc)meth_traverse, /* tp_traverse */ |
| 392 | 0, /* tp_clear */ |
| 393 | meth_richcompare, /* tp_richcompare */ |
| 394 | 0, /* tp_weaklistoffset */ |
| 395 | 0, /* tp_iter */ |
| 396 | 0, /* tp_iternext */ |
| 397 | 0, /* tp_methods */ |
| 398 | meth_members, /* tp_members */ |
| 399 | meth_getsets, /* tp_getset */ |
| 400 | 0, /* tp_base */ |
| 401 | 0, /* tp_dict */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 402 | }; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 403 | |
Guido van Rossum | 1f39c5c | 1997-08-05 02:11:41 +0000 | [diff] [blame] | 404 | /* Clear out the free list */ |
| 405 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 406 | int |
| 407 | PyCFunction_ClearFreeList(void) |
Guido van Rossum | 1f39c5c | 1997-08-05 02:11:41 +0000 | [diff] [blame] | 408 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 409 | int freelist_size = numfree; |
| 410 | |
| 411 | while (free_list) { |
| 412 | PyCFunctionObject *v = free_list; |
| 413 | free_list = (PyCFunctionObject *)(v->m_self); |
| 414 | PyObject_GC_Del(v); |
| 415 | numfree--; |
| 416 | } |
| 417 | assert(numfree == 0); |
| 418 | return freelist_size; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | void |
| 422 | PyCFunction_Fini(void) |
| 423 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 424 | (void)PyCFunction_ClearFreeList(); |
Guido van Rossum | 1f39c5c | 1997-08-05 02:11:41 +0000 | [diff] [blame] | 425 | } |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 426 | |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 427 | /* Print summary info about the state of the optimized allocator */ |
| 428 | void |
| 429 | _PyCFunction_DebugMallocStats(FILE *out) |
| 430 | { |
| 431 | _PyDebugAllocatorStats(out, |
Antoine Pitrou | 36b045f | 2013-04-11 21:01:40 +0200 | [diff] [blame] | 432 | "free PyCFunctionObject", |
Antoine Pitrou | 0811f98 | 2012-12-30 22:46:04 +0100 | [diff] [blame] | 433 | numfree, sizeof(PyCFunctionObject)); |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 434 | } |