mirror of
https://github.com/python/cpython.git
synced 2025-07-15 07:15:18 +00:00
dSupport non-dict globals in LOAD_FROM_DICT_OR_GLOBALS
The implementation basically copies LOAD_GLOBAL. Possibly it could be deduplicated,
but that seems like it may get hairy since the two operations have different operands.
This is important to fix in 3.14 for PEP 649, but it's a bug in earlier versions too,
and we should backport to 3.13 and 3.12 if possible.
(cherry picked from commit 80a4e38994
)
This commit is contained in:
parent
0a266f7e74
commit
a0559849ac
8 changed files with 76 additions and 59 deletions
|
@ -1375,18 +1375,35 @@ dummy_func(
|
|||
ERROR_NO_POP();
|
||||
}
|
||||
if (v == NULL) {
|
||||
if (PyDict_GetItemRef(GLOBALS(), name, &v) < 0) {
|
||||
ERROR_NO_POP();
|
||||
}
|
||||
if (v == NULL) {
|
||||
if (PyMapping_GetOptionalItem(BUILTINS(), name, &v) < 0) {
|
||||
if (PyDict_CheckExact(GLOBALS())
|
||||
&& PyDict_CheckExact(BUILTINS()))
|
||||
{
|
||||
v = _PyDict_LoadGlobal((PyDictObject *)GLOBALS(),
|
||||
(PyDictObject *)BUILTINS(),
|
||||
name);
|
||||
if (v == NULL) {
|
||||
if (!_PyErr_Occurred(tstate)) {
|
||||
/* _PyDict_LoadGlobal() returns NULL without raising
|
||||
* an exception if the key doesn't exist */
|
||||
_PyEval_FormatExcCheckArg(tstate, PyExc_NameError,
|
||||
NAME_ERROR_MSG, name);
|
||||
}
|
||||
ERROR_NO_POP();
|
||||
}
|
||||
}
|
||||
else {
|
||||
/* Slow-path if globals or builtins is not a dict */
|
||||
/* namespace 1: globals */
|
||||
ERROR_IF(PyMapping_GetOptionalItem(GLOBALS(), name, &v) < 0, error);
|
||||
if (v == NULL) {
|
||||
_PyEval_FormatExcCheckArg(
|
||||
tstate, PyExc_NameError,
|
||||
NAME_ERROR_MSG, name);
|
||||
ERROR_NO_POP();
|
||||
/* namespace 2: builtins */
|
||||
ERROR_IF(PyMapping_GetOptionalItem(BUILTINS(), name, &v) < 0, error);
|
||||
if (v == NULL) {
|
||||
_PyEval_FormatExcCheckArg(
|
||||
tstate, PyExc_NameError,
|
||||
NAME_ERROR_MSG, name);
|
||||
ERROR_IF(true, error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
30
Python/executor_cases.c.h
generated
30
Python/executor_cases.c.h
generated
|
@ -1394,35 +1394,7 @@
|
|||
break;
|
||||
}
|
||||
|
||||
case _LOAD_FROM_DICT_OR_GLOBALS: {
|
||||
PyObject *mod_or_class_dict;
|
||||
PyObject *v;
|
||||
oparg = CURRENT_OPARG();
|
||||
mod_or_class_dict = stack_pointer[-1];
|
||||
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg);
|
||||
if (PyMapping_GetOptionalItem(mod_or_class_dict, name, &v) < 0) {
|
||||
JUMP_TO_ERROR();
|
||||
}
|
||||
if (v == NULL) {
|
||||
if (PyDict_GetItemRef(GLOBALS(), name, &v) < 0) {
|
||||
JUMP_TO_ERROR();
|
||||
}
|
||||
if (v == NULL) {
|
||||
if (PyMapping_GetOptionalItem(BUILTINS(), name, &v) < 0) {
|
||||
JUMP_TO_ERROR();
|
||||
}
|
||||
if (v == NULL) {
|
||||
_PyEval_FormatExcCheckArg(
|
||||
tstate, PyExc_NameError,
|
||||
NAME_ERROR_MSG, name);
|
||||
JUMP_TO_ERROR();
|
||||
}
|
||||
}
|
||||
}
|
||||
Py_DECREF(mod_or_class_dict);
|
||||
stack_pointer[-1] = v;
|
||||
break;
|
||||
}
|
||||
/* _LOAD_FROM_DICT_OR_GLOBALS is not a viable micro-op for tier 2 because it has both popping and not-popping errors */
|
||||
|
||||
/* _LOAD_NAME is not a viable micro-op for tier 2 because it has both popping and not-popping errors */
|
||||
|
||||
|
|
35
Python/generated_cases.c.h
generated
35
Python/generated_cases.c.h
generated
|
@ -4381,18 +4381,35 @@
|
|||
goto error;
|
||||
}
|
||||
if (v == NULL) {
|
||||
if (PyDict_GetItemRef(GLOBALS(), name, &v) < 0) {
|
||||
goto error;
|
||||
}
|
||||
if (v == NULL) {
|
||||
if (PyMapping_GetOptionalItem(BUILTINS(), name, &v) < 0) {
|
||||
if (PyDict_CheckExact(GLOBALS())
|
||||
&& PyDict_CheckExact(BUILTINS()))
|
||||
{
|
||||
v = _PyDict_LoadGlobal((PyDictObject *)GLOBALS(),
|
||||
(PyDictObject *)BUILTINS(),
|
||||
name);
|
||||
if (v == NULL) {
|
||||
if (!_PyErr_Occurred(tstate)) {
|
||||
/* _PyDict_LoadGlobal() returns NULL without raising
|
||||
* an exception if the key doesn't exist */
|
||||
_PyEval_FormatExcCheckArg(tstate, PyExc_NameError,
|
||||
NAME_ERROR_MSG, name);
|
||||
}
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
else {
|
||||
/* Slow-path if globals or builtins is not a dict */
|
||||
/* namespace 1: globals */
|
||||
if (PyMapping_GetOptionalItem(GLOBALS(), name, &v) < 0) goto pop_1_error;
|
||||
if (v == NULL) {
|
||||
_PyEval_FormatExcCheckArg(
|
||||
tstate, PyExc_NameError,
|
||||
NAME_ERROR_MSG, name);
|
||||
goto error;
|
||||
/* namespace 2: builtins */
|
||||
if (PyMapping_GetOptionalItem(BUILTINS(), name, &v) < 0) goto pop_1_error;
|
||||
if (v == NULL) {
|
||||
_PyEval_FormatExcCheckArg(
|
||||
tstate, PyExc_NameError,
|
||||
NAME_ERROR_MSG, name);
|
||||
if (true) goto pop_1_error;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
8
Python/optimizer_cases.c.h
generated
8
Python/optimizer_cases.c.h
generated
|
@ -815,13 +815,7 @@
|
|||
break;
|
||||
}
|
||||
|
||||
case _LOAD_FROM_DICT_OR_GLOBALS: {
|
||||
_Py_UopsSymbol *v;
|
||||
v = sym_new_not_null(ctx);
|
||||
if (v == NULL) goto out_of_space;
|
||||
stack_pointer[-1] = v;
|
||||
break;
|
||||
}
|
||||
/* _LOAD_FROM_DICT_OR_GLOBALS is not a viable micro-op for tier 2 */
|
||||
|
||||
/* _LOAD_NAME is not a viable micro-op for tier 2 */
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue