Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions Include/descrobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ typedef struct PyGetSetDef {
} PyGetSetDef;

#ifndef Py_LIMITED_API
typedef PyObject *(*wrapperfunc)(PyObject *self, PyObject *args,
void *wrapped);

/* For PyWrapperFlag_FASTCALL */
typedef PyObject *(*wrapperfunc)(PyObject *self, PyObject *const *args,
Py_ssize_t nargs, void *wrapped);

/* For PyWrapperFlag_KEYWORDS */
typedef PyObject *(*wrapperfunc_kwds)(PyObject *self, PyObject *args,
void *wrapped, PyObject *kwds);

Expand All @@ -33,8 +36,9 @@ struct wrapperbase {
PyObject *name_strobj;
};

/* Flags for above struct */
#define PyWrapperFlag_KEYWORDS 1 /* wrapper function takes keyword args */
/* The "flags" field in wrapperbase must be exactly one of these two values */
#define PyWrapperFlag_FASTCALL 0x0080 /* fastcall without keywords */
#define PyWrapperFlag_KEYWORDS 0x0003 /* tp_call convention (args tuple and kwargs dict) */

/* Various kinds of descriptor objects */

Expand Down Expand Up @@ -69,7 +73,8 @@ typedef struct {
typedef struct {
PyDescr_COMMON;
struct wrapperbase *d_base;
void *d_wrapped; /* This can be any function pointer */
vectorcallfunc vectorcall;
void *d_wrapped; /* Function pointer for the slot function which is being wrapped */
} PyWrapperDescrObject;
#endif /* Py_LIMITED_API */

Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -1140,9 +1140,9 @@ def inner():
import collections
check(collections.defaultdict.default_factory, size('3PP'))
# wrapper_descriptor (descriptor object)
check(int.__add__, size('3P2P'))
check(int.__add__, size('6P'))
# method-wrapper (descriptor object)
check({}.__iter__, size('2P'))
check({}.__iter__, size('3P'))
# empty dict
check({}, size('nQ2P'))
# dict
Expand Down
175 changes: 145 additions & 30 deletions Objects/descrobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -451,44 +451,101 @@ classmethoddescr_call(PyMethodDescrObject *descr, PyObject *args,
return res;
}

Py_LOCAL_INLINE(PyObject *)
wrapperdescr_raw_call(PyWrapperDescrObject *descr, PyObject *self,
PyObject *args, PyObject *kwds)
static inline PyObject *
wrapperdescr_raw_call_KEYWORDS(PyWrapperDescrObject *descr, PyObject *self,
PyObject *args, PyObject *kwds)
{
wrapperfunc wrapper = descr->d_base->wrapper;
assert(descr->d_base->flags == PyWrapperFlag_KEYWORDS);
if (Py_EnterRecursiveCall(" while calling a Python object")) {
return NULL;
}
wrapperfunc_kwds f = (wrapperfunc_kwds)(void(*)(void))descr->d_base->wrapper;
PyObject *res = f(self, args, descr->d_wrapped, kwds);
if (res == NULL) {
assert(PyErr_Occurred());
}
else {
assert(!PyErr_Occurred());
}
Py_LeaveRecursiveCall();
return res;
}

if (descr->d_base->flags & PyWrapperFlag_KEYWORDS) {
wrapperfunc_kwds wk = (wrapperfunc_kwds)(void(*)(void))wrapper;
return (*wk)(self, args, descr->d_wrapped, kwds);
static inline PyObject *
wrapperdescr_raw_call_FASTCALL(PyWrapperDescrObject *descr, PyObject *self,
PyObject *const *args, Py_ssize_t nargs)
{
assert(descr->d_base->flags == PyWrapperFlag_FASTCALL);
if (Py_EnterRecursiveCall(" while calling a Python object")) {
return NULL;
}
wrapperfunc f = descr->d_base->wrapper;
PyObject *res = f(self, args, nargs, descr->d_wrapped);
if (res == NULL) {
assert(PyErr_Occurred());
}
else {
assert(!PyErr_Occurred());
}
Py_LeaveRecursiveCall();
return res;
}

static PyObject *
wrapperdescr_vectorcall_FASTCALL(PyWrapperDescrObject *descr,
PyObject *const *args, size_t nargsf,
PyObject *kwnames)
{
Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);

/* Make sure that the first argument is acceptable as 'self' */
if (nargs < 1) {
PyErr_Format(PyExc_TypeError,
"descriptor '%V' of '%.100s' "
"object needs an argument",
descr_name((PyDescrObject *)descr), "?",
PyDescr_TYPE(descr)->tp_name);
return NULL;
}
PyObject *self = args[0];
if (!_PyObject_RealIsSubclass((PyObject *)Py_TYPE(self),
(PyObject *)PyDescr_TYPE(descr))) {
PyErr_Format(PyExc_TypeError,
"descriptor '%V' "
"requires a '%.100s' object "
"but received a '%.100s'",
descr_name((PyDescrObject *)descr), "?",
PyDescr_TYPE(descr)->tp_name,
self->ob_type->tp_name);
return NULL;
}

if (kwds != NULL && (!PyDict_Check(kwds) || PyDict_GET_SIZE(kwds) != 0)) {
if (kwnames != NULL && PyTuple_GET_SIZE(kwnames)) {
PyErr_Format(PyExc_TypeError,
"wrapper %s() takes no keyword arguments",
descr->d_base->name);
return NULL;
}
return (*wrapper)(self, args, descr->d_wrapped);
return wrapperdescr_raw_call_FASTCALL(descr, self, args+1, nargs-1);
}

static PyObject *
wrapperdescr_call(PyWrapperDescrObject *descr, PyObject *args, PyObject *kwds)
wrapperdescr_vectorcall_KEYWORDS(PyWrapperDescrObject *descr,
PyObject *const *args, size_t nargsf,
PyObject *kwnames)
{
Py_ssize_t argc;
PyObject *self, *result;
Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);

/* Make sure that the first argument is acceptable as 'self' */
assert(PyTuple_Check(args));
argc = PyTuple_GET_SIZE(args);
if (argc < 1) {
if (nargs < 1) {
PyErr_Format(PyExc_TypeError,
"descriptor '%V' of '%.100s' "
"object needs an argument",
descr_name((PyDescrObject *)descr), "?",
PyDescr_TYPE(descr)->tp_name);
return NULL;
}
self = PyTuple_GET_ITEM(args, 0);
PyObject *self = args[0];
if (!_PyObject_RealIsSubclass((PyObject *)Py_TYPE(self),
(PyObject *)PyDescr_TYPE(descr))) {
PyErr_Format(PyExc_TypeError,
Expand All @@ -501,12 +558,22 @@ wrapperdescr_call(PyWrapperDescrObject *descr, PyObject *args, PyObject *kwds)
return NULL;
}

args = PyTuple_GetSlice(args, 1, argc);
if (args == NULL) {
PyObject *argstuple = _PyTuple_FromArray(args+1, nargs-1);
if (argstuple == NULL) {
return NULL;
}
result = wrapperdescr_raw_call(descr, self, args, kwds);
Py_DECREF(args);
PyObject *kwdict = NULL;
if (kwnames != NULL) {
kwdict = _PyStack_AsDict(args + nargs, kwnames);
if (kwdict == NULL) {
Py_DECREF(argstuple);
return NULL;
}
}

PyObject *result = wrapperdescr_raw_call_KEYWORDS(descr, self, argstuple, kwdict);
Py_DECREF(argstuple);
Py_XDECREF(kwdict);
return result;
}

Expand Down Expand Up @@ -801,7 +868,7 @@ PyTypeObject PyWrapperDescr_Type = {
sizeof(PyWrapperDescrObject),
0,
(destructor)descr_dealloc, /* tp_dealloc */
0, /* tp_vectorcall_offset */
offsetof(PyWrapperDescrObject, vectorcall), /* tp_vectorcall_offset */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_as_async */
Expand All @@ -810,12 +877,13 @@ PyTypeObject PyWrapperDescr_Type = {
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
(ternaryfunc)wrapperdescr_call, /* tp_call */
PyVectorcall_Call, /* tp_call */
0, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
_Py_TPFLAGS_HAVE_VECTORCALL |
Py_TPFLAGS_METHOD_DESCRIPTOR, /* tp_flags */
0, /* tp_doc */
descr_traverse, /* tp_traverse */
Expand Down Expand Up @@ -934,12 +1002,27 @@ PyDescr_NewGetSet(PyTypeObject *type, PyGetSetDef *getset)
PyObject *
PyDescr_NewWrapper(PyTypeObject *type, struct wrapperbase *base, void *wrapped)
{
PyWrapperDescrObject *descr;
/* Figure out correct vectorcall function to use */
vectorcallfunc vectorcall;
switch (base->flags)
{
case PyWrapperFlag_FASTCALL:
vectorcall = (vectorcallfunc)wrapperdescr_vectorcall_FASTCALL;
break;
case PyWrapperFlag_KEYWORDS:
vectorcall = (vectorcallfunc)wrapperdescr_vectorcall_KEYWORDS;
break;
default:
PyErr_SetString(PyExc_SystemError, "bad flags in PyDescr_NewWrapper(): exactly one of PyWrapperFlag_FASTCALL or PyWrapperFlag_KEYWORDS must be set");
return NULL;
}

PyWrapperDescrObject *descr;
descr = (PyWrapperDescrObject *)descr_new(&PyWrapperDescr_Type,
type, base->name);
if (descr != NULL) {
descr->d_base = base;
descr->vectorcall = vectorcall;
descr->d_wrapped = wrapped;
}
return (PyObject *)descr;
Expand Down Expand Up @@ -1159,15 +1242,13 @@ PyDictProxy_New(PyObject *mapping)
}


/* --- Wrapper object for "slot" methods --- */

/* This has no reason to be in this file except that adding new files is a
bit of a pain */
/* --- MethodWrapper: bound wrapper descriptors such as [].__len__ --- */

typedef struct {
PyObject_HEAD
PyWrapperDescrObject *descr;
PyObject *self;
vectorcallfunc vectorcall;
} wrapperobject;

#define Wrapper_Check(v) (Py_TYPE(v) == &_PyMethodWrapper_Type)
Expand Down Expand Up @@ -1292,10 +1373,27 @@ static PyGetSetDef wrapper_getsets[] = {
{0}
};

static PyObject *
wrapper_vectorcall_FASTCALL(wrapperobject *wp, PyObject *const *args,
size_t nargsf, PyObject *kwnames)
{
Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
if (kwnames != NULL && PyTuple_GET_SIZE(kwnames)) {
PyErr_Format(PyExc_TypeError,
"wrapper %s() takes no keyword arguments",
wp->descr->d_base->name);
return NULL;
}
return wrapperdescr_raw_call_FASTCALL(wp->descr, wp->self, args, nargs);
}

static PyObject *
wrapper_call(wrapperobject *wp, PyObject *args, PyObject *kwds)
{
return wrapperdescr_raw_call(wp->descr, wp->self, args, kwds);
if (wp->vectorcall) {
return PyVectorcall_Call((PyObject *)wp, args, kwds);
}
return wrapperdescr_raw_call_KEYWORDS(wp->descr, wp->self, args, kwds);
}

static int
Expand All @@ -1314,7 +1412,7 @@ PyTypeObject _PyMethodWrapper_Type = {
0, /* tp_itemsize */
/* methods */
(destructor)wrapper_dealloc, /* tp_dealloc */
0, /* tp_vectorcall_offset */
offsetof(wrapperobject, vectorcall), /* tp_vectorcall_offset */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_as_async */
Expand All @@ -1328,7 +1426,8 @@ PyTypeObject _PyMethodWrapper_Type = {
PyObject_GenericGetAttr, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
_Py_TPFLAGS_HAVE_VECTORCALL, /* tp_flags */
0, /* tp_doc */
wrapper_traverse, /* tp_traverse */
0, /* tp_clear */
Expand Down Expand Up @@ -1356,12 +1455,28 @@ PyWrapper_New(PyObject *d, PyObject *self)
assert(_PyObject_RealIsSubclass((PyObject *)Py_TYPE(self),
(PyObject *)PyDescr_TYPE(descr)));

/* Figure out correct vectorcall function to use */
vectorcallfunc vectorcall;
switch (descr->d_base->flags)
{
case PyWrapperFlag_FASTCALL:
vectorcall = (vectorcallfunc)wrapper_vectorcall_FASTCALL;
break;
case PyWrapperFlag_KEYWORDS:
vectorcall = NULL; /* Use tp_call instead */
break;
default:
PyErr_SetString(PyExc_SystemError, "bad flags in PyWrapper_New(): exactly one of PyWrapperFlag_FASTCALL or PyWrapperFlag_KEYWORDS must be set");
return NULL;
}

wp = PyObject_GC_New(wrapperobject, &_PyMethodWrapper_Type);
if (wp != NULL) {
Py_INCREF(descr);
wp->descr = descr;
Py_INCREF(self);
wp->self = self;
wp->vectorcall = vectorcall;
_PyObject_GC_TRACK(wp);
}
return (PyObject *)wp;
Expand Down
Loading