Issue #14385: Support other types than dict for builtins · python/cpython@b0b2242 (original) (raw)
`@@ -1932,11 +1932,26 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
`
1932
1932
`TARGET(LOAD_BUILD_CLASS)
`
1933
1933
` {
`
1934
1934
`_Py_IDENTIFIER(build_class);
`
1935
``
`-
x = PyDict_GetItemId(f->f_builtins, &PyId___build_class_);
`
1936
``
`-
if (x == NULL) {
`
1937
``
`-
PyErr_SetString(PyExc_ImportError,
`
1938
``
`-
"build_class not found");
`
1939
``
`-
break;
`
``
1935
+
``
1936
`+
if (PyDict_CheckExact(f->f_builtins)) {
`
``
1937
`+
x = PyDict_GetItemId(f->f_builtins, &PyId___build_class_);
`
``
1938
`+
if (x == NULL) {
`
``
1939
`+
PyErr_SetString(PyExc_NameError,
`
``
1940
`+
"build_class not found");
`
``
1941
`+
break;
`
``
1942
`+
}
`
``
1943
`+
}
`
``
1944
`+
else {
`
``
1945
`+
PyObject *build_class_str = PyUnicode_FromId(&PyId___build_class_);
`
``
1946
`+
if (build_class_str == NULL)
`
``
1947
`+
break;
`
``
1948
`+
x = PyObject_GetItem(f->f_builtins, build_class_str);
`
``
1949
`+
if (x == NULL) {
`
``
1950
`+
if (PyErr_ExceptionMatches(PyExc_KeyError))
`
``
1951
`+
PyErr_SetString(PyExc_NameError,
`
``
1952
`+
"build_class not found");
`
``
1953
`+
break;
`
``
1954
`+
}
`
1940
1955
` }
`
1941
1956
`Py_INCREF(x);
`
1942
1957
`PUSH(x);
`
`@@ -2078,12 +2093,24 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
`
2078
2093
`if (x == NULL) {
`
2079
2094
`x = PyDict_GetItem(f->f_globals, w);
`
2080
2095
`if (x == NULL) {
`
2081
``
`-
x = PyDict_GetItem(f->f_builtins, w);
`
2082
``
`-
if (x == NULL) {
`
2083
``
`-
format_exc_check_arg(
`
2084
``
`-
PyExc_NameError,
`
2085
``
`-
NAME_ERROR_MSG, w);
`
2086
``
`-
break;
`
``
2096
`+
if (PyDict_CheckExact(f->f_builtins)) {
`
``
2097
`+
x = PyDict_GetItem(f->f_builtins, w);
`
``
2098
`+
if (x == NULL) {
`
``
2099
`+
format_exc_check_arg(
`
``
2100
`+
PyExc_NameError,
`
``
2101
`+
NAME_ERROR_MSG, w);
`
``
2102
`+
break;
`
``
2103
`+
}
`
``
2104
`+
}
`
``
2105
`+
else {
`
``
2106
`+
x = PyObject_GetItem(f->f_builtins, w);
`
``
2107
`+
if (x == NULL) {
`
``
2108
`+
if (PyErr_ExceptionMatches(PyExc_KeyError))
`
``
2109
`+
format_exc_check_arg(
`
``
2110
`+
PyExc_NameError,
`
``
2111
`+
NAME_ERROR_MSG, w);
`
``
2112
`+
break;
`
``
2113
`+
}
`
2087
2114
` }
`
2088
2115
` }
`
2089
2116
`Py_INCREF(x);
`
`@@ -2093,50 +2120,69 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
`
2093
2120
``
2094
2121
`TARGET(LOAD_GLOBAL)
`
2095
2122
`w = GETITEM(names, oparg);
`
2096
``
`-
if (PyUnicode_CheckExact(w)) {
`
2097
``
`-
/* Inline the PyDict_GetItem() calls.
`
2098
``
`-
WARNING: this is an extreme speed hack.
`
2099
``
`-
Do not try this at home. */
`
2100
``
`-
Py_hash_t hash = ((PyASCIIObject *)w)->hash;
`
2101
``
`-
if (hash != -1) {
`
2102
``
`-
PyDictObject *d;
`
2103
``
`-
PyDictEntry *e;
`
2104
``
`-
d = (PyDictObject *)(f->f_globals);
`
2105
``
`-
e = d->ma_lookup(d, w, hash);
`
2106
``
`-
if (e == NULL) {
`
2107
``
`-
x = NULL;
`
2108
``
`-
break;
`
2109
``
`-
}
`
2110
``
`-
x = e->me_value;
`
2111
``
`-
if (x != NULL) {
`
2112
``
`-
Py_INCREF(x);
`
2113
``
`-
PUSH(x);
`
2114
``
`-
DISPATCH();
`
``
2123
`+
if (PyDict_CheckExact(f->f_globals)
`
``
2124
`+
&& PyDict_CheckExact(f->f_builtins)) {
`
``
2125
`+
if (PyUnicode_CheckExact(w)) {
`
``
2126
`+
/* Inline the PyDict_GetItem() calls.
`
``
2127
`+
WARNING: this is an extreme speed hack.
`
``
2128
`+
Do not try this at home. */
`
``
2129
`+
Py_hash_t hash = ((PyASCIIObject *)w)->hash;
`
``
2130
`+
if (hash != -1) {
`
``
2131
`+
PyDictObject *d;
`
``
2132
`+
PyDictEntry *e;
`
``
2133
`+
d = (PyDictObject *)(f->f_globals);
`
``
2134
`+
e = d->ma_lookup(d, w, hash);
`
``
2135
`+
if (e == NULL) {
`
``
2136
`+
x = NULL;
`
``
2137
`+
break;
`
``
2138
`+
}
`
``
2139
`+
x = e->me_value;
`
``
2140
`+
if (x != NULL) {
`
``
2141
`+
Py_INCREF(x);
`
``
2142
`+
PUSH(x);
`
``
2143
`+
DISPATCH();
`
``
2144
`+
}
`
``
2145
`+
d = (PyDictObject *)(f->f_builtins);
`
``
2146
`+
e = d->ma_lookup(d, w, hash);
`
``
2147
`+
if (e == NULL) {
`
``
2148
`+
x = NULL;
`
``
2149
`+
break;
`
``
2150
`+
}
`
``
2151
`+
x = e->me_value;
`
``
2152
`+
if (x != NULL) {
`
``
2153
`+
Py_INCREF(x);
`
``
2154
`+
PUSH(x);
`
``
2155
`+
DISPATCH();
`
``
2156
`+
}
`
``
2157
`+
goto load_global_error;
`
2115
2158
` }
`
2116
``
`-
d = (PyDictObject *)(f->f_builtins);
`
2117
``
`-
e = d->ma_lookup(d, w, hash);
`
2118
``
`-
if (e == NULL) {
`
2119
``
`-
x = NULL;
`
``
2159
`+
}
`
``
2160
`+
/* This is the un-inlined version of the code above */
`
``
2161
`+
x = PyDict_GetItem(f->f_globals, w);
`
``
2162
`+
if (x == NULL) {
`
``
2163
`+
x = PyDict_GetItem(f->f_builtins, w);
`
``
2164
`+
if (x == NULL) {
`
``
2165
`+
load_global_error:
`
``
2166
`+
format_exc_check_arg(
`
``
2167
`+
PyExc_NameError,
`
``
2168
`+
GLOBAL_NAME_ERROR_MSG, w);
`
2120
2169
`break;
`
2121
2170
` }
`
2122
``
`-
x = e->me_value;
`
2123
``
`-
if (x != NULL) {
`
2124
``
`-
Py_INCREF(x);
`
2125
``
`-
PUSH(x);
`
2126
``
`-
DISPATCH();
`
2127
``
`-
}
`
2128
``
`-
goto load_global_error;
`
2129
2171
` }
`
``
2172
`+
Py_INCREF(x);
`
``
2173
`+
PUSH(x);
`
``
2174
`+
DISPATCH();
`
2130
2175
` }
`
2131
``
`-
/* This is the un-inlined version of the code above */
`
2132
``
`-
x = PyDict_GetItem(f->f_globals, w);
`
``
2176
+
``
2177
`+
/* Slow-path if globals or builtins is not a dict */
`
``
2178
`+
x = PyObject_GetItem(f->f_globals, w);
`
2133
2179
`if (x == NULL) {
`
2134
``
`-
x = PyDict_GetItem(f->f_builtins, w);
`
``
2180
`+
x = PyObject_GetItem(f->f_builtins, w);
`
2135
2181
`if (x == NULL) {
`
2136
``
`-
load_global_error:
`
2137
``
`-
format_exc_check_arg(
`
2138
``
`-
PyExc_NameError,
`
2139
``
`-
GLOBAL_NAME_ERROR_MSG, w);
`
``
2182
`+
if (PyErr_ExceptionMatches(PyExc_KeyError))
`
``
2183
`+
format_exc_check_arg(
`
``
2184
`+
PyExc_NameError,
`
``
2185
`+
GLOBAL_NAME_ERROR_MSG, w);
`
2140
2186
`break;
`
2141
2187
` }
`
2142
2188
` }
`