Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Commit d06c912

Browse files
author
Anselm Kruis
committed
Merge branch master into master-slp.
Issue python#29263: LOAD_METHOD support for C methods The outcome of this merge is not fully functional.
2 parents 0cfe7c5 + 5566bbb commit d06c912

File tree

8 files changed

+95
-41
lines changed

8 files changed

+95
-41
lines changed

Include/descrobject.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ PyAPI_FUNC(PyObject *) PyDescr_NewMember(PyTypeObject *,
9696
PyAPI_FUNC(PyObject *) PyDescr_NewGetSet(PyTypeObject *,
9797
struct PyGetSetDef *);
9898
#ifndef Py_LIMITED_API
99+
100+
PyAPI_FUNC(PyObject *) _PyMethodDescr_FastCallKeywords(
101+
PyObject *descrobj, PyObject **stack, Py_ssize_t nargs, PyObject *kwnames);
99102
PyAPI_FUNC(PyObject *) PyDescr_NewWrapper(PyTypeObject *,
100103
struct wrapperbase *, void *);
101104
#define PyDescr_IsData(d) (Py_TYPE(d)->tp_descr_set != NULL)

Include/methodobject.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ PyAPI_FUNC(PyObject *) _PyMethodDef_RawFastCallDict(
110110
PyObject **args,
111111
Py_ssize_t nargs,
112112
PyObject *kwargs);
113+
114+
PyAPI_FUNC(PyObject *) _PyMethodDef_RawFastCallKeywords(
115+
PyMethodDef *method,
116+
PyObject *self,
117+
PyObject **args,
118+
Py_ssize_t nargs,
119+
PyObject *kwnames);
113120
#endif
114121

115122
PyAPI_FUNC(int) PyCFunction_ClearFreeList(void);

Lib/test/test_gdb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,7 @@ def test_pycfunction(self):
846846
breakpoint='time_gmtime',
847847
cmds_after_breakpoint=['py-bt-full'],
848848
)
849-
self.assertIn('#1 <built-in method gmtime', gdb_output)
849+
self.assertIn('#2 <built-in method gmtime', gdb_output)
850850

851851
@unittest.skipIf(python_is_optimized(),
852852
"Python was compiled with optimizations")

Objects/descrobject.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,44 @@ methoddescr_call(PyMethodDescrObject *descr, PyObject *args, PyObject *kwargs)
250250
return result;
251251
}
252252

253+
// same to methoddescr_call(), but use FASTCALL convention.
254+
PyObject *
255+
_PyMethodDescr_FastCallKeywords(PyObject *descrobj,
256+
PyObject **args, Py_ssize_t nargs,
257+
PyObject *kwnames)
258+
{
259+
assert(Py_TYPE(descrobj) == &PyMethodDescr_Type);
260+
PyMethodDescrObject *descr = (PyMethodDescrObject *)descrobj;
261+
PyObject *self, *result;
262+
263+
/* Make sure that the first argument is acceptable as 'self' */
264+
if (nargs < 1) {
265+
PyErr_Format(PyExc_TypeError,
266+
"descriptor '%V' of '%.100s' "
267+
"object needs an argument",
268+
descr_name((PyDescrObject *)descr), "?",
269+
PyDescr_TYPE(descr)->tp_name);
270+
return NULL;
271+
}
272+
self = args[0];
273+
if (!_PyObject_RealIsSubclass((PyObject *)Py_TYPE(self),
274+
(PyObject *)PyDescr_TYPE(descr))) {
275+
PyErr_Format(PyExc_TypeError,
276+
"descriptor '%V' "
277+
"requires a '%.100s' object "
278+
"but received a '%.100s'",
279+
descr_name((PyDescrObject *)descr), "?",
280+
PyDescr_TYPE(descr)->tp_name,
281+
self->ob_type->tp_name);
282+
return NULL;
283+
}
284+
285+
result = _PyMethodDef_RawFastCallKeywords(descr->d_method, self,
286+
args+1, nargs-1, kwnames);
287+
result = _Py_CheckFunctionResult((PyObject *)descr, result, NULL);
288+
return result;
289+
}
290+
253291
static PyObject *
254292
classmethoddescr_call(PyMethodDescrObject *descr, PyObject *args,
255293
PyObject *kwds)

Objects/methodobject.c

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -222,71 +222,63 @@ _PyCFunction_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs,
222222
}
223223

