8000 Make LOAD_CLASS_DICT also look in the global namespace and rename it … · python/cpython@e0acb87 · GitHub
[go: up one dir, main page]

Skip to content

Commit e0acb87

Browse files
committed
Make LOAD_CLASS_DICT also look in the global namespace and rename it to LOAD_CLASS_OR_GLOBAL
1 parent 84a00ce commit e0acb87

File tree

7 files changed

+430
-444
lines changed

7 files changed

+430
-444
lines changed

Include/internal/pycore_opcode.h

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/opcode.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/opcode.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ def pseudo_op(name, op, real_ops):
227227
def_op('CALL_INTRINSIC_1', 173)
228228
def_op('CALL_INTRINSIC_2', 174)
229229

230-
def_op('LOAD_CLASS_DICT', 175)
230+
def_op('LOAD_CLASS_OR_GLOBAL', 175)
231231

232232
# Instrumented instructions
233233
MIN_INSTRUMENTED_OPCODE = 238

Python/bytecodes.c

Lines changed: 14 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,81 +1158,33 @@ dummy_func(
11581158
}
11591159
}
11601160

1161-
inst(LOAD_NAME, ( -- v)) {
1162-
PyObject *name = GETITEM(frame->f_code->co_names, oparg);
1163-
PyObject *locals = LOCALS();
1164-
if (locals == NULL) {
1161+
op(_LOAD_NAME_INTRO, (-- class_dict, name)) {
1162+
name = GETITEM(frame->f_code->co_names, oparg);
1163+
class_dict = LOCALS();
1164+
if (class_dict == NULL) {
11651165
_PyErr_Format(tstate, PyExc_SystemError,
11661166
"no locals when loading %R", name);
11671167
goto error;
11681168
}
1169-
if (PyDict_CheckExact(locals)) {
1170-
v = PyDict_GetItemWithError(locals, name);
1171-
if (v != NULL) {
1172-
Py_INCREF(v);
1173-
}
1174-
else if (_PyErr_Occurred(tstate)) {
1175-
goto error;
1176-
}
1177-
}
1178-
else {
1179-
v = PyObject_GetItem(locals, name);
1180-
if (v == NULL) {
1181-
if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
1182-
goto error;
1183-
_PyErr_Clear(tstate);
1184-
}
1185-
}
1186-
if (v == NULL) {
1187-
v = PyDict_GetItemWithError(GLOBALS(), name);
1188-
if (v != NULL) {
1189-
Py_INCREF(v);
1190-
}
1191-
else if (_PyErr_Occurred(tstate)) {
1192-
goto error;
1193-
}
1194-
else {
1195-
if (PyDict_CheckExact(BUILTINS())) {
1196-
v = PyDict_GetItemWithError(BUILTINS(), name);
1197-
if (v == NULL) {
1198-
if (!_PyErr_Occurred(tstate)) {
1199-
format_exc_check_arg(
1200-
tstate, PyExc_NameError,
1201-
NAME_ERROR_MSG, name);
1202-
}
1203-
goto error;
1204-
}
1205-
Py_INCREF(v);
1206-
}
1207-
else {
1208-
v = PyObject_GetItem(BUILTINS(), name);
1209-
if (v == NULL) {
1210-
if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
1211-
format_exc_check_arg(
1212-
tstate, PyExc_NameError,
1213-
NAME_ERROR_MSG, name);
1214-
}
1215-
goto error;
1216-
}
1217-
}
1218-
}
1219-
}
12201169
}
12211170

1222-
inst(LOAD_CLASS_DICT, ( -- v)) {
1223-
PyObject *name = GETITEM(frame->f_code->co_names, oparg);
1171+
op(_LOAD_CLASS_OR_GLOBAL_INTRO, (-- class_dict, name)) {
1172+
name = GETITEM(frame->f_code->co_names, oparg);
12241173
PyFunctionObject *func = (PyFunctionObject *)frame->f_funcobj;
12251174
if (func == NULL) {
12261175
_PyErr_Format(tstate, PyExc_SystemError,
12271176
"no function defined when loading %R from class dict", name);
12281177
goto error;
12291178
}
1230-
PyObject *class_dict = func->func_class_dict;
1179+
class_dict = func->func_class_dict;
12311180
if (class_dict == NULL) {
12321181
_PyErr_Format(tstate, PyExc_SystemError,
12331182
"no class dict set when loading %R", name);
12341183
goto error;
12351184
}
1185+
}
1186+
1187+
op(_LOAD_NAME_COMMON, (class_dict, name -- v)) {
12361188
if (PyDict_CheckExact(class_dict)) {
12371189
v = PyDict_GetItemWithError(class_dict, name);
12381190
if (v != NULL) {
@@ -1286,6 +1238,10 @@ dummy_func(
12861238
}
12871239
}
12881240

1241+
macro(LOAD_NAME) = _LOAD_NAME_INTRO + _LOAD_NAME_COMMON;
1242+
1243+
macro(LOAD_CLASS_OR_GLOBAL) = _LOAD_CLASS_OR_GLOBAL_INTRO + _LOAD_NAME_COMMON;
1244+
12891245
family(load_global, INLINE_CACHE_ENTRIES_LOAD_GLOBAL) = {
12901246
LOAD_GLOBAL,
12911247
LOAD_GLOBAL_MODULE,

0 commit comments

Comments
 (0)
0