PageRenderTime 524ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/kbe/src/lib/python/Objects/methodobject.c

https://bitbucket.org/kbengine/kbengine
C | 318 lines | 274 code | 34 blank | 10 comment | 54 complexity | e1ba87452ca93d572cfbac8bcd2a06b3 MD5 | raw file
  1. /* Method object implementation */
  2. #include "Python.h"
  3. #include "structmember.h"
  4. /* Free list for method objects to safe malloc/free overhead
  5. * The m_self element is used to chain the objects.
  6. */
  7. static PyCFunctionObject *free_list = NULL;
  8. static int numfree = 0;
  9. #ifndef PyCFunction_MAXFREELIST
  10. #define PyCFunction_MAXFREELIST 256
  11. #endif
  12. PyObject *
  13. PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module)
  14. {
  15. PyCFunctionObject *op;
  16. op = free_list;
  17. if (op != NULL) {
  18. free_list = (PyCFunctionObject *)(op->m_self);
  19. PyObject_INIT(op, &PyCFunction_Type);
  20. numfree--;
  21. }
  22. else {
  23. op = PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type);
  24. if (op == NULL)
  25. return NULL;
  26. }
  27. op->m_ml = ml;
  28. Py_XINCREF(self);
  29. op->m_self = self;
  30. Py_XINCREF(module);
  31. op->m_module = module;
  32. _PyObject_GC_TRACK(op);
  33. return (PyObject *)op;
  34. }
  35. PyCFunction
  36. PyCFunction_GetFunction(PyObject *op)
  37. {
  38. if (!PyCFunction_Check(op)) {
  39. PyErr_BadInternalCall();
  40. return NULL;
  41. }
  42. return ((PyCFunctionObject *)op) -> m_ml -> ml_meth;
  43. }
  44. PyObject *
  45. PyCFunction_GetSelf(PyObject *op)
  46. {
  47. if (!PyCFunction_Check(op)) {
  48. PyErr_BadInternalCall();
  49. return NULL;
  50. }
  51. return ((PyCFunctionObject *)op) -> m_self;
  52. }
  53. int
  54. PyCFunction_GetFlags(PyObject *op)
  55. {
  56. if (!PyCFunction_Check(op)) {
  57. PyErr_BadInternalCall();
  58. return -1;
  59. }
  60. return ((PyCFunctionObject *)op) -> m_ml -> ml_flags;
  61. }
  62. PyObject *
  63. PyCFunction_Call(PyObject *func, PyObject *arg, PyObject *kw)
  64. {
  65. PyCFunctionObject* f = (PyCFunctionObject*)func;
  66. PyCFunction meth = PyCFunction_GET_FUNCTION(func);
  67. PyObject *self = PyCFunction_GET_SELF(func);
  68. Py_ssize_t size;
  69. switch (PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST)) {
  70. case METH_VARARGS:
  71. if (kw == NULL || PyDict_Size(kw) == 0)
  72. return (*meth)(self, arg);
  73. break;
  74. case METH_VARARGS | METH_KEYWORDS:
  75. return (*(PyCFunctionWithKeywords)meth)(self, arg, kw);
  76. case METH_NOARGS:
  77. if (kw == NULL || PyDict_Size(kw) == 0) {
  78. size = PyTuple_GET_SIZE(arg);
  79. if (size == 0)
  80. return (*meth)(self, NULL);
  81. PyErr_Format(PyExc_TypeError,
  82. "%.200s() takes no arguments (%zd given)",
  83. f->m_ml->ml_name, size);
  84. return NULL;
  85. }
  86. break;
  87. case METH_O:
  88. if (kw == NULL || PyDict_Size(kw) == 0) {
  89. size = PyTuple_GET_SIZE(arg);
  90. if (size == 1)
  91. return (*meth)(self, PyTuple_GET_ITEM(arg, 0));
  92. PyErr_Format(PyExc_TypeError,
  93. "%.200s() takes exactly one argument (%zd given)",
  94. f->m_ml->ml_name, size);
  95. return NULL;
  96. }
  97. break;
  98. default:
  99. PyErr_SetString(PyExc_SystemError, "Bad call flags in "
  100. "PyCFunction_Call. METH_OLDARGS is no "
  101. "longer supported!");
  102. return NULL;
  103. }
  104. PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
  105. f->m_ml->ml_name);
  106. return NULL;
  107. }
  108. /* Methods (the standard built-in methods, that is) */
  109. static void
  110. meth_dealloc(PyCFunctionObject *m)
  111. {
  112. _PyObject_GC_UNTRACK(m);
  113. Py_XDECREF(m->m_self);
  114. Py_XDECREF(m->m_module);
  115. if (numfree < PyCFunction_MAXFREELIST) {
  116. m->m_self = (PyObject *)free_list;
  117. free_list = m;
  118. numfree++;
  119. }
  120. else {
  121. PyObject_GC_Del(m);
  122. }
  123. }
  124. static PyObject *
  125. meth_get__doc__(PyCFunctionObject *m, void *closure)
  126. {
  127. const char *doc = m->m_ml->ml_doc;
  128. if (doc != NULL)
  129. return PyUnicode_FromString(doc);
  130. Py_INCREF(Py_None);
  131. return Py_None;
  132. }
  133. static PyObject *
  134. meth_get__name__(PyCFunctionObject *m, void *closure)
  135. {
  136. return PyUnicode_FromString(m->m_ml->ml_name);
  137. }
  138. static int
  139. meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
  140. {
  141. Py_VISIT(m->m_self);
  142. Py_VISIT(m->m_module);
  143. return 0;
  144. }
  145. static PyObject *
  146. meth_get__self__(PyCFunctionObject *m, void *closure)
  147. {
  148. PyObject *self;
  149. self = m->m_self;
  150. if (self == NULL)
  151. self = Py_None;
  152. Py_INCREF(self);
  153. return self;
  154. }
  155. static PyGetSetDef meth_getsets [] = {
  156. {"__doc__", (getter)meth_get__doc__, NULL, NULL},
  157. {"__name__", (getter)meth_get__name__, NULL, NULL},
  158. {"__self__", (getter)meth_get__self__, NULL, NULL},
  159. {0}
  160. };
  161. #define OFF(x) offsetof(PyCFunctionObject, x)
  162. static PyMemberDef meth_members[] = {
  163. {"__module__", T_OBJECT, OFF(m_module), PY_WRITE_RESTRICTED},
  164. {NULL}
  165. };
  166. static PyObject *
  167. meth_repr(PyCFunctionObject *m)
  168. {
  169. if (m->m_self == NULL || PyModule_Check(m->m_self))
  170. return PyUnicode_FromFormat("<built-in function %s>",
  171. m->m_ml->ml_name);
  172. return PyUnicode_FromFormat("<built-in method %s of %s object at %p>",
  173. m->m_ml->ml_name,
  174. m->m_self->ob_type->tp_name,
  175. m->m_self);
  176. }
  177. static PyObject *
  178. meth_richcompare(PyObject *self, PyObject *other, int op)
  179. {
  180. PyCFunctionObject *a, *b;
  181. PyObject *res;
  182. int eq;
  183. if ((op != Py_EQ && op != Py_NE) ||
  184. !PyCFunction_Check(self) ||
  185. !PyCFunction_Check(other))
  186. {
  187. Py_INCREF(Py_NotImplemented);
  188. return Py_NotImplemented;
  189. }
  190. a = (PyCFunctionObject *)self;
  191. b = (PyCFunctionObject *)other;
  192. eq = a->m_self == b->m_self;
  193. if (eq)
  194. eq = a->m_ml->ml_meth == b->m_ml->ml_meth;
  195. if (op == Py_EQ)
  196. res = eq ? Py_True : Py_False;
  197. else
  198. res = eq ? Py_False : Py_True;
  199. Py_INCREF(res);
  200. return res;
  201. }
  202. static Py_hash_t
  203. meth_hash(PyCFunctionObject *a)
  204. {
  205. Py_hash_t x, y;
  206. if (a->m_self == NULL)
  207. x = 0;
  208. else {
  209. x = PyObject_Hash(a->m_self);
  210. if (x == -1)
  211. return -1;
  212. }
  213. y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
  214. if (y == -1)
  215. return -1;
  216. x ^= y;
  217. if (x == -1)
  218. x = -2;
  219. return x;
  220. }
  221. PyTypeObject PyCFunction_Type = {
  222. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  223. "builtin_function_or_method",
  224. sizeof(PyCFunctionObject),
  225. 0,
  226. (destructor)meth_dealloc, /* tp_dealloc */
  227. 0, /* tp_print */
  228. 0, /* tp_getattr */
  229. 0, /* tp_setattr */
  230. 0, /* tp_reserved */
  231. (reprfunc)meth_repr, /* tp_repr */
  232. 0, /* tp_as_number */
  233. 0, /* tp_as_sequence */
  234. 0, /* tp_as_mapping */
  235. (hashfunc)meth_hash, /* tp_hash */
  236. PyCFunction_Call, /* tp_call */
  237. 0, /* tp_str */
  238. PyObject_GenericGetAttr, /* tp_getattro */
  239. 0, /* tp_setattro */
  240. 0, /* tp_as_buffer */
  241. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
  242. 0, /* tp_doc */
  243. (traverseproc)meth_traverse, /* tp_traverse */
  244. 0, /* tp_clear */
  245. meth_richcompare, /* tp_richcompare */
  246. 0, /* tp_weaklistoffset */
  247. 0, /* tp_iter */
  248. 0, /* tp_iternext */
  249. 0, /* tp_methods */
  250. meth_members, /* tp_members */
  251. meth_getsets, /* tp_getset */
  252. 0, /* tp_base */
  253. 0, /* tp_dict */
  254. };
  255. /* Clear out the free list */
  256. int
  257. PyCFunction_ClearFreeList(void)
  258. {
  259. int freelist_size = numfree;
  260. while (free_list) {
  261. PyCFunctionObject *v = free_list;
  262. free_list = (PyCFunctionObject *)(v->m_self);
  263. PyObject_GC_Del(v);
  264. numfree--;
  265. }
  266. assert(numfree == 0);
  267. return freelist_size;
  268. }
  269. void
  270. PyCFunction_Fini(void)
  271. {
  272. (void)PyCFunction_ClearFreeList();
  273. }
  274. /* PyCFunction_New() is now just a macro that calls PyCFunction_NewEx(),
  275. but it's part of the API so we need to keep a function around that
  276. existing C extensions can call.
  277. */
  278. #undef PyCFunction_New
  279. PyAPI_FUNC(PyObject *) PyCFunction_New(PyMethodDef *, PyObject *);
  280. PyObject *
  281. PyCFunction_New(PyMethodDef *ml, PyObject *self)
  282. {
  283. return PyCFunction_NewEx(ml, self, NULL);
  284. }