224224
PyObject *
225-
_PyCFunction_FastCallKeywords(PyObject *func_obj, PyObject **args,
226-
Py_ssize_t nargs, PyObject *kwnames)
225+
_PyMethodDef_RawFastCallKeywords(PyMethodDef *method, PyObject *self, PyObject **args,
226+
Py_ssize_t nargs, PyObject *kwnames)
227227
{
228228
STACKLESS_GETARG();
229-
PyCFunctionObject *func;
230-
PyCFunction meth;
231-
PyObject *self, *result;
232-
Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
233-
int flags;
229+
/* _PyMethodDef_RawFastCallKeywords() must not be called with an exception set,
230+
because it can clear it (directly or indirectly) and so the
231+
caller loses its exception */
232+
assert(!PyErr_Occurred());
234233

235-
assert(func_obj != NULL);
236-
assert(PyCFunction_Check(func_obj));
234+
assert(method != NULL);
237235
assert(nargs >= 0);
238236
assert(kwnames == NULL || PyTuple_CheckExact(kwnames));
239-
assert((nargs == 0 && nkwargs == 0) || args != NULL);
240237
/* kwnames must only contains str strings, no subclass, and all keys must
241238
be unique */
242239

243-
/* _PyCFunction_FastCallKeywords() must not be called with an exception
244-
set, because it can clear it (directly or indirectly) and so the caller
245-
loses its exception */
246-
assert(!PyErr_Occurred());
247-
248-
func = (PyCFunctionObject*)func_obj;
249-
meth = PyCFunction_GET_FUNCTION(func);
250-
self = PyCFunction_GET_SELF(func);
251-
flags = PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST | METH_STACKLESS);
240+
PyCFunction meth = method->ml_meth;
241+
int flags = method->ml_flags & ~(METH_CLASS | METH_STATIC | METH_COEXIST | METH_STACKLESS);
242+
Py_ssize_t nkwargs = kwnames == NULL ? 0 : PyTuple_Size(kwnames);
243+
PyObject *result;
252244

253245
switch (flags)
254246
{
255247
case METH_NOARGS:
256248
if (nargs != 0) {
257249
PyErr_Format(PyExc_TypeError,
258250
"%.200s() takes no arguments (%zd given)",
259-
func->m_ml->ml_name, nargs);
251+
method->ml_name, nargs);
260252
return NULL;
261253
}
262254

263255
if (nkwargs) {
264256
goto no_keyword_error;
265257
}
266258

267-
STACKLESS_PROMOTE_FLAG(PyCFunction_GET_FLAGS(func) & METH_STACKLESS);
259+
STACKLESS_PROMOTE_FLAG(method->ml_flags & METH_STACKLESS);
268260
result = (*meth) (self, NULL);
269261
break;
270262

271263
case METH_O:
272264
if (nargs != 1) {
273265
PyErr_Format(PyExc_TypeError,
274266
"%.200s() takes exactly one argument (%zd given)",
275-
func->m_ml->ml_name, nargs);
267+
method->ml_name, nargs);
276268
return NULL;
277269
}
278270

279271
if (nkwargs) {
280272
goto no_keyword_error;
281273
}
282274

283-
STACKLESS_PROMOTE_FLAG(PyCFunction_GET_FLAGS(func) & METH_STACKLESS);
275+
STACKLESS_PROMOTE_FLAG(method->ml_flags & METH_STACKLESS);
284276
result = (*meth) (self, args[0]);
285277
break;
286278

287279
case METH_FASTCALL:
288280
/* Fast-path: avoid temporary dict to pass keyword arguments */
289-
STACKLESS_PROMOTE_FLAG(PyCFunction_GET_FLAGS(func) & METH_STACKLESS);
281+
STACKLESS_PROMOTE_FLAG(method->ml_flags & METH_STACKLESS);
290282
result = ((_PyCFunctionFast)meth) (self, args, nargs, kwnames);
291283
break;
292284

@@ -320,12 +312,12 @@ _PyCFunction_FastCallKeywords(PyObject *func_obj, PyObject **args,
320312
kwdict = NULL;
321313
}
322314

323-
STACKLESS_PROMOTE_FLAG(PyCFunction_GET_FLAGS(func) & METH_STACKLESS);
315+
STACKLESS_PROMOTE_FLAG(method->ml_flags & METH_STACKLESS);
324316
result = (*(PyCFunctionWithKeywords)meth) (self, argtuple, kwdict);
325317
Py_XDECREF(kwdict);
326318
}
327319
else {
328-
STACKLESS_PROMOTE_FLAG(PyCFunction_GET_FLAGS(func) & METH_STACKLESS);
320+
STACKLESS_PROMOTE_FLAG(method->ml_flags & METH_STACKLESS);
329321
result = (*meth) (self, argtuple);
330322
}
331323
Py_DECREF(argtuple);
@@ -340,16 +332,31 @@ _PyCFunction_FastCallKeywords(PyObject *func_obj, PyObject **args,
340332
}
341333
STACKLESS_ASSERT();
342334

343-
result = _Py_CheckFunctionResult(func_obj, result, NULL);
344335
return result;
345336

346337
no_keyword_error:
347338
PyErr_Format(PyExc_TypeError,
348339
"%.200s() takes no keyword arguments",
349-
func->m_ml->ml_name);
340+
method->ml_name);
350341
return NULL;
351342
}
352343

344+
PyObject *
345+
_PyCFunction_FastCallKeywords(PyObject *func, PyObject **args,
346+
Py_ssize_t nargs, PyObject *kwnames)
347+
{
348+
PyObject *result;
349+
350+
assert(func != NULL);
351+
assert(PyCFunction_Check(func));
352+
353+
result = _PyMethodDef_RawFastCallKeywords(((PyCFunctionObject*)func)->m_ml,
354+
PyCFunction_GET_SELF(func),
355+
args, nargs, kwnames);
356+
result = _Py_CheckFunctionResult(func, result, NULL);
357+
return result;
358+
}
359+
353360
/* Methods (the standard built-in methods, that is) */
354361

355362
static void

Objects/object.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,8 +1063,8 @@ _PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method)
10631063
descr = _PyType_Lookup(tp, name);
10641064
if (descr != NULL) {
10651065
Py_INCREF(descr);
1066-
if (PyFunction_Check(descr)) {
1067-
/* A python method. */
1066+
if (PyFunction_Check(descr) ||
1067+
(Py_TYPE(descr) == &PyMethodDescr_Type)) {
10681068
meth_found = 1;
10691069
} else {
10701070
f = descr->ob_type->tp_descr_get;

Python/ceval.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5351,19 +5351,23 @@ call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
53515351
PyObject *x, *w;
53525352
Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
53535353
Py_ssize_t nargs = oparg - nkwargs;
5354-
PyObject **stack;
5354+
PyObject **stack = (*pp_stack) - nargs - nkwargs;
53555355

53565356
/* Always dispatch PyCFunction first, because these are
53575357
presumed to be the most frequent callable object.
53585358
*/
53595359
if (PyCFunction_Check(func)) {
53605360
PyThreadState *tstate = PyThreadState_GET();
5361-
5362-
stack = (*pp_stack) - nargs - nkwargs;
53635361
STACKLESS_PROPOSE_ALL();
53645362
C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
53655363
STACKLESS_ASSERT();
53665364
}
5365+
else if (Py_TYPE(func) == &PyMethodDescr_Type) {
5366+
PyThreadState *tstate = PyThreadState_GET();
5367+
STACKLESS_PROPOSE_ALL();
5368+
C_TRACE(x, _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames));
5369+
STACKLESS_ASSERT();
5370+
}
53675371
else {
53685372
if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
53695373
/* Optimize access to bound methods. Reuse the Python stack
@@ -5377,13 +5381,12 @@ call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
53775381
Py_INCREF(func);
53785382
Py_SETREF(*pfunc, self);
53795383
nargs++;
5384+
stack--;
53805385
}
53815386
else {
53825387
Py_INCREF(func);
53835388
}
53845389

5385-
stack = (*pp_stack) - nargs - nkwargs;
5386-
53875390
STACKLESS_PROPOSE_ALL();
53885391
if (PyFunction_Check(func)) {
53895392
x = fast_function(func, stack, nargs, kwnames);
@@ -5392,7 +5395,6 @@ call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
53925395
x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
53935396
}
53945397
STACKLESS_ASSERT();
5395-
53965398
Py_DECREF(func);
53975399
}
53985400

Tools/gdb/libpython.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,10 +1554,7 @@ def is_other_python_frame(self):
15541554

15551555
if caller in ('_PyCFunction_FastCallDict',
15561556
'_PyCFunction_FastCallKeywords'):
1557-
if caller == '_PyCFunction_FastCallKeywords':
1558-
arg_name = 'func_obj'
1559-
else:
1560-
arg_name = 'func'
1557+
arg_name = 'func'
15611558
# Within that frame:
15621559
# "func" is the local containing the PyObject* of the
15631560
# PyCFunctionObject instance

0 commit comments

Comments
 (0)