gh-74929: Implement PEP 667 (GH-115153)

This commit is contained in:
Tian Gao 2024-05-04 04:12:10 -07:00 committed by GitHub
parent 1ab6356ebe
commit b034f14a4b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 921 additions and 257 deletions

View file

@ -2475,12 +2475,7 @@ PyEval_GetLocals(void)
return NULL;
}
if (_PyFrame_FastToLocalsWithError(current_frame) < 0) {
return NULL;
}
PyObject *locals = current_frame->f_locals;
assert(locals != NULL);
PyObject *locals = _PyEval_GetFrameLocals();
return locals;
}
@ -2494,7 +2489,24 @@ _PyEval_GetFrameLocals(void)
return NULL;
}
return _PyFrame_GetLocals(current_frame, 1);
PyObject *locals = _PyFrame_GetLocals(current_frame);
if (locals == NULL) {
return NULL;
}
if (PyFrameLocalsProxy_Check(locals)) {
PyObject* ret = PyDict_New();
if (PyDict_Update(ret, locals)) {
Py_DECREF(ret);
return NULL;
}
Py_DECREF(locals);
return ret;
} else if (PyMapping_Check(locals)) {
return locals;
}
return NULL;
}
PyObject *
@ -2508,6 +2520,28 @@ PyEval_GetGlobals(void)
return current_frame->f_globals;
}
PyObject*
PyEval_GetFrameLocals(void)
{
return _PyEval_GetFrameLocals();
}
PyObject* PyEval_GetFrameGlobals(void)
{
PyThreadState *tstate = _PyThreadState_GET();
_PyInterpreterFrame *current_frame = _PyThreadState_GetFrame(tstate);
if (current_frame == NULL) {
return NULL;
}
return Py_XNewRef(current_frame->f_globals);
}
PyObject* PyEval_GetFrameBuiltins(void)
{
PyThreadState *tstate = _PyThreadState_GET();
return Py_XNewRef(_PyEval_GetBuiltins(tstate));
}
int
PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
{

View file

@ -123,18 +123,15 @@ static PyObject *
import_star(PyThreadState* tstate, PyObject *from)
{
_PyInterpreterFrame *frame = tstate->current_frame;
if (_PyFrame_FastToLocalsWithError(frame) < 0) {
return NULL;
}
PyObject *locals = frame->f_locals;
PyObject *locals = _PyFrame_GetLocals(frame);
if (locals == NULL) {
_PyErr_SetString(tstate, PyExc_SystemError,
"no locals found during 'import *'");
return NULL;
}
int err = import_all_from(tstate, locals, from);
_PyFrame_LocalsToFast(frame, 0);
Py_DECREF(locals);
if (err < 0) {
return NULL;
}

View file

@ -1022,13 +1022,6 @@ static PyObject *
call_trampoline(PyThreadState *tstate, PyObject* callback,
PyFrameObject *frame, int what, PyObject *arg)
{
/* Discard any previous modifications the frame's fast locals */
if (frame->f_fast_as_locals) {
if (PyFrame_FastToLocalsWithError(frame) < 0) {
return NULL;
}
}
/* call the Python-level function */
if (arg == NULL) {
arg = Py_None;
@ -1036,7 +1029,6 @@ call_trampoline(PyThreadState *tstate, PyObject* callback,
PyObject *args[3] = {(PyObject *)frame, whatstrings[what], arg};
PyObject *result = _PyObject_VectorcallTstate(tstate, callback, args, 3, NULL);
PyFrame_LocalsToFast(frame, 1);
return result;
}