1991-02-19 08:39:46 -04:00
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
/* Method object implementation */
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
#include "Python.h"
|
2018-11-21 17:27:47 -04:00
|
|
|
#include "pycore_object.h"
|
2019-11-04 19:51:22 -04:00
|
|
|
#include "pycore_pyerrors.h"
|
2018-11-12 11:53:38 -04:00
|
|
|
#include "pycore_pymem.h"
|
|
|
|
#include "pycore_pystate.h"
|
2003-01-31 14:33:18 -04:00
|
|
|
#include "structmember.h"
|
1990-10-14 09:07:46 -03:00
|
|
|
|
2012-12-26 17:08:54 -04:00
|
|
|
/* undefine macro trampoline to PyCFunction_NewEx */
|
|
|
|
#undef PyCFunction_New
|
|
|
|
|
2019-07-05 09:48:24 -03:00
|
|
|
/* Forward declarations */
|
|
|
|
static PyObject * cfunction_vectorcall_FASTCALL(
|
|
|
|
PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
|
|
|
|
static PyObject * cfunction_vectorcall_FASTCALL_KEYWORDS(
|
|
|
|
PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
|
|
|
|
static PyObject * cfunction_vectorcall_NOARGS(
|
|
|
|
PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
|
|
|
|
static PyObject * cfunction_vectorcall_O(
|
|
|
|
PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
|
2019-09-11 08:01:01 -03:00
|
|
|
static PyObject * cfunction_call(
|
|
|
|
PyObject *func, PyObject *args, PyObject *kwargs);
|
2019-07-05 09:48:24 -03:00
|
|
|
|
|
|
|
|
2018-09-12 16:06:42 -03:00
|
|
|
PyObject *
|
2012-12-25 07:32:35 -04:00
|
|
|
PyCFunction_New(PyMethodDef *ml, PyObject *self)
|
|
|
|
{
|
|
|
|
return PyCFunction_NewEx(ml, self, NULL);
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
2003-01-31 14:33:18 -04:00
|
|
|
PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2019-07-05 09:48:24 -03:00
|
|
|
/* Figure out correct vectorcall function to use */
|
|
|
|
vectorcallfunc vectorcall;
|
|
|
|
switch (ml->ml_flags & (METH_VARARGS | METH_FASTCALL | METH_NOARGS | METH_O | METH_KEYWORDS))
|
|
|
|
{
|
|
|
|
case METH_VARARGS:
|
|
|
|
case METH_VARARGS | METH_KEYWORDS:
|
|
|
|
/* For METH_VARARGS functions, it's more efficient to use tp_call
|
|
|
|
* instead of vectorcall. */
|
|
|
|
vectorcall = NULL;
|
|
|
|
break;
|
|
|
|
case METH_FASTCALL:
|
|
|
|
vectorcall = cfunction_vectorcall_FASTCALL;
|
|
|
|
break;
|
|
|
|
case METH_FASTCALL | METH_KEYWORDS:
|
|
|
|
vectorcall = cfunction_vectorcall_FASTCALL_KEYWORDS;
|
|
|
|
break;
|
|
|
|
case METH_NOARGS:
|
|
|
|
vectorcall = cfunction_vectorcall_NOARGS;
|
|
|
|
break;
|
|
|
|
case METH_O:
|
|
|
|
vectorcall = cfunction_vectorcall_O;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
PyErr_SetString(PyExc_SystemError, "bad call flags");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-07-26 03:05:50 -03:00
|
|
|
PyCFunctionObject *op =
|
|
|
|
PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type);
|
|
|
|
if (op == NULL) {
|
|
|
|
return NULL;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2014-08-06 20:31:40 -03:00
|
|
|
op->m_weakreflist = NULL;
|
2010-05-09 12:52:27 -03:00
|
|
|
op->m_ml = ml;
|
|
|
|
Py_XINCREF(self);
|
|
|
|
op->m_self = self;
|
|
|
|
Py_XINCREF(module);
|
|
|
|
op->m_module = module;
|
2019-07-05 09:48:24 -03:00
|
|
|
op->vectorcall = vectorcall;
|
2010-05-09 12:52:27 -03:00
|
|
|
_PyObject_GC_TRACK(op);
|
|
|
|
return (PyObject *)op;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyCFunction
|
2000-07-09 03:03:25 -03:00
|
|
|
PyCFunction_GetFunction(PyObject *op)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!PyCFunction_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-12-23 07:40:16 -04:00
|
|
|
return PyCFunction_GET_FUNCTION(op);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
2000-07-09 03:03:25 -03:00
|
|
|
PyCFunction_GetSelf(PyObject *op)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!PyCFunction_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-12-23 07:40:16 -04:00
|
|
|
return PyCFunction_GET_SELF(op);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1991-12-16 09:07:24 -04:00
|
|
|
int
|
2000-07-09 03:03:25 -03:00
|
|
|
PyCFunction_GetFlags(PyObject *op)
|
1991-12-16 09:07:24 -04:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!PyCFunction_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return -1;
|
|
|
|
}
|
2011-12-23 07:40:16 -04:00
|
|
|
return PyCFunction_GET_FLAGS(op);
|
1991-12-16 09:07:24 -04:00
|
|
|
}
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
/* Methods (the standard built-in methods, that is) */
|
|
|
|
|
|
|
|
static void
|
2000-07-09 03:03:25 -03:00
|
|
|
meth_dealloc(PyCFunctionObject *m)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
_PyObject_GC_UNTRACK(m);
|
2014-08-06 20:31:40 -03:00
|
|
|
if (m->m_weakreflist != NULL) {
|
|
|
|
PyObject_ClearWeakRefs((PyObject*) m);
|
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_XDECREF(m->m_self);
|
|
|
|
Py_XDECREF(m->m_module);
|
2019-07-26 03:05:50 -03:00
|
|
|
PyObject_GC_Del(m);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
2013-11-24 06:41:05 -04:00
|
|
|
static PyObject *
|
2018-04-29 15:59:33 -03:00
|
|
|
meth_reduce(PyCFunctionObject *m, PyObject *Py_UNUSED(ignored))
|
2013-11-24 06:41:05 -04:00
|
|
|
{
|
|
|
|
_Py_IDENTIFIER(getattr);
|
|
|
|
|
|
|
|
if (m->m_self == NULL || PyModule_Check(m->m_self))
|
|
|
|
return PyUnicode_FromString(m->m_ml->ml_name);
|
|
|
|
|
2018-12-11 02:28:18 -04:00
|
|
|
return Py_BuildValue("N(Os)", _PyEval_GetBuiltinId(&PyId_getattr),
|
|
|
|
m->m_self, m->m_ml->ml_name);
|
2013-11-24 06:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyMethodDef meth_methods[] = {
|
|
|
|
{"__reduce__", (PyCFunction)meth_reduce, METH_NOARGS, NULL},
|
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
2013-11-23 19:37:55 -04:00
|
|
|
static PyObject *
|
|
|
|
meth_get__text_signature__(PyCFunctionObject *m, void *closure)
|
|
|
|
{
|
2014-02-09 02:15:29 -04:00
|
|
|
return _PyType_GetTextSignatureFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
|
2013-11-23 19:37:55 -04:00
|
|
|
}
|
|
|
|
|
2001-08-02 01:15:00 -03:00
|
|
|
static PyObject *
|
|
|
|
meth_get__doc__(PyCFunctionObject *m, void *closure)
|
|
|
|
{
|
2014-02-09 02:15:29 -04:00
|
|
|
return _PyType_GetDocFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
|
2001-08-02 01:15:00 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
meth_get__name__(PyCFunctionObject *m, void *closure)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
return PyUnicode_FromString(m->m_ml->ml_name);
|
2001-08-02 01:15:00 -03:00
|
|
|
}
|
|
|
|
|
2011-12-23 07:40:16 -04:00
|
|
|
static PyObject *
|
|
|
|
meth_get__qualname__(PyCFunctionObject *m, void *closure)
|
|
|
|
{
|
|
|
|
/* If __self__ is a module or NULL, return m.__name__
|
|
|
|
(e.g. len.__qualname__ == 'len')
|
|
|
|
|
|
|
|
If __self__ is a type, return m.__self__.__qualname__ + '.' + m.__name__
|
|
|
|
(e.g. dict.fromkeys.__qualname__ == 'dict.fromkeys')
|
|
|
|
|
|
|
|
Otherwise return type(m.__self__).__qualname__ + '.' + m.__name__
|
|
|
|
(e.g. [].append.__qualname__ == 'list.append') */
|
|
|
|
PyObject *type, *type_qualname, *res;
|
|
|
|
_Py_IDENTIFIER(__qualname__);
|
|
|
|
|
|
|
|
if (m->m_self == NULL || PyModule_Check(m->m_self))
|
|
|
|
return PyUnicode_FromString(m->m_ml->ml_name);
|
|
|
|
|
|
|
|
type = PyType_Check(m->m_self) ? m->m_self : (PyObject*)Py_TYPE(m->m_self);
|
|
|
|
|
|
|
|
type_qualname = _PyObject_GetAttrId(type, &PyId___qualname__);
|
|
|
|
if (type_qualname == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!PyUnicode_Check(type_qualname)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError, "<method>.__class__."
|
|
|
|
"__qualname__ is not a unicode object");
|
|
|
|
Py_XDECREF(type_qualname);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = PyUnicode_FromFormat("%S.%s", type_qualname, m->m_ml->ml_name);
|
|
|
|
Py_DECREF(type_qualname);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-07-12 10:27:35 -03:00
|
|
|
static int
|
|
|
|
meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_VISIT(m->m_self);
|
|
|
|
Py_VISIT(m->m_module);
|
|
|
|
return 0;
|
2001-07-12 10:27:35 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2001-08-02 01:15:00 -03:00
|
|
|
meth_get__self__(PyCFunctionObject *m, void *closure)
|
1995-01-07 08:34:58 -04:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *self;
|
Merged revisions 55270-55324 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/p3yk
........
r55271 | fred.drake | 2007-05-11 10:14:47 -0700 (Fri, 11 May 2007) | 3 lines
remove jpeg, panel libraries for SGI; there is more IRIX stuff left over,
I guess that should be removed too, but will leave for someone who is sure
........
r55280 | fred.drake | 2007-05-11 19:11:37 -0700 (Fri, 11 May 2007) | 1 line
remove mention of file that has been removed
........
r55301 | brett.cannon | 2007-05-13 17:38:05 -0700 (Sun, 13 May 2007) | 4 lines
Remove rexec and Bastion from the stdlib. This also eliminates the need for
f_restricted on frames. This in turn negates the need for
PyEval_GetRestricted() and PyFrame_IsRestricted().
........
r55303 | brett.cannon | 2007-05-13 19:22:22 -0700 (Sun, 13 May 2007) | 2 lines
Remove the md5 and sha modules.
........
r55305 | george.yoshida | 2007-05-13 19:45:55 -0700 (Sun, 13 May 2007) | 2 lines
fix markup
........
r55306 | neal.norwitz | 2007-05-13 19:47:57 -0700 (Sun, 13 May 2007) | 1 line
Get the doc building again after some removals.
........
r55307 | neal.norwitz | 2007-05-13 19:50:45 -0700 (Sun, 13 May 2007) | 1 line
Get test_pyclbr passing again after getstatus was removed from commands. This "test case" was weird since it was just importing a seemingly random module. Remove the import
........
r55322 | brett.cannon | 2007-05-14 14:09:20 -0700 (Mon, 14 May 2007) | 3 lines
Remove the compiler package. Will eventually need a mechanism to byte compile
an AST.
........
2007-05-14 19:03:55 -03:00
|
|
|
|
2011-12-23 07:40:16 -04:00
|
|
|
self = PyCFunction_GET_SELF(m);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (self == NULL)
|
|
|
|
self = Py_None;
|
|
|
|
Py_INCREF(self);
|
|
|
|
return self;
|
1995-01-07 08:34:58 -04:00
|
|
|
}
|
|
|
|
|
2001-09-20 18:45:26 -03:00
|
|
|
static PyGetSetDef meth_getsets [] = {
|
2010-05-09 12:52:27 -03:00
|
|
|
{"__doc__", (getter)meth_get__doc__, NULL, NULL},
|
|
|
|
{"__name__", (getter)meth_get__name__, NULL, NULL},
|
2011-12-23 07:40:16 -04:00
|
|
|
{"__qualname__", (getter)meth_get__qualname__, NULL, NULL},
|
2010-05-09 12:52:27 -03:00
|
|
|
{"__self__", (getter)meth_get__self__, NULL, NULL},
|
2013-11-23 19:37:55 -04:00
|
|
|
{"__text_signature__", (getter)meth_get__text_signature__, NULL, NULL},
|
2010-05-09 12:52:27 -03:00
|
|
|
{0}
|
2001-08-02 01:15:00 -03:00
|
|
|
};
|
|
|
|
|
2003-01-31 14:33:18 -04:00
|
|
|
#define OFF(x) offsetof(PyCFunctionObject, x)
|
|
|
|
|
|
|
|
static PyMemberDef meth_members[] = {
|
2010-05-09 12:52:27 -03:00
|
|
|
{"__module__", T_OBJECT, OFF(m_module), PY_WRITE_RESTRICTED},
|
|
|
|
{NULL}
|
2003-01-31 14:33:18 -04:00
|
|
|
};
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2000-07-09 03:03:25 -03:00
|
|
|
meth_repr(PyCFunctionObject *m)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (m->m_self == NULL || PyModule_Check(m->m_self))
|
|
|
|
return PyUnicode_FromFormat("<built-in function %s>",
|
|
|
|
m->m_ml->ml_name);
|
|
|
|
return PyUnicode_FromFormat("<built-in method %s of %s object at %p>",
|
|
|
|
m->m_ml->ml_name,
|
|
|
|
m->m_self->ob_type->tp_name,
|
|
|
|
m->m_self);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
2006-08-23 21:41:19 -03:00
|
|
|
static PyObject *
|
|
|
|
meth_richcompare(PyObject *self, PyObject *other, int op)
|
1993-03-29 06:43:31 -04:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyCFunctionObject *a, *b;
|
|
|
|
PyObject *res;
|
|
|
|
int eq;
|
|
|
|
|
|
|
|
if ((op != Py_EQ && op != Py_NE) ||
|
|
|
|
!PyCFunction_Check(self) ||
|
|
|
|
!PyCFunction_Check(other))
|
|
|
|
{
|
2011-08-10 22:28:54 -03:00
|
|
|
Py_RETURN_NOTIMPLEMENTED;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
a = (PyCFunctionObject *)self;
|
|
|
|
b = (PyCFunctionObject *)other;
|
|
|
|
eq = a->m_self == b->m_self;
|
|
|
|
if (eq)
|
|
|
|
eq = a->m_ml->ml_meth == b->m_ml->ml_meth;
|
|
|
|
if (op == Py_EQ)
|
|
|
|
res = eq ? Py_True : Py_False;
|
|
|
|
else
|
|
|
|
res = eq ? Py_False : Py_True;
|
|
|
|
Py_INCREF(res);
|
|
|
|
return res;
|
1993-03-29 06:43:31 -04:00
|
|
|
}
|
|
|
|
|
2010-10-17 17:54:53 -03:00
|
|
|
static Py_hash_t
|
2000-07-09 03:03:25 -03:00
|
|
|
meth_hash(PyCFunctionObject *a)
|
1993-03-29 06:43:31 -04:00
|
|
|
{
|
2010-10-17 17:54:53 -03:00
|
|
|
Py_hash_t x, y;
|
2018-07-31 03:18:24 -03:00
|
|
|
x = _Py_HashPointer(a->m_self);
|
2010-05-09 12:52:27 -03:00
|
|
|
y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
|
|
|
|
x ^= y;
|
|
|
|
if (x == -1)
|
|
|
|
x = -2;
|
|
|
|
return x;
|
1993-03-29 06:43:31 -04:00
|
|
|
}
|
|
|
|
|
2001-08-02 01:15:00 -03:00
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyTypeObject PyCFunction_Type = {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
|
|
|
"builtin_function_or_method",
|
|
|
|
sizeof(PyCFunctionObject),
|
|
|
|
0,
|
|
|
|
(destructor)meth_dealloc, /* tp_dealloc */
|
2019-05-29 15:31:52 -03:00
|
|
|
offsetof(PyCFunctionObject, vectorcall), /* tp_vectorcall_offset */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
2019-05-30 23:13:39 -03:00
|
|
|
0, /* tp_as_async */
|
2010-05-09 12:52:27 -03:00
|
|
|
(reprfunc)meth_repr, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
(hashfunc)meth_hash, /* tp_hash */
|
2019-09-11 08:01:01 -03:00
|
|
|
cfunction_call, /* tp_call */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_str */
|
|
|
|
PyObject_GenericGetAttr, /* tp_getattro */
|
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
2019-05-29 15:31:52 -03:00
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
|
|
|
|
_Py_TPFLAGS_HAVE_VECTORCALL, /* tp_flags */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_doc */
|
|
|
|
(traverseproc)meth_traverse, /* tp_traverse */
|
|
|
|
0, /* tp_clear */
|
|
|
|
meth_richcompare, /* tp_richcompare */
|
2014-08-06 20:31:40 -03:00
|
|
|
offsetof(PyCFunctionObject, m_weakreflist), /* tp_weaklistoffset */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_iter */
|
|
|
|
0, /* tp_iternext */
|
2013-11-24 06:41:05 -04:00
|
|
|
meth_methods, /* tp_methods */
|
2010-05-09 12:52:27 -03:00
|
|
|
meth_members, /* tp_members */
|
|
|
|
meth_getsets, /* tp_getset */
|
|
|
|
0, /* tp_base */
|
|
|
|
0, /* tp_dict */
|
1990-10-14 09:07:46 -03:00
|
|
|
};
|
1990-12-20 11:06:42 -04:00
|
|
|
|
2019-07-05 09:48:24 -03:00
|
|
|
/* Vectorcall functions for each of the PyCFunction calling conventions,
|
|
|
|
* except for METH_VARARGS (possibly combined with METH_KEYWORDS) which
|
|
|
|
* doesn't use vectorcall.
|
|
|
|
*
|
|
|
|
* First, common helpers
|
|
|
|
*/
|
|
|
|
|
|
|
|
static inline int
|
2019-11-04 19:51:22 -04:00
|
|
|
cfunction_check_kwargs(PyThreadState *tstate, PyObject *func, PyObject *kwnames)
|
2019-07-05 09:48:24 -03:00
|
|
|
{
|
2019-11-04 19:51:22 -04:00
|
|
|
assert(!_PyErr_Occurred(tstate));
|
2019-07-05 09:48:24 -03:00
|
|
|
assert(PyCFunction_Check(func));
|
|
|
|
if (kwnames && PyTuple_GET_SIZE(kwnames)) {
|
2019-11-05 11:48:04 -04:00
|
|
|
PyObject *funcstr = _PyObject_FunctionStr(func);
|
|
|
|
if (funcstr != NULL) {
|
|
|
|
_PyErr_Format(tstate, PyExc_TypeError,
|
|
|
|
"%U takes no keyword arguments", funcstr);
|
|
|
|
Py_DECREF(funcstr);
|
|
|
|
}
|
2019-07-05 09:48:24 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-11-05 11:48:04 -04:00
|
|
|
typedef void (*funcptr)(void);
|
|
|
|
|
2019-07-05 09:48:24 -03:00
|
|
|
static inline funcptr
|
2019-11-04 19:51:22 -04:00
|
|
|
cfunction_enter_call(PyThreadState *tstate, PyObject *func)
|
2019-07-05 09:48:24 -03:00
|
|
|
{
|
2019-11-04 19:51:22 -04:00
|
|
|
if (_Py_EnterRecursiveCall(tstate, " while calling a Python object")) {
|
2019-07-05 09:48:24 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return (funcptr)PyCFunction_GET_FUNCTION(func);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now the actual vectorcall functions */
|
|
|
|
static PyObject *
|
|
|
|
cfunction_vectorcall_FASTCALL(
|
|
|
|
PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
|
|
|
|
{
|
2019-11-04 19:51:22 -04:00
|
|
|
PyThreadState *tstate = _PyThreadState_GET();
|
|
|
|
if (cfunction_check_kwargs(tstate, func, kwnames)) {
|
2019-07-05 09:48:24 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
|
|
|
|
_PyCFunctionFast meth = (_PyCFunctionFast)
|
2019-11-04 19:51:22 -04:00
|
|
|
cfunction_enter_call(tstate, func);
|
2019-07-05 09:48:24 -03:00
|
|
|
if (meth == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
PyObject *result = meth(PyCFunction_GET_SELF(func), args, nargs);
|
2019-11-04 19:51:22 -04:00
|
|
|
_Py_LeaveRecursiveCall(tstate);
|
2019-07-05 09:48:24 -03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
cfunction_vectorcall_FASTCALL_KEYWORDS(
|
|
|
|
PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
|
|
|
|
{
|
2019-11-04 19:51:22 -04:00
|
|
|
PyThreadState *tstate = _PyThreadState_GET();
|
2019-07-05 09:48:24 -03:00
|
|
|
Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
|
|
|
|
_PyCFunctionFastWithKeywords meth = (_PyCFunctionFastWithKeywords)
|
2019-11-04 19:51:22 -04:00
|
|
|
cfunction_enter_call(tstate, func);
|
2019-07-05 09:48:24 -03:00
|
|
|
if (meth == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
PyObject *result = meth(PyCFunction_GET_SELF(func), args, nargs, kwnames);
|
2019-11-04 19:51:22 -04:00
|
|
|
_Py_LeaveRecursiveCall(tstate);
|
2019-07-05 09:48:24 -03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
cfunction_vectorcall_NOARGS(
|
|
|
|
PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
|
|
|
|
{
|
2019-11-04 19:51:22 -04:00
|
|
|
PyThreadState *tstate = _PyThreadState_GET();
|
|
|
|
if (cfunction_check_kwargs(tstate, func, kwnames)) {
|
2019-07-05 09:48:24 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
|
|
|
|
if (nargs != 0) {
|
2019-11-05 11:48:04 -04:00
|
|
|
PyObject *funcstr = _PyObject_FunctionStr(func);
|
|
|
|
if (funcstr != NULL) {
|
|
|
|
_PyErr_Format(tstate, PyExc_TypeError,
|
|
|
|
"%U takes no arguments (%zd given)", funcstr, nargs);
|
|
|
|
Py_DECREF(funcstr);
|
|
|
|
}
|
2019-07-05 09:48:24 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2019-11-04 19:51:22 -04:00
|
|
|
PyCFunction meth = (PyCFunction)cfunction_enter_call(tstate, func);
|
2019-07-05 09:48:24 -03:00
|
|
|
if (meth == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
PyObject *result = meth(PyCFunction_GET_SELF(func), NULL);
|
2019-11-04 19:51:22 -04:00
|
|
|
_Py_LeaveRecursiveCall(tstate);
|
2019-07-05 09:48:24 -03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
cfunction_vectorcall_O(
|
|
|
|
PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
|
|
|
|
{
|
2019-11-04 19:51:22 -04:00
|
|
|
PyThreadState *tstate = _PyThreadState_GET();
|
|
|
|
if (cfunction_check_kwargs(tstate, func, kwnames)) {
|
2019-07-05 09:48:24 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
|
|
|
|
if (nargs != 1) {
|
2019-11-05 11:48:04 -04:00
|
|
|
PyObject *funcstr = _PyObject_FunctionStr(func);
|
|
|
|
if (funcstr != NULL) {
|
|
|
|
_PyErr_Format(tstate, PyExc_TypeError,
|
|
|
|
"%U takes exactly one argument (%zd given)", funcstr, nargs);
|
|
|
|
Py_DECREF(funcstr);
|
|
|
|
}
|
2019-07-05 09:48:24 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2019-11-04 19:51:22 -04:00
|
|
|
PyCFunction meth = (PyCFunction)cfunction_enter_call(tstate, func);
|
2019-07-05 09:48:24 -03:00
|
|
|
if (meth == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
PyObject *result = meth(PyCFunction_GET_SELF(func), args[0]);
|
2019-11-04 19:51:22 -04:00
|
|
|
_Py_LeaveRecursiveCall(tstate);
|
2019-07-05 09:48:24 -03:00
|
|
|
return result;
|
|
|
|
}
|
2019-09-11 08:01:01 -03:00
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
cfunction_call(PyObject *func, PyObject *args, PyObject *kwargs)
|
|
|
|
{
|
|
|
|
assert(kwargs == NULL || PyDict_Check(kwargs));
|
|
|
|
|
2019-11-04 20:22:12 -04:00
|
|
|
PyThreadState *tstate = _PyThreadState_GET();
|
|
|
|
assert(!_PyErr_Occurred(tstate));
|
|
|
|
|
2019-09-11 08:01:01 -03:00
|
|
|
int flags = PyCFunction_GET_FLAGS(func);
|
|
|
|
if (!(flags & METH_VARARGS)) {
|
|
|
|
/* If this is not a METH_VARARGS function, delegate to vectorcall */
|
|
|
|
return PyVectorcall_Call(func, args, kwargs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* For METH_VARARGS, we cannot use vectorcall as the vectorcall pointer
|
|
|
|
* is NULL. This is intentional, since vectorcall would be slower. */
|
|
|
|
PyCFunction meth = PyCFunction_GET_FUNCTION(func);
|
|
|
|
PyObject *self = PyCFunction_GET_SELF(func);
|
|
|
|
|
|
|
|
PyObject *result;
|
|
|
|
if (flags & METH_KEYWORDS) {
|
|
|
|
result = (*(PyCFunctionWithKeywords)(void(*)(void))meth)(self, args, kwargs);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
|
2019-11-04 20:22:12 -04:00
|
|
|
_PyErr_Format(tstate, PyExc_TypeError,
|
|
|
|
"%.200s() takes no keyword arguments",
|
|
|
|
((PyCFunctionObject*)func)->m_ml->ml_name);
|
2019-09-11 08:01:01 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
result = meth(self, args);
|
|
|
|
}
|
2019-11-04 20:22:12 -04:00
|
|
|
return _Py_CheckFunctionResult(tstate, func, result, NULL);
|
2019-09-11 08:01:01 -03:00
|
|
|
}
|