PageRenderTime 94ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/Objects/methodobject.c

https://github.com/albertz/CPython
C | 333 lines | 276 code | 41 blank | 16 comment | 46 complexity | d64e3f1bfde74e2487dcb6b0e13b81a0 MD5 | raw file
  1. /* Method object implementation */
  2. #include "Python.h"
  3. #include "internal/mem.h"
  4. #include "internal/pystate.h"
  5. #include "structmember.h"
  6. /* Free list for method objects to safe malloc/free overhead
  7. * The m_self element is used to chain the objects.
  8. */
  9. static PyCFunctionObject *free_list = NULL;
  10. static int numfree = 0;
  11. #ifndef PyCFunction_MAXFREELIST
  12. #define PyCFunction_MAXFREELIST 256
  13. #endif
  14. /* undefine macro trampoline to PyCFunction_NewEx */
  15. #undef PyCFunction_New
  16. PyAPI_FUNC(PyObject *)
  17. PyCFunction_New(PyMethodDef *ml, PyObject *self)
  18. {
  19. return PyCFunction_NewEx(ml, self, NULL);
  20. }
  21. PyObject *
  22. PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module)
  23. {
  24. PyCFunctionObject *op;
  25. op = free_list;
  26. if (op != NULL) {
  27. free_list = (PyCFunctionObject *)(op->m_self);
  28. (void)PyObject_INIT(op, &PyCFunction_Type);
  29. numfree--;
  30. }
  31. else {
  32. op = PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type);
  33. if (op == NULL)
  34. return NULL;
  35. }
  36. op->m_weakreflist = NULL;
  37. op->m_ml = ml;
  38. Py_XINCREF(self);
  39. op->m_self = self;
  40. Py_XINCREF(module);
  41. op->m_module = module;
  42. _PyObject_GC_TRACK(op);
  43. return (PyObject *)op;
  44. }
  45. PyCFunction
  46. PyCFunction_GetFunction(PyObject *op)
  47. {
  48. if (!PyCFunction_Check(op)) {
  49. PyErr_BadInternalCall();
  50. return NULL;
  51. }
  52. return PyCFunction_GET_FUNCTION(op);
  53. }
  54. PyObject *
  55. PyCFunction_GetSelf(PyObject *op)
  56. {
  57. if (!PyCFunction_Check(op)) {
  58. PyErr_BadInternalCall();
  59. return NULL;
  60. }
  61. return PyCFunction_GET_SELF(op);
  62. }
  63. int
  64. PyCFunction_GetFlags(PyObject *op)
  65. {
  66. if (!PyCFunction_Check(op)) {
  67. PyErr_BadInternalCall();
  68. return -1;
  69. }
  70. return PyCFunction_GET_FLAGS(op);
  71. }
  72. /* Methods (the standard built-in methods, that is) */
  73. static void
  74. meth_dealloc(PyCFunctionObject *m)
  75. {
  76. _PyObject_GC_UNTRACK(m);
  77. if (m->m_weakreflist != NULL) {
  78. PyObject_ClearWeakRefs((PyObject*) m);
  79. }
  80. Py_XDECREF(m->m_self);
  81. Py_XDECREF(m->m_module);
  82. if (numfree < PyCFunction_MAXFREELIST) {
  83. m->m_self = (PyObject *)free_list;
  84. free_list = m;
  85. numfree++;
  86. }
  87. else {
  88. PyObject_GC_Del(m);
  89. }
  90. }
  91. static PyObject *
  92. meth_reduce(PyCFunctionObject *m)
  93. {
  94. _Py_IDENTIFIER(getattr);
  95. if (m->m_self == NULL || PyModule_Check(m->m_self))
  96. return PyUnicode_FromString(m->m_ml->ml_name);
  97. return Py_BuildValue("N(Os)", _PyEval_GetBuiltinId(&PyId_getattr),
  98. m->m_self, m->m_ml->ml_name);
  99. }
  100. static PyMethodDef meth_methods[] = {
  101. {"__reduce__", (PyCFunction)meth_reduce, METH_NOARGS, NULL},
  102. {NULL, NULL}
  103. };
  104. static PyObject *
  105. meth_get__text_signature__(PyCFunctionObject *m, void *closure)
  106. {
  107. return _PyType_GetTextSignatureFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
  108. }
  109. static PyObject *
  110. meth_get__doc__(PyCFunctionObject *m, void *closure)
  111. {
  112. return _PyType_GetDocFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
  113. }
  114. static PyObject *
  115. meth_get__name__(PyCFunctionObject *m, void *closure)
  116. {
  117. return PyUnicode_FromString(m->m_ml->ml_name);
  118. }
  119. static PyObject *
  120. meth_get__qualname__(PyCFunctionObject *m, void *closure)
  121. {
  122. /* If __self__ is a module or NULL, return m.__name__
  123. (e.g. len.__qualname__ == 'len')
  124. If __self__ is a type, return m.__self__.__qualname__ + '.' + m.__name__
  125. (e.g. dict.fromkeys.__qualname__ == 'dict.fromkeys')
  126. Otherwise return type(m.__self__).__qualname__ + '.' + m.__name__
  127. (e.g. [].append.__qualname__ == 'list.append') */
  128. PyObject *type, *type_qualname, *res;
  129. _Py_IDENTIFIER(__qualname__);
  130. if (m->m_self == NULL || PyModule_Check(m->m_self))
  131. return PyUnicode_FromString(m->m_ml->ml_name);
  132. type = PyType_Check(m->m_self) ? m->m_self : (PyObject*)Py_TYPE(m->m_self);
  133. type_qualname = _PyObject_GetAttrId(type, &PyId___qualname__);
  134. if (type_qualname == NULL)
  135. return NULL;
  136. if (!PyUnicode_Check(type_qualname)) {
  137. PyErr_SetString(PyExc_TypeError, "<method>.__class__."
  138. "__qualname__ is not a unicode object");
  139. Py_XDECREF(type_qualname);
  140. return NULL;
  141. }
  142. res = PyUnicode_FromFormat("%S.%s", type_qualname, m->m_ml->ml_name);
  143. Py_DECREF(type_qualname);
  144. return res;
  145. }
  146. static int
  147. meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
  148. {
  149. Py_VISIT(m->m_self);
  150. Py_VISIT(m->m_module);
  151. return 0;
  152. }
  153. static PyObject *
  154. meth_get__self__(PyCFunctionObject *m, void *closure)
  155. {
  156. PyObject *self;
  157. self = PyCFunction_GET_SELF(m);
  158. if (self == NULL)
  159. self = Py_None;
  160. Py_INCREF(self);
  161. return self;
  162. }
  163. static PyGetSetDef meth_getsets [] = {
  164. {"__doc__", (getter)meth_get__doc__, NULL, NULL},
  165. {"__name__", (getter)meth_get__name__, NULL, NULL},
  166. {"__qualname__", (getter)meth_get__qualname__, NULL, NULL},
  167. {"__self__", (getter)meth_get__self__, NULL, NULL},
  168. {"__text_signature__", (getter)meth_get__text_signature__, NULL, NULL},
  169. {0}
  170. };
  171. #define OFF(x) offsetof(PyCFunctionObject, x)
  172. static PyMemberDef meth_members[] = {
  173. {"__module__", T_OBJECT, OFF(m_module), PY_WRITE_RESTRICTED},
  174. {NULL}
  175. };
  176. static PyObject *
  177. meth_repr(PyCFunctionObject *m)
  178. {
  179. if (m->m_self == NULL || PyModule_Check(m->m_self))
  180. return PyUnicode_FromFormat("<built-in function %s>",
  181. m->m_ml->ml_name);
  182. return PyUnicode_FromFormat("<built-in method %s of %s object at %p>",
  183. m->m_ml->ml_name,
  184. m->m_self->ob_type->tp_name,
  185. m->m_self);
  186. }
  187. static PyObject *
  188. meth_richcompare(PyObject *self, PyObject *other, int op)
  189. {
  190. PyCFunctionObject *a, *b;
  191. PyObject *res;
  192. int eq;
  193. if ((op != Py_EQ && op != Py_NE) ||
  194. !PyCFunction_Check(self) ||
  195. !PyCFunction_Check(other))
  196. {
  197. Py_RETURN_NOTIMPLEMENTED;
  198. }
  199. a = (PyCFunctionObject *)self;
  200. b = (PyCFunctionObject *)other;
  201. eq = a->m_self == b->m_self;
  202. if (eq)
  203. eq = a->m_ml->ml_meth == b->m_ml->ml_meth;
  204. if (op == Py_EQ)
  205. res = eq ? Py_True : Py_False;
  206. else
  207. res = eq ? Py_False : Py_True;
  208. Py_INCREF(res);
  209. return res;
  210. }
  211. static Py_hash_t
  212. meth_hash(PyCFunctionObject *a)
  213. {
  214. Py_hash_t x, y;
  215. if (a->m_self == NULL)
  216. x = 0;
  217. else {
  218. x = PyObject_Hash(a->m_self);
  219. if (x == -1)
  220. return -1;
  221. }
  222. y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
  223. if (y == -1)
  224. return -1;
  225. x ^= y;
  226. if (x == -1)
  227. x = -2;
  228. return x;
  229. }
  230. PyTypeObject PyCFunction_Type = {
  231. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  232. "builtin_function_or_method",
  233. sizeof(PyCFunctionObject),
  234. 0,
  235. (destructor)meth_dealloc, /* tp_dealloc */
  236. 0, /* tp_print */
  237. 0, /* tp_getattr */
  238. 0, /* tp_setattr */
  239. 0, /* tp_reserved */
  240. (reprfunc)meth_repr, /* tp_repr */
  241. 0, /* tp_as_number */
  242. 0, /* tp_as_sequence */
  243. 0, /* tp_as_mapping */
  244. (hashfunc)meth_hash, /* tp_hash */
  245. PyCFunction_Call, /* tp_call */
  246. 0, /* tp_str */
  247. PyObject_GenericGetAttr, /* tp_getattro */
  248. 0, /* tp_setattro */
  249. 0, /* tp_as_buffer */
  250. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
  251. 0, /* tp_doc */
  252. (traverseproc)meth_traverse, /* tp_traverse */
  253. 0, /* tp_clear */
  254. meth_richcompare, /* tp_richcompare */
  255. offsetof(PyCFunctionObject, m_weakreflist), /* tp_weaklistoffset */
  256. 0, /* tp_iter */
  257. 0, /* tp_iternext */
  258. meth_methods, /* tp_methods */
  259. meth_members, /* tp_members */
  260. meth_getsets, /* tp_getset */
  261. 0, /* tp_base */
  262. 0, /* tp_dict */
  263. };
  264. /* Clear out the free list */
  265. int
  266. PyCFunction_ClearFreeList(void)
  267. {
  268. int freelist_size = numfree;
  269. while (free_list) {
  270. PyCFunctionObject *v = free_list;
  271. free_list = (PyCFunctionObject *)(v->m_self);
  272. PyObject_GC_Del(v);
  273. numfree--;
  274. }
  275. assert(numfree == 0);
  276. return freelist_size;
  277. }
  278. void
  279. PyCFunction_Fini(void)
  280. {
  281. (void)PyCFunction_ClearFreeList();
  282. }
  283. /* Print summary info about the state of the optimized allocator */
  284. void
  285. _PyCFunction_DebugMallocStats(FILE *out)
  286. {
  287. _PyDebugAllocatorStats(out,
  288. "free PyCFunctionObject",
  289. numfree, sizeof(PyCFunctionObject));
  290. }