Skip to content
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

GH-100987: Allow objects other than code objects as the "executable" of an internal frame. #105727

Merged
merged 10 commits into from
Jun 14, 2023
Prev Previous commit
Next Next commit
Merge branch 'main' into allow-any-object-as-code
  • Loading branch information
markshannon committed Jun 9, 2023
commit ea51766ec6977519e513c0ea390aba27e1711f6d
50 changes: 25 additions & 25 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ dummy_func(
}

inst(LOAD_CONST, (-- value)) {
value = GETITEM(CONSTS(), oparg);
value = GETITEM(FRAME_CO_CONSTS, oparg);
Py_INCREF(value);
}

Expand Down Expand Up @@ -709,7 +709,7 @@ dummy_func(
}

inst(RETURN_CONST, (--)) {
PyObject *retval = GETITEM(CONSTS(), oparg);
PyObject *retval = GETITEM(FRAME_CO_CONSTS, oparg);
Py_INCREF(retval);
assert(EMPTY());
_PyFrame_SetStackPointer(frame, stack_pointer);
Expand All @@ -725,7 +725,7 @@ dummy_func(
}

inst(INSTRUMENTED_RETURN_CONST, (--)) {
PyObject *retval = GETITEM(_PyFrame_GetCode(frame)->co_consts, oparg);
PyObject *retval = GETITEM(FRAME_CO_CONSTS, oparg);
int err = _Py_call_instrumentation_arg(
tstate, PY_MONITORING_EVENT_PY_RETURN,
frame, next_instr-1, retval);
Expand Down Expand Up @@ -1040,7 +1040,7 @@ dummy_func(


inst(STORE_NAME, (v -- )) {
PyObject *name = GETITEM(NAMES(), oparg);
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg);
PyObject *ns = LOCALS();
int err;
if (ns == NULL) {
Expand All @@ -1058,7 +1058,7 @@ dummy_func(
}

inst(DELETE_NAME, (--)) {
PyObject *name = GETITEM(NAMES(), oparg);
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg);
PyObject *ns = LOCALS();
int err;
if (ns == NULL) {
Expand Down Expand Up @@ -1150,7 +1150,7 @@ dummy_func(
inst(STORE_ATTR, (counter/1, unused/3, v, owner --)) {
#if ENABLE_SPECIALIZATION
if (ADAPTIVE_COUNTER_IS_ZERO(counter)) {
PyObject *name = GETITEM(NAMES(), oparg);
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg);
next_instr--;
_Py_Specialize_StoreAttr(owner, next_instr, name);
DISPATCH_SAME_OPARG();
Expand All @@ -1161,28 +1161,28 @@ dummy_func(
#else
(void)counter; // Unused.
#endif /* ENABLE_SPECIALIZATION */
PyObject *name = GETITEM(NAMES(), oparg);
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg);
int err = PyObject_SetAttr(owner, name, v);
DECREF_INPUTS();
ERROR_IF(err, error);
}

inst(DELETE_ATTR, (owner --)) {
PyObject *name = GETITEM(NAMES(), oparg);
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg);
int err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
DECREF_INPUTS();
ERROR_IF(err, error);
}

inst(STORE_GLOBAL, (v --)) {
PyObject *name = GETITEM(NAMES(), oparg);
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg);
int err = PyDict_SetItem(GLOBALS(), name, v);
DECREF_INPUTS();
ERROR_IF(err, error);
}

inst(DELETE_GLOBAL, (--)) {
PyObject *name = GETITEM(NAMES(), oparg);
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg);
int err;
err = PyDict_DelItem(GLOBALS(), name);
// Can't use ERROR_IF here.
Expand All @@ -1208,7 +1208,7 @@ dummy_func(
macro(LOAD_LOCALS) = _LOAD_LOCALS;

op(_LOAD_FROM_DICT_OR_GLOBALS, (mod_or_class_dict -- v)) {
PyObject *name = GETITEM(NAMES(), oparg);
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg);
if (PyDict_CheckExact(mod_or_class_dict)) {
v = PyDict_GetItemWithError(mod_or_class_dict, name);
if (v != NULL) {
Expand Down Expand Up @@ -1280,15 +1280,15 @@ dummy_func(
#if ENABLE_SPECIALIZATION
_PyLoadGlobalCache *cache = (_PyLoadGlobalCache *)next_instr;
if (ADAPTIVE_COUNTER_IS_ZERO(cache->counter)) {
PyObject *name = GETITEM(NAMES(), oparg>>1);
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg>>1);
next_instr--;
_Py_Specialize_LoadGlobal(GLOBALS(), BUILTINS(), next_instr, name);
DISPATCH_SAME_OPARG();
}
STAT_INC(LOAD_GLOBAL, deferred);
DECREMENT_ADAPTIVE_COUNTER(cache->counter);
#endif /* ENABLE_SPECIALIZATION */
PyObject *name = GETITEM(NAMES(), oparg>>1);
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg>>1);
if (PyDict_CheckExact(GLOBALS())
&& PyDict_CheckExact(BUILTINS()))
{
Expand Down Expand Up @@ -1630,7 +1630,7 @@ dummy_func(
};

inst(LOAD_SUPER_ATTR, (unused/1, global_super, class, self -- res2 if (oparg & 1), res)) {
PyObject *name = GETITEM(_PyFrame_GetCode(frame)->co_names, oparg >> 2);
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg >> 2);
int load_method = oparg & 1;
#if ENABLE_SPECIALIZATION
_PySuperAttrCache *cache = (_PySuperAttrCache *)next_instr;
Expand Down Expand Up @@ -1695,7 +1695,7 @@ dummy_func(
DEOPT_IF(global_super != (PyObject *)&PySuper_Type, LOAD_SUPER_ATTR);
DEOPT_IF(!PyType_Check(class), LOAD_SUPER_ATTR);
STAT_INC(LOAD_SUPER_ATTR, hit);
PyObject *name = GETITEM(NAMES(), oparg >> 2);
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg >> 2);
res = _PySuper_Lookup((PyTypeObject *)class, self, name, NULL);
DECREF_INPUTS();
ERROR_IF(res == NULL, error);
Expand All @@ -1706,7 +1706,7 @@ dummy_func(
DEOPT_IF(global_super != (PyObject *)&PySuper_Type, LOAD_SUPER_ATTR);
DEOPT_IF(!PyType_Check(class), LOAD_SUPER_ATTR);
STAT_INC(LOAD_SUPER_ATTR, hit);
PyObject *name = GETITEM(NAMES(), oparg >> 2);
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg >> 2);
PyTypeObject *cls = (PyTypeObject *)class;
int method_found = 0;
res2 = _PySuper_Lookup(cls, self, name,
Expand Down Expand Up @@ -1744,15 +1744,15 @@ dummy_func(
#if ENABLE_SPECIALIZATION
_PyAttrCache *cache = (_PyAttrCache *)next_instr;
if (ADAPTIVE_COUNTER_IS_ZERO(cache->counter)) {
PyObject *name = GETITEM(NAMES(), oparg>>1);
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg>>1);
next_instr--;
_Py_Specialize_LoadAttr(owner, next_instr, name);
DISPATCH_SAME_OPARG();
}
STAT_INC(LOAD_ATTR, deferred);
DECREMENT_ADAPTIVE_COUNTER(cache->counter);
#endif /* ENABLE_SPECIALIZATION */
PyObject *name = GETITEM(NAMES(), oparg >> 1);
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg >> 1);
if (oparg & 1) {
/* Designed to work in tandem with CALL, pushes two values. */
PyObject* meth = NULL;
Expand Down Expand Up @@ -1834,7 +1834,7 @@ dummy_func(
PyDictObject *dict = (PyDictObject *)_PyDictOrValues_GetDict(dorv);
DEOPT_IF(dict == NULL, LOAD_ATTR);
assert(PyDict_CheckExact((PyObject *)dict));
PyObject *name = GETITEM(NAMES(), oparg>>1);
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg>>1);
uint16_t hint = index;
DEOPT_IF(hint >= (size_t)dict->ma_keys->dk_nentries, LOAD_ATTR);
if (DK_IS_UNICODE(dict->ma_keys)) {
Expand Down Expand Up @@ -1922,7 +1922,7 @@ dummy_func(
DEOPT_IF(!_PyThreadState_HasStackSpace(tstate, code->co_framesize), LOAD_ATTR);
STAT_INC(LOAD_ATTR, hit);

PyObject *name = GETITEM(NAMES(), oparg >> 1);
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg >> 1);
Py_INCREF(f);
_PyInterpreterFrame *new_frame = _PyFrame_PushUnchecked(tstate, f, 2);
// Manipulate stack directly because we exit with DISPATCH_INLINED().
Expand Down Expand Up @@ -1966,7 +1966,7 @@ dummy_func(
PyDictObject *dict = (PyDictObject *)_PyDictOrValues_GetDict(dorv);
DEOPT_IF(dict == NULL, STORE_ATTR);
assert(PyDict_CheckExact((PyObject *)dict));
PyObject *name = GETITEM(NAMES(), oparg);
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg);
DEOPT_IF(hint >= (size_t)dict->ma_keys->dk_nentries, STORE_ATTR);
PyObject *old_value;
uint64_t new_version;
Expand Down Expand Up @@ -2126,14 +2126,14 @@ dummy_func(
}

inst(IMPORT_NAME, (level, fromlist -- res)) {
PyObject *name = GETITEM(NAMES(), oparg);
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg);
res = import_name(tstate, frame, name, fromlist, level);
DECREF_INPUTS();
ERROR_IF(res == NULL, error);
}

inst(IMPORT_FROM, (from -- from, res)) {
PyObject *name = GETITEM(NAMES(), oparg);
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg);
res = import_from(tstate, from, name);
ERROR_IF(res == NULL, error);
}
Expand Down Expand Up @@ -2638,8 +2638,8 @@ dummy_func(

inst(KW_NAMES, (--)) {
assert(kwnames == NULL);
assert(oparg < PyTuple_GET_SIZE(CONSTS()));
kwnames = GETITEM(CONSTS(), oparg);
assert(oparg < PyTuple_GET_SIZE(FRAME_CO_CONSTS));
kwnames = GETITEM(FRAME_CO_CONSTS, oparg);
}

inst(INSTRUMENTED_CALL, ( -- )) {
Expand Down
4 changes: 2 additions & 2 deletions Python/ceval_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ GETITEM(PyObject *v, Py_ssize_t i) {


/* Data access macros */
#define FRAME_CO_CONSTS (frame->f_code->co_consts)
#define FRAME_CO_NAMES (frame->f_code->co_names)
#define FRAME_CO_CONSTS (_PyFrame_GetCode(frame)->co_consts)
#define FRAME_CO_NAMES (_PyFrame_GetCode(frame)->co_names)

/* Local variable macros */

Expand Down
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.