methodobject.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /* Method object implementation */
  2. #include "Python.h"
  3. #include "pycore_ceval.h" // _Py_EnterRecursiveCallTstate()
  4. #include "pycore_object.h"
  5. #include "pycore_pyerrors.h"
  6. #include "pycore_pystate.h" // _PyThreadState_GET()
  7. #include "structmember.h" // PyMemberDef
  8. /* undefine macro trampoline to PyCFunction_NewEx */
  9. #undef PyCFunction_New
  10. /* undefine macro trampoline to PyCMethod_New */
  11. #undef PyCFunction_NewEx
  12. /* Forward declarations */
  13. static PyObject * cfunction_vectorcall_FASTCALL(
  14. PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
  15. static PyObject * cfunction_vectorcall_FASTCALL_KEYWORDS(
  16. PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
  17. static PyObject * cfunction_vectorcall_FASTCALL_KEYWORDS_METHOD(
  18. PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
  19. static PyObject * cfunction_vectorcall_NOARGS(
  20. PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
  21. static PyObject * cfunction_vectorcall_O(
  22. PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
  23. static PyObject * cfunction_call(
  24. PyObject *func, PyObject *args, PyObject *kwargs);
  25. PyObject *
  26. PyCFunction_New(PyMethodDef *ml, PyObject *self)
  27. {
  28. return PyCFunction_NewEx(ml, self, NULL);
  29. }
  30. PyObject *
  31. PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module)
  32. {
  33. return PyCMethod_New(ml, self, module, NULL);
  34. }
  35. PyObject *
  36. PyCMethod_New(PyMethodDef *ml, PyObject *self, PyObject *module, PyTypeObject *cls)
  37. {
  38. /* Figure out correct vectorcall function to use */
  39. vectorcallfunc vectorcall;
  40. switch (ml->ml_flags & (METH_VARARGS | METH_FASTCALL | METH_NOARGS |
  41. METH_O | METH_KEYWORDS | METH_METHOD))
  42. {
  43. case METH_VARARGS:
  44. case METH_VARARGS | METH_KEYWORDS:
  45. /* For METH_VARARGS functions, it's more efficient to use tp_call
  46. * instead of vectorcall. */
  47. vectorcall = NULL;
  48. break;
  49. case METH_FASTCALL:
  50. vectorcall = cfunction_vectorcall_FASTCALL;
  51. break;
  52. case METH_FASTCALL | METH_KEYWORDS:
  53. vectorcall = cfunction_vectorcall_FASTCALL_KEYWORDS;
  54. break;
  55. case METH_NOARGS:
  56. vectorcall = cfunction_vectorcall_NOARGS;
  57. break;
  58. case METH_O:
  59. vectorcall = cfunction_vectorcall_O;
  60. break;
  61. case METH_METHOD | METH_FASTCALL | METH_KEYWORDS:
  62. vectorcall = cfunction_vectorcall_FASTCALL_KEYWORDS_METHOD;
  63. break;
  64. default:
  65. PyErr_Format(PyExc_SystemError,
  66. "%s() method: bad call flags", ml->ml_name);
  67. return NULL;
  68. }
  69. PyCFunctionObject *op = NULL;
  70. if (ml->ml_flags & METH_METHOD) {
  71. if (!cls) {
  72. PyErr_SetString(PyExc_SystemError,
  73. "attempting to create PyCMethod with a METH_METHOD "
  74. "flag but no class");
  75. return NULL;
  76. }
  77. PyCMethodObject *om = PyObject_GC_New(PyCMethodObject, &PyCMethod_Type);
  78. if (om == NULL) {
  79. return NULL;
  80. }
  81. om->mm_class = (PyTypeObject*)Py_NewRef(cls);
  82. op = (PyCFunctionObject *)om;
  83. } else {
  84. if (cls) {
  85. PyErr_SetString(PyExc_SystemError,
  86. "attempting to create PyCFunction with class "
  87. "but no METH_METHOD flag");
  88. return NULL;
  89. }
  90. op = PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type);
  91. if (op == NULL) {
  92. return NULL;
  93. }
  94. }
  95. op->m_weakreflist = NULL;
  96. op->m_ml = ml;
  97. op->m_self = Py_XNewRef(self);
  98. op->m_module = Py_XNewRef(module);
  99. op->vectorcall = vectorcall;
  100. _PyObject_GC_TRACK(op);
  101. return (PyObject *)op;
  102. }
  103. PyCFunction
  104. PyCFunction_GetFunction(PyObject *op)
  105. {
  106. if (!PyCFunction_Check(op)) {
  107. PyErr_BadInternalCall();
  108. return NULL;
  109. }
  110. return PyCFunction_GET_FUNCTION(op);
  111. }
  112. PyObject *
  113. PyCFunction_GetSelf(PyObject *op)
  114. {
  115. if (!PyCFunction_Check(op)) {
  116. PyErr_BadInternalCall();
  117. return NULL;
  118. }
  119. return PyCFunction_GET_SELF(op);
  120. }
  121. int
  122. PyCFunction_GetFlags(PyObject *op)
  123. {
  124. if (!PyCFunction_Check(op)) {
  125. PyErr_BadInternalCall();
  126. return -1;
  127. }
  128. return PyCFunction_GET_FLAGS(op);
  129. }
  130. PyTypeObject *
  131. PyCMethod_GetClass(PyObject *op)
  132. {
  133. if (!PyCFunction_Check(op)) {
  134. PyErr_BadInternalCall();
  135. return NULL;
  136. }
  137. return PyCFunction_GET_CLASS(op);
  138. }
  139. /* Methods (the standard built-in methods, that is) */
  140. static void
  141. meth_dealloc(PyCFunctionObject *m)
  142. {
  143. // The Py_TRASHCAN mechanism requires that we be able to
  144. // call PyObject_GC_UnTrack twice on an object.
  145. PyObject_GC_UnTrack(m);
  146. Py_TRASHCAN_BEGIN(m, meth_dealloc);
  147. if (m->m_weakreflist != NULL) {
  148. PyObject_ClearWeakRefs((PyObject*) m);
  149. }
  150. // Dereference class before m_self: PyCFunction_GET_CLASS accesses
  151. // PyMethodDef m_ml, which could be kept alive by m_self
  152. Py_XDECREF(PyCFunction_GET_CLASS(m));
  153. Py_XDECREF(m->m_self);
  154. Py_XDECREF(m->m_module);
  155. PyObject_GC_Del(m);
  156. Py_TRASHCAN_END;
  157. }
  158. static PyObject *
  159. meth_reduce(PyCFunctionObject *m, PyObject *Py_UNUSED(ignored))
  160. {
  161. if (m->m_self == NULL || PyModule_Check(m->m_self))
  162. return PyUnicode_FromString(m->m_ml->ml_name);
  163. return Py_BuildValue("N(Os)", _PyEval_GetBuiltin(&_Py_ID(getattr)),
  164. m->m_self, m->m_ml->ml_name);
  165. }
  166. static PyMethodDef meth_methods[] = {
  167. {"__reduce__", (PyCFunction)meth_reduce, METH_NOARGS, NULL},
  168. {NULL, NULL}
  169. };
  170. static PyObject *
  171. meth_get__text_signature__(PyCFunctionObject *m, void *closure)
  172. {
  173. return _PyType_GetTextSignatureFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
  174. }
  175. static PyObject *
  176. meth_get__doc__(PyCFunctionObject *m, void *closure)
  177. {
  178. return _PyType_GetDocFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
  179. }
  180. static PyObject *
  181. meth_get__name__(PyCFunctionObject *m, void *closure)
  182. {
  183. return PyUnicode_FromString(m->m_ml->ml_name);
  184. }
  185. static PyObject *
  186. meth_get__qualname__(PyCFunctionObject *m, void *closure)
  187. {
  188. /* If __self__ is a module or NULL, return m.__name__
  189. (e.g. len.__qualname__ == 'len')
  190. If __self__ is a type, return m.__self__.__qualname__ + '.' + m.__name__
  191. (e.g. dict.fromkeys.__qualname__ == 'dict.fromkeys')
  192. Otherwise return type(m.__self__).__qualname__ + '.' + m.__name__
  193. (e.g. [].append.__qualname__ == 'list.append') */
  194. PyObject *type, *type_qualname, *res;
  195. if (m->m_self == NULL || PyModule_Check(m->m_self))
  196. return PyUnicode_FromString(m->m_ml->ml_name);
  197. type = PyType_Check(m->m_self) ? m->m_self : (PyObject*)Py_TYPE(m->m_self);
  198. type_qualname = PyObject_GetAttr(type, &_Py_ID(__qualname__));
  199. if (type_qualname == NULL)
  200. return NULL;
  201. if (!PyUnicode_Check(type_qualname)) {
  202. PyErr_SetString(PyExc_TypeError, "<method>.__class__."
  203. "__qualname__ is not a unicode object");
  204. Py_XDECREF(type_qualname);
  205. return NULL;
  206. }
  207. res = PyUnicode_FromFormat("%S.%s", type_qualname, m->m_ml->ml_name);
  208. Py_DECREF(type_qualname);
  209. return res;
  210. }
  211. static int
  212. meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
  213. {
  214. Py_VISIT(PyCFunction_GET_CLASS(m));
  215. Py_VISIT(m->m_self);
  216. Py_VISIT(m->m_module);
  217. return 0;
  218. }
  219. static PyObject *
  220. meth_get__self__(PyCFunctionObject *m, void *closure)
  221. {
  222. PyObject *self;
  223. self = PyCFunction_GET_SELF(m);
  224. if (self == NULL)
  225. self = Py_None;
  226. return Py_NewRef(self);
  227. }
  228. static PyGetSetDef meth_getsets [] = {
  229. {"__doc__", (getter)meth_get__doc__, NULL, NULL},
  230. {"__name__", (getter)meth_get__name__, NULL, NULL},
  231. {"__qualname__", (getter)meth_get__qualname__, NULL, NULL},
  232. {"__self__", (getter)meth_get__self__, NULL, NULL},
  233. {"__text_signature__", (getter)meth_get__text_signature__, NULL, NULL},
  234. {0}
  235. };
  236. #define OFF(x) offsetof(PyCFunctionObject, x)
  237. static PyMemberDef meth_members[] = {
  238. {"__module__", T_OBJECT, OFF(m_module), 0},
  239. {NULL}
  240. };
  241. static PyObject *
  242. meth_repr(PyCFunctionObject *m)
  243. {
  244. if (m->m_self == NULL || PyModule_Check(m->m_self))
  245. return PyUnicode_FromFormat("<built-in function %s>",
  246. m->m_ml->ml_name);
  247. return PyUnicode_FromFormat("<built-in method %s of %s object at %p>",
  248. m->m_ml->ml_name,
  249. Py_TYPE(m->m_self)->tp_name,
  250. m->m_self);
  251. }
  252. static PyObject *
  253. meth_richcompare(PyObject *self, PyObject *other, int op)
  254. {
  255. PyCFunctionObject *a, *b;
  256. PyObject *res;
  257. int eq;
  258. if ((op != Py_EQ && op != Py_NE) ||
  259. !PyCFunction_Check(self) ||
  260. !PyCFunction_Check(other))
  261. {
  262. Py_RETURN_NOTIMPLEMENTED;
  263. }
  264. a = (PyCFunctionObject *)self;
  265. b = (PyCFunctionObject *)other;
  266. eq = a->m_self == b->m_self;
  267. if (eq)
  268. eq = a->m_ml->ml_meth == b->m_ml->ml_meth;
  269. if (op == Py_EQ)
  270. res = eq ? Py_True : Py_False;
  271. else
  272. res = eq ? Py_False : Py_True;
  273. return Py_NewRef(res);
  274. }
  275. static Py_hash_t
  276. meth_hash(PyCFunctionObject *a)
  277. {
  278. Py_hash_t x, y;
  279. x = _Py_HashPointer(a->m_self);
  280. y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
  281. x ^= y;
  282. if (x == -1)
  283. x = -2;
  284. return x;
  285. }
  286. PyTypeObject PyCFunction_Type = {
  287. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  288. "builtin_function_or_method",
  289. sizeof(PyCFunctionObject),
  290. 0,
  291. (destructor)meth_dealloc, /* tp_dealloc */
  292. offsetof(PyCFunctionObject, vectorcall), /* tp_vectorcall_offset */
  293. 0, /* tp_getattr */
  294. 0, /* tp_setattr */
  295. 0, /* tp_as_async */
  296. (reprfunc)meth_repr, /* tp_repr */
  297. 0, /* tp_as_number */
  298. 0, /* tp_as_sequence */
  299. 0, /* tp_as_mapping */
  300. (hashfunc)meth_hash, /* tp_hash */
  301. cfunction_call, /* tp_call */
  302. 0, /* tp_str */
  303. PyObject_GenericGetAttr, /* tp_getattro */
  304. 0, /* tp_setattro */
  305. 0, /* tp_as_buffer */
  306. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
  307. Py_TPFLAGS_HAVE_VECTORCALL, /* tp_flags */
  308. 0, /* tp_doc */
  309. (traverseproc)meth_traverse, /* tp_traverse */
  310. 0, /* tp_clear */
  311. meth_richcompare, /* tp_richcompare */
  312. offsetof(PyCFunctionObject, m_weakreflist), /* tp_weaklistoffset */
  313. 0, /* tp_iter */
  314. 0, /* tp_iternext */
  315. meth_methods, /* tp_methods */
  316. meth_members, /* tp_members */
  317. meth_getsets, /* tp_getset */
  318. 0, /* tp_base */
  319. 0, /* tp_dict */
  320. };
  321. PyTypeObject PyCMethod_Type = {
  322. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  323. .tp_name = "builtin_method",
  324. .tp_basicsize = sizeof(PyCMethodObject),
  325. .tp_base = &PyCFunction_Type,
  326. };
  327. /* Vectorcall functions for each of the PyCFunction calling conventions,
  328. * except for METH_VARARGS (possibly combined with METH_KEYWORDS) which
  329. * doesn't use vectorcall.
  330. *
  331. * First, common helpers
  332. */
  333. static inline int
  334. cfunction_check_kwargs(PyThreadState *tstate, PyObject *func, PyObject *kwnames)
  335. {
  336. assert(!_PyErr_Occurred(tstate));
  337. assert(PyCFunction_Check(func));
  338. if (kwnames && PyTuple_GET_SIZE(kwnames)) {
  339. PyObject *funcstr = _PyObject_FunctionStr(func);
  340. if (funcstr != NULL) {
  341. _PyErr_Format(tstate, PyExc_TypeError,
  342. "%U takes no keyword arguments", funcstr);
  343. Py_DECREF(funcstr);
  344. }
  345. return -1;
  346. }
  347. return 0;
  348. }
  349. typedef void (*funcptr)(void);
  350. static inline funcptr
  351. cfunction_enter_call(PyThreadState *tstate, PyObject *func)
  352. {
  353. if (_Py_EnterRecursiveCallTstate(tstate, " while calling a Python object")) {
  354. return NULL;
  355. }
  356. return (funcptr)PyCFunction_GET_FUNCTION(func);
  357. }
  358. /* Now the actual vectorcall functions */
  359. static PyObject *
  360. cfunction_vectorcall_FASTCALL(
  361. PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
  362. {
  363. PyThreadState *tstate = _PyThreadState_GET();
  364. if (cfunction_check_kwargs(tstate, func, kwnames)) {
  365. return NULL;
  366. }
  367. Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
  368. _PyCFunctionFast meth = (_PyCFunctionFast)
  369. cfunction_enter_call(tstate, func);
  370. if (meth == NULL) {
  371. return NULL;
  372. }
  373. PyObject *result = meth(PyCFunction_GET_SELF(func), args, nargs);
  374. _Py_LeaveRecursiveCallTstate(tstate);
  375. return result;
  376. }
  377. static PyObject *
  378. cfunction_vectorcall_FASTCALL_KEYWORDS(
  379. PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
  380. {
  381. PyThreadState *tstate = _PyThreadState_GET();
  382. Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
  383. _PyCFunctionFastWithKeywords meth = (_PyCFunctionFastWithKeywords)
  384. cfunction_enter_call(tstate, func);
  385. if (meth == NULL) {
  386. return NULL;
  387. }
  388. PyObject *result = meth(PyCFunction_GET_SELF(func), args, nargs, kwnames);
  389. _Py_LeaveRecursiveCallTstate(tstate);
  390. return result;
  391. }
  392. static PyObject *
  393. cfunction_vectorcall_FASTCALL_KEYWORDS_METHOD(
  394. PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
  395. {
  396. PyThreadState *tstate = _PyThreadState_GET();
  397. PyTypeObject *cls = PyCFunction_GET_CLASS(func);
  398. Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
  399. PyCMethod meth = (PyCMethod)cfunction_enter_call(tstate, func);
  400. if (meth == NULL) {
  401. return NULL;
  402. }
  403. PyObject *result = meth(PyCFunction_GET_SELF(func), cls, args, nargs, kwnames);
  404. _Py_LeaveRecursiveCallTstate(tstate);
  405. return result;
  406. }
  407. static PyObject *
  408. cfunction_vectorcall_NOARGS(
  409. PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
  410. {
  411. PyThreadState *tstate = _PyThreadState_GET();
  412. if (cfunction_check_kwargs(tstate, func, kwnames)) {
  413. return NULL;
  414. }
  415. Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
  416. if (nargs != 0) {
  417. PyObject *funcstr = _PyObject_FunctionStr(func);
  418. if (funcstr != NULL) {
  419. _PyErr_Format(tstate, PyExc_TypeError,
  420. "%U takes no arguments (%zd given)", funcstr, nargs);
  421. Py_DECREF(funcstr);
  422. }
  423. return NULL;
  424. }
  425. PyCFunction meth = (PyCFunction)cfunction_enter_call(tstate, func);
  426. if (meth == NULL) {
  427. return NULL;
  428. }
  429. PyObject *result = _PyCFunction_TrampolineCall(
  430. meth, PyCFunction_GET_SELF(func), NULL);
  431. _Py_LeaveRecursiveCallTstate(tstate);
  432. return result;
  433. }
  434. static PyObject *
  435. cfunction_vectorcall_O(
  436. PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
  437. {
  438. PyThreadState *tstate = _PyThreadState_GET();
  439. if (cfunction_check_kwargs(tstate, func, kwnames)) {
  440. return NULL;
  441. }
  442. Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
  443. if (nargs != 1) {
  444. PyObject *funcstr = _PyObject_FunctionStr(func);
  445. if (funcstr != NULL) {
  446. _PyErr_Format(tstate, PyExc_TypeError,
  447. "%U takes exactly one argument (%zd given)", funcstr, nargs);
  448. Py_DECREF(funcstr);
  449. }
  450. return NULL;
  451. }
  452. PyCFunction meth = (PyCFunction)cfunction_enter_call(tstate, func);
  453. if (meth == NULL) {
  454. return NULL;
  455. }
  456. PyObject *result = _PyCFunction_TrampolineCall(
  457. meth, PyCFunction_GET_SELF(func), args[0]);
  458. _Py_LeaveRecursiveCallTstate(tstate);
  459. return result;
  460. }
  461. static PyObject *
  462. cfunction_call(PyObject *func, PyObject *args, PyObject *kwargs)
  463. {
  464. assert(kwargs == NULL || PyDict_Check(kwargs));
  465. PyThreadState *tstate = _PyThreadState_GET();
  466. assert(!_PyErr_Occurred(tstate));
  467. int flags = PyCFunction_GET_FLAGS(func);
  468. if (!(flags & METH_VARARGS)) {
  469. /* If this is not a METH_VARARGS function, delegate to vectorcall */
  470. return PyVectorcall_Call(func, args, kwargs);
  471. }
  472. /* For METH_VARARGS, we cannot use vectorcall as the vectorcall pointer
  473. * is NULL. This is intentional, since vectorcall would be slower. */
  474. PyCFunction meth = PyCFunction_GET_FUNCTION(func);
  475. PyObject *self = PyCFunction_GET_SELF(func);
  476. PyObject *result;
  477. if (flags & METH_KEYWORDS) {
  478. result = _PyCFunctionWithKeywords_TrampolineCall(
  479. (*(PyCFunctionWithKeywords)(void(*)(void))meth),
  480. self, args, kwargs);
  481. }
  482. else {
  483. if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
  484. _PyErr_Format(tstate, PyExc_TypeError,
  485. "%.200s() takes no keyword arguments",
  486. ((PyCFunctionObject*)func)->m_ml->ml_name);
  487. return NULL;
  488. }
  489. result = _PyCFunction_TrampolineCall(meth, self, args);
  490. }
  491. return _Py_CheckFunctionResult(tstate, func, result, NULL);
  492. }
  493. #if defined(__EMSCRIPTEN__) && defined(PY_CALL_TRAMPOLINE)
  494. #error #include <emscripten.h>
  495. EM_JS(PyObject*, _PyCFunctionWithKeywords_TrampolineCall, (PyCFunctionWithKeywords func, PyObject *self, PyObject *args, PyObject *kw), {
  496. return wasmTable.get(func)(self, args, kw);
  497. });
  498. #endif