Skip to content

bpo-37151: simplify classmethoddescr_call #13340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 7, 2019
Merged
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
4 changes: 2 additions & 2 deletions Lib/test/test_descr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1613,8 +1613,8 @@ class SubSpam(spam.spamlist): pass
spam_cm(spam.spamlist())
self.assertEqual(
str(cm.exception),
"descriptor 'classmeth' requires a type "
"but received a 'xxsubtype.spamlist' instance")
"descriptor 'classmeth' for type 'xxsubtype.spamlist' "
"needs a type, not a 'xxsubtype.spamlist' as arg 2")

with self.assertRaises(TypeError) as cm:
spam_cm(list)
Expand Down
44 changes: 16 additions & 28 deletions Objects/descrobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,16 +300,19 @@ _PyMethodDescr_Vectorcall(PyObject *descrobj,
return result;
}

/* Instances of classmethod_descriptor are unlikely to be called directly.
For one, the analogous class "classmethod" (for Python classes) is not
callable. Second, users are not likely to access a classmethod_descriptor
directly, since it means pulling it from the class __dict__.

This is just an excuse to say that this doesn't need to be optimized:
we implement this simply by calling __get__ and then calling the result.
*/
static PyObject *
classmethoddescr_call(PyMethodDescrObject *descr, PyObject *args,
PyObject *kwds)
{
Py_ssize_t argc;
PyObject *self, *result;

/* Make sure that the first argument is acceptable as 'self' */
assert(PyTuple_Check(args));
argc = PyTuple_GET_SIZE(args);
Py_ssize_t argc = PyTuple_GET_SIZE(args);
if (argc < 1) {
PyErr_Format(PyExc_TypeError,
"descriptor '%V' of '%.100s' "
Expand All @@ -318,30 +321,15 @@ classmethoddescr_call(PyMethodDescrObject *descr, PyObject *args,
PyDescr_TYPE(descr)->tp_name);
return NULL;
}
self = PyTuple_GET_ITEM(args, 0);
if (!PyType_Check(self)) {
PyErr_Format(PyExc_TypeError,
"descriptor '%V' requires a type "
"but received a '%.100s' instance",
descr_name((PyDescrObject *)descr), "?",
self->ob_type->tp_name);
return NULL;
}
if (!PyType_IsSubtype((PyTypeObject *)self, PyDescr_TYPE(descr))) {
PyErr_Format(PyExc_TypeError,
"descriptor '%V' requires a subtype of '%.100s' "
"but received '%.100s'",
descr_name((PyDescrObject *)descr), "?",
PyDescr_TYPE(descr)->tp_name,
((PyTypeObject*)self)->tp_name);
PyObject *self = PyTuple_GET_ITEM(args, 0);
PyObject *bound = classmethod_get(descr, NULL, self);
if (bound == NULL) {
return NULL;
}

result = _PyMethodDef_RawFastCallDict(descr->d_method, self,
&_PyTuple_ITEMS(args)[1], argc - 1,
kwds);
result = _Py_CheckFunctionResult((PyObject *)descr, result, NULL);
return result;
PyObject *res = _PyObject_FastCallDict(bound, _PyTuple_ITEMS(args)+1,
argc-1, kwds);
Py_DECREF(bound);
return res;
}

Py_LOCAL_INLINE(PyObject *)
Expand Down