bpo-38644: Add _PyObject_Call() (GH-17089)

* Add pycore_call.h internal header file.
* Add _PyObject_Call(): PyObject_Call() with tstate
* Add _PyObject_CallNoArgTstate(): _PyObject_CallNoArg() with tstate
* Add _PyObject_FastCallDictTstate(): _PyObject_FastCallDict()
  with tstate
* _PyObject_Call_Prepend() now takes tstate
* Replace _PyObject_FastCall() calls
  with _PyObject_VectorcallTstate() calls
This commit is contained in:
Victor Stinner
2019-11-14 13:36:21 +01:00
committed by GitHub
parent b9e681261c
commit 4d231bcc77
10 changed files with 217 additions and 138 deletions

View File

@@ -137,7 +137,8 @@ PyAPI_FUNC(PyObject *) PyVectorcall_Call(PyObject *callable, PyObject *tuple, Py
static inline PyObject *
_PyObject_FastCall(PyObject *func, PyObject *const *args, Py_ssize_t nargs)
{
return _PyObject_Vectorcall(func, args, (size_t)nargs, NULL);
PyThreadState *tstate = PyThreadState_GET();
return _PyObject_VectorcallTstate(tstate, func, args, (size_t)nargs, NULL);
}
/* Call a callable without any arguments
@@ -145,7 +146,8 @@ _PyObject_FastCall(PyObject *func, PyObject *const *args, Py_ssize_t nargs)
PyObject_CallNoArgs(). */
static inline PyObject *
_PyObject_CallNoArg(PyObject *func) {
return _PyObject_Vectorcall(func, NULL, 0, NULL);
PyThreadState *tstate = PyThreadState_GET();
return _PyObject_VectorcallTstate(tstate, func, NULL, 0, NULL);
}
static inline PyObject *
@@ -155,16 +157,11 @@ _PyObject_CallOneArg(PyObject *func, PyObject *arg)
PyObject *_args[2];
PyObject **args = _args + 1; // For PY_VECTORCALL_ARGUMENTS_OFFSET
args[0] = arg;
return _PyObject_Vectorcall(func, args,
1 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
PyThreadState *tstate = PyThreadState_GET();
size_t nargsf = 1 | PY_VECTORCALL_ARGUMENTS_OFFSET;
return _PyObject_VectorcallTstate(tstate, func, args, nargsf, NULL);
}
PyAPI_FUNC(PyObject *) _PyObject_Call_Prepend(
PyObject *callable,
PyObject *obj,
PyObject *args,
PyObject *kwargs);
PyAPI_FUNC(PyObject *) _PyObject_VectorcallMethod(
PyObject *name, PyObject *const *args,
size_t nargsf, PyObject *kwnames);

View File

@@ -0,0 +1,39 @@
#ifndef Py_INTERNAL_CALL_H
#define Py_INTERNAL_CALL_H
#ifdef __cplusplus
extern "C" {
#endif
#ifndef Py_BUILD_CORE
# error "this header requires Py_BUILD_CORE define"
#endif
PyAPI_FUNC(PyObject *) _PyObject_Call_Prepend(
PyThreadState *tstate,
PyObject *callable,
PyObject *obj,
PyObject *args,
PyObject *kwargs);
PyAPI_FUNC(PyObject *) _PyObject_FastCallDictTstate(
PyThreadState *tstate,
PyObject *callable,
PyObject *const *args,
size_t nargsf,
PyObject *kwargs);
PyAPI_FUNC(PyObject *) _PyObject_Call(
PyThreadState *tstate,
PyObject *callable,
PyObject *args,
PyObject *kwargs);
static inline PyObject *
_PyObject_CallNoArgTstate(PyThreadState *tstate, PyObject *func) {
return _PyObject_VectorcallTstate(tstate, func, NULL, 0, NULL);
}
#ifdef __cplusplus
}
#endif
#endif /* !Py_INTERNAL_CALL_H */

View File

@@ -1076,6 +1076,7 @@ PYTHON_HEADERS= \
\
$(srcdir)/Include/internal/pycore_accu.h \
$(srcdir)/Include/internal/pycore_atomic.h \
$(srcdir)/Include/internal/pycore_call.h \
$(srcdir)/Include/internal/pycore_ceval.h \
$(srcdir)/Include/internal/pycore_code.h \
$(srcdir)/Include/internal/pycore_condvar.h \

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,7 @@
/* Type object implementation */
#include "Python.h"
#include "pycore_call.h"
#include "pycore_object.h"
#include "pycore_pyerrors.h"
#include "pycore_pystate.h"
@@ -6554,19 +6555,21 @@ slot_tp_hash(PyObject *self)
static PyObject *
slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
{
PyThreadState *tstate = _PyThreadState_GET();
_Py_IDENTIFIER(__call__);
int unbound;
PyObject *meth = lookup_method(self, &PyId___call__, &unbound);
PyObject *res;
if (meth == NULL)
if (meth == NULL) {
return NULL;
}
PyObject *res;
if (unbound) {
res = _PyObject_Call_Prepend(meth, self, args, kwds);
res = _PyObject_Call_Prepend(tstate, meth, self, args, kwds);
}
else {
res = PyObject_Call(meth, args, kwds);
res = _PyObject_Call(tstate, meth, args, kwds);
}
Py_DECREF(meth);
@@ -6688,17 +6691,16 @@ static PyObject *
slot_tp_richcompare(PyObject *self, PyObject *other, int op)
{
PyThreadState *tstate = _PyThreadState_GET();
int unbound;
PyObject *func, *res;
func = lookup_maybe_method(self, &name_op[op], &unbound);
int unbound;
PyObject *func = lookup_maybe_method(self, &name_op[op], &unbound);
if (func == NULL) {
PyErr_Clear();
Py_RETURN_NOTIMPLEMENTED;
}
PyObject *stack[2] = {self, other};
res = vectorcall_unbound(tstate, unbound, func, stack, 2);
PyObject *res = vectorcall_unbound(tstate, unbound, func, stack, 2);
Py_DECREF(func);
return res;
}
@@ -6793,18 +6795,21 @@ slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)
static int
slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
{
PyThreadState *tstate = _PyThreadState_GET();
_Py_IDENTIFIER(__init__);
int unbound;
PyObject *meth = lookup_method(self, &PyId___init__, &unbound);
PyObject *res;
if (meth == NULL)
if (meth == NULL) {
return -1;
}
PyObject *res;
if (unbound) {
res = _PyObject_Call_Prepend(meth, self, args, kwds);
res = _PyObject_Call_Prepend(tstate, meth, self, args, kwds);
}
else {
res = PyObject_Call(meth, args, kwds);
res = _PyObject_Call(tstate, meth, args, kwds);
}
Py_DECREF(meth);
if (res == NULL)
@@ -6823,6 +6828,7 @@ slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
static PyObject *
slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyThreadState *tstate = _PyThreadState_GET();
PyObject *func, *result;
func = _PyObject_GetAttrId((PyObject *)type, &PyId___new__);
@@ -6830,7 +6836,7 @@ slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return NULL;
}
result = _PyObject_Call_Prepend(func, (PyObject *)type, args, kwds);
result = _PyObject_Call_Prepend(tstate, func, (PyObject *)type, args, kwds);
Py_DECREF(func);
return result;
}

View File

@@ -160,6 +160,7 @@
<ClInclude Include="..\Include\import.h" />
<ClInclude Include="..\Include\internal\pycore_accu.h" />
<ClInclude Include="..\Include\internal\pycore_atomic.h" />
<ClInclude Include="..\Include\internal\pycore_call.h" />
<ClInclude Include="..\Include\internal\pycore_ceval.h" />
<ClInclude Include="..\Include\internal\pycore_code.h" />
<ClInclude Include="..\Include\internal\pycore_condvar.h" />

View File

@@ -183,6 +183,9 @@
<ClInclude Include="..\Include\internal\pycore_atomic.h">
<Filter>Include</Filter>
</ClInclude>
<ClInclude Include="..\Include\internal\pycore_call.h">
<Filter>Include</Filter>
</ClInclude>
<ClInclude Include="..\Include\internal\pycore_code.h">
<Filter>Include</Filter>
</ClInclude>

View File

@@ -1,4 +1,5 @@
#include "Python.h"
#include "pycore_pyerrors.h"
#include "pycore_pystate.h"
#include "frameobject.h"
#include "clinic/_warnings.c.h"
@@ -27,10 +28,11 @@ static struct PyModuleDef warningsmodule;
static WarningsState *
_Warnings_GetState()
{
PyThreadState *tstate = PyThreadState_GET();
PyThreadState *tstate = _PyThreadState_GET();
if (tstate == NULL) {
PyErr_SetString(PyExc_RuntimeError,
"_Warnings_GetState: could not identify current interpreter");
_PyErr_SetString(tstate, PyExc_RuntimeError,
"_Warnings_GetState: could not identify "
"current interpreter");
return NULL;
}
return &tstate->interp->warnings;

View File

@@ -4,6 +4,7 @@
#include <ctype.h>
#include "ast.h"
#undef Yield /* undefine macro conflicting with <winbase.h> */
#include "pycore_pyerrors.h"
#include "pycore_pystate.h"
#include "pycore_tupleobject.h"
@@ -1254,23 +1255,23 @@ map_next(mapobject *lz)
{
PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
PyObject **stack;
Py_ssize_t niters, nargs, i;
PyObject *result = NULL;
PyThreadState *tstate = _PyThreadState_GET();
niters = PyTuple_GET_SIZE(lz->iters);
const Py_ssize_t niters = PyTuple_GET_SIZE(lz->iters);
if (niters <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
stack = small_stack;
}
else {
stack = PyMem_Malloc(niters * sizeof(stack[0]));
if (stack == NULL) {
PyErr_NoMemory();
_PyErr_NoMemory(tstate);
return NULL;
}
}
nargs = 0;
for (i=0; i < niters; i++) {
Py_ssize_t nargs = 0;
for (Py_ssize_t i=0; i < niters; i++) {
PyObject *it = PyTuple_GET_ITEM(lz->iters, i);
PyObject *val = Py_TYPE(it)->tp_iternext(it);
if (val == NULL) {
@@ -1280,10 +1281,10 @@ map_next(mapobject *lz)
nargs++;
}
result = _PyObject_FastCall(lz->func, stack, nargs);
result = _PyObject_VectorcallTstate(tstate, lz->func, stack, nargs, NULL);
exit:
for (i=0; i < nargs; i++) {
for (Py_ssize_t i=0; i < nargs; i++) {
Py_DECREF(stack[i]);
}
if (stack != small_stack) {

View File

@@ -10,6 +10,7 @@
#define PY_LOCAL_AGGRESSIVE
#include "Python.h"
#include "pycore_call.h"
#include "pycore_ceval.h"
#include "pycore_code.h"
#include "pycore_object.h"
@@ -4306,7 +4307,6 @@ fail: /* Jump here from prelude on failure */
current Python frame (f), the associated C stack is still in use,
so recursion_depth must be boosted for the duration.
*/
assert(tstate != NULL);
if (Py_REFCNT(f) > 1) {
Py_DECREF(f);
_PyObject_GC_TRACK(f);
@@ -5024,10 +5024,11 @@ do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject
return NULL;
}
C_TRACE(result, _PyObject_FastCallDict(func,
&_PyTuple_ITEMS(callargs)[1],
nargs - 1,
kwdict));
C_TRACE(result, _PyObject_FastCallDictTstate(
tstate, func,
&_PyTuple_ITEMS(callargs)[1],
nargs - 1,
kwdict));
Py_DECREF(func);
return result;
}