PageRenderTime 45ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/necco/python/Objects/methodobject.c

https://github.com/brosner/cleese
C | 118 lines | 104 code | 7 blank | 7 comment | 31 complexity | 909cf6f4cff1812e9ef89ee5c2de21dd MD5 | raw file
  1. /* Method object implementation */
  2. #include "Python.h"
  3. static PyCFunctionObject *free_list = NULL;
  4. PyObject *
  5. PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module)
  6. {
  7. PyCFunctionObject *op;
  8. op = free_list;
  9. if (op != NULL) {
  10. free_list = (PyCFunctionObject *)(op->m_self);
  11. PyObject_INIT(op, &PyCFunction_Type);
  12. }
  13. else {
  14. op = PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type);
  15. if (op == NULL)
  16. return NULL;
  17. }
  18. op->m_ml = ml;
  19. Py_XINCREF(self);
  20. op->m_self = self;
  21. Py_XINCREF(module);
  22. op->m_module = module;
  23. _PyObject_GC_TRACK(op);
  24. return (PyObject *)op;
  25. }
  26. PyObject *
  27. PyCFunction_Call(PyObject *func, PyObject *arg, PyObject *kw)
  28. {
  29. // PyCFunctionObject* f = (PyCFunctionObject*)func;
  30. PyCFunction meth = PyCFunction_GET_FUNCTION(func);
  31. PyObject *self = PyCFunction_GET_SELF(func);
  32. int size;
  33. switch (PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC)) {
  34. case METH_VARARGS:
  35. if (kw == NULL || PyDict_Size(kw) == 0)
  36. return (*meth)(self, arg);
  37. break;
  38. case METH_VARARGS | METH_KEYWORDS:
  39. case METH_OLDARGS | METH_KEYWORDS:
  40. return (*(PyCFunctionWithKeywords)meth)(self, arg, kw);
  41. case METH_NOARGS:
  42. if (kw == NULL || PyDict_Size(kw) == 0) {
  43. size = PyTuple_GET_SIZE(arg);
  44. if (size == 0)
  45. return (*meth)(self, NULL);
  46. /* ERROR */
  47. return NULL;
  48. }
  49. break;
  50. case METH_O:
  51. if (kw == NULL || PyDict_Size(kw) == 0) {
  52. size = PyTuple_GET_SIZE(arg);
  53. if (size == 1)
  54. return (*meth)(self, PyTuple_GET_ITEM(arg, 0));
  55. /* ERROR */
  56. return NULL;
  57. }
  58. break;
  59. case METH_OLDARGS:
  60. /* the really old style */
  61. if (kw == NULL || PyDict_Size(kw) == 0) {
  62. size = PyTuple_GET_SIZE(arg);
  63. if (size == 1)
  64. arg = PyTuple_GET_ITEM(arg, 0);
  65. else if (size == 0)
  66. arg = NULL;
  67. return (*meth)(self, arg);
  68. }
  69. break;
  70. default:
  71. /* ERROR */
  72. return NULL;
  73. }
  74. /* ERROR */
  75. return NULL;
  76. }
  77. PyTypeObject PyCFunction_Type = {
  78. PyObject_HEAD_INIT(&PyType_Type)
  79. 0,
  80. "builtin_function_or_method",
  81. sizeof(PyCFunctionObject),
  82. 0,
  83. 0, //(destructor)meth_dealloc, /* tp_dealloc */
  84. 0, /* tp_print */
  85. 0, /* tp_getattr */
  86. 0, /* tp_setattr */
  87. 0, //(cmpfunc)meth_compare, /* tp_compare */
  88. 0, //(reprfunc)meth_repr, /* tp_repr */
  89. 0, /* tp_as_number */
  90. 0, /* tp_as_sequence */
  91. 0, /* tp_as_mapping */
  92. 0, //(hashfunc)meth_hash, /* tp_hash */
  93. PyCFunction_Call, /* tp_call */
  94. 0, /* tp_str */
  95. PyObject_GenericGetAttr, /* tp_getattro */
  96. 0, /* tp_setattro */
  97. 0, /* tp_as_buffer */
  98. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
  99. 0, /* tp_doc */
  100. 0, //(traverseproc)meth_traverse, /* tp_traverse */
  101. 0, /* tp_clear */
  102. 0, /* tp_richcompare */
  103. 0, /* tp_weaklistoffset */
  104. 0, /* tp_iter */
  105. 0, /* tp_iternext */
  106. 0, /* tp_methods */
  107. 0, //meth_members, /* tp_members */
  108. 0, //meth_getsets, /* tp_getset */
  109. 0, /* tp_base */
  110. 0, /* tp_dict */
  111. };