diff options
author | Jelle Zijlstra <jelle.zijlstra@gmail.com> | 2024-05-31 21:05:24 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-31 21:05:24 (GMT) |
commit | 80a4e3899420faaa012c82b4e82cdb6675a6a944 (patch) | |
tree | a9bde6c673e54d2c5f55397f89e2f2a0d323fcea /Python/generated_cases.c.h | |
parent | 2237946af0981c46dc7d3886477e425ccfb37f28 (diff) | |
download | cpython-80a4e3899420faaa012c82b4e82cdb6675a6a944.zip cpython-80a4e3899420faaa012c82b4e82cdb6675a6a944.tar.gz cpython-80a4e3899420faaa012c82b4e82cdb6675a6a944.tar.bz2 |
gh-119821: Support non-dict globals in LOAD_FROM_DICT_OR_GLOBALS (#119822)
Support 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.
Diffstat (limited to 'Python/generated_cases.c.h')
-rw-r--r-- | Python/generated_cases.c.h | 35 |
1 files changed, 26 insertions, 9 deletions
diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 4402787..1a99160 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -4401,18 +4401,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; + } } } } |