Skip to content

Commit a217fc4

Browse files
committed
use interp->callable_cache
1 parent c3bb232 commit a217fc4

File tree

8 files changed

+57
-40
lines changed

8 files changed

+57
-40
lines changed

Include/internal/pycore_interp_structs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,8 @@ struct _Py_unicode_state {
655655
struct callable_cache {
656656
PyObject *isinstance;
657657
PyObject *len;
658+
PyObject *all;
659+
PyObject *any;
658660
PyObject *list_append;
659661
PyObject *object__getattribute__;
660662
};

Include/internal/pycore_opcode_metadata.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.

Include/internal/pycore_uop_metadata.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/test/test_builtin.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,6 @@ def f_tuple():
264264

265265

266266
# check the overriding the builtins works
267-
bltin_outputs = [f() for f in funcs]
268267

269268
global all, any, tuple
270269
saved = all, any, tuple
@@ -273,15 +272,26 @@ def f_tuple():
273272
any = lambda x : "any"
274273
tuple = lambda x : "tuple"
275274

276-
return [f() for f in funcs]
275+
overridden_outputs = [f() for f in funcs]
277276
finally:
278277
all, any, tuple = saved
279278

280-
overridden_outputs = run_with_overrides()
279+
self.assertEqual(overridden_outputs, ['all', 'any', 'tuple'])
280+
281+
# Now repeat, overriding the builtins module as well
282+
saved = all, any, tuple
283+
try:
284+
builtins.all = all = lambda x : "all"
285+
builtins.any = any = lambda x : "any"
286+
builtins.tuple = tuple = lambda x : "tuple"
287+
288+
overridden_outputs = [f() for f in funcs]
289+
finally:
290+
all, any, tuple = saved
291+
builtins.all, builtins.any, builtins.tuple = saved
292+
293+
self.assertEqual(overridden_outputs, ['all', 'any', 'tuple'])
281294

282-
for f, out1, out2 in zip(funcs, bltin_outputs, overridden_outputs):
283-
with self.subTest(func = f.__name__):
284-
self.assertNotEqual(out1, out2)
285295

286296
def test_ascii(self):
287297
self.assertEqual(ascii(''), '\'\'')

Python/bytecodes.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,26 +1405,25 @@ dummy_func(
14051405
inst(LOAD_COMMON_CONSTANT, ( -- value)) {
14061406
// Keep in sync with _common_constants in opcode.py
14071407
// If we ever have more than two constants, use a lookup table
1408-
PyObject *val;
14091408
if (oparg == CONSTANT_ASSERTIONERROR) {
1410-
val = PyExc_AssertionError;
1409+
value = PyStackRef_FromPyObjectImmortal(PyExc_AssertionError);
14111410
}
14121411
else if (oparg == CONSTANT_NOTIMPLEMENTEDERROR) {
1413-
val = PyExc_NotImplementedError;
1412+
value = PyStackRef_FromPyObjectImmortal(PyExc_NotImplementedError);
14141413
}
14151414
else if (oparg == CONSTANT_BUILTIN_TUPLE) {
1416-
val = (PyObject*)&PyTuple_Type;
1415+
value = PyStackRef_FromPyObjectImmortal((PyObject*)&PyTuple_Type);
14171416
}
14181417
else if (oparg == CONSTANT_BUILTIN_ALL) {
1419-
val = PyDict_GetItemWithError(BUILTINS(), &_Py_ID(all));
1418+
value = PyStackRef_FromPyObjectNew(tstate->interp->callable_cache.all);
14201419
}
14211420
else if (oparg == CONSTANT_BUILTIN_ANY) {
1422-
val = PyDict_GetItemWithError(BUILTINS(), &_Py_ID(any));
1421+
value = PyStackRef_FromPyObjectNew(tstate->interp->callable_cache.any);
14231422
}
14241423
else {
1425-
Py_UNREACHABLE();
1424+
_PyErr_SetString(tstate, PyExc_ValueError, "unknown common const");
1425+
ERROR_IF(true, error);
14261426
}
1427-
value = PyStackRef_FromPyObjectNew(val);
14281427
}
14291428

14301429
inst(LOAD_BUILD_CLASS, ( -- bc)) {

Python/executor_cases.c.h

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

Python/generated_cases.c.h

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

Python/pylifecycle.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,18 @@ pycore_init_builtins(PyThreadState *tstate)
794794
}
795795
interp->callable_cache.len = len;
796796

797+
PyObject *all = PyDict_GetItemWithError(builtins_dict, &_Py_ID(all));
798+
if (!all) {
799+
goto error;
800+
}
801+
interp->callable_cache.all = all;
802+
803+
PyObject *any = PyDict_GetItemWithError(builtins_dict, &_Py_ID(any));
804+
if (!any) {
805+
goto error;
806+
}
807+
interp->callable_cache.any = any;
808+
797809
PyObject *list_append = _PyType_Lookup(&PyList_Type, &_Py_ID(append));
798810
if (list_append == NULL) {
799811
goto error;

0 commit comments

Comments
 (0)