Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added .jit-stamp
Empty file.
2 changes: 1 addition & 1 deletion Include/internal/pycore_uop_metadata.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import contextlib
import enum
import itertools
import sys
import textwrap
Expand Down Expand Up @@ -3511,6 +3512,50 @@ def f(n):
self.assertNotIn("_LOAD_ATTR_METHOD_NO_DICT", uops)
self.assertIn("_LOAD_CONST_INLINE_BORROW", uops)

def test_load_attr_class_with_metaclass_check(self):
# LOAD_ATTR_CLASS_WITH_METACLASS_CHECK must check
# for `__class__` writes, see gh-149239
class ColorMeta(enum.EnumType):
Comment on lines +3515 to +3518
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be in test_opcache.

pass

class Color(enum.IntEnum, metaclass=ColorMeta):
RED = 1

red = Color.RED

def f1(n):
for _ in range(n):
assert Color.RED == 1
return n

res, ex = self._run_with_optimizer(f1, TIER2_THRESHOLD)
self.assertIsNotNone(ex)
self.assertEqual(res, TIER2_THRESHOLD)
uops = get_opnames(ex)
self.assertIn("_CHECK_ATTR_CLASS", uops)
self.assertIn("_GUARD_TYPE_VERSION", uops)

# Reassign the `__class__` attr to deopt:
class Descriptor(enum.IntEnum):
RED = 1

def __get__(self, obj, owner):
return "descr"

red.__class__ = Descriptor

def f2(n):
for _ in range(n):
assert Color.RED == 'descr'
return n

res, ex = self._run_with_optimizer(f2, TIER2_THRESHOLD)
self.assertIsNotNone(ex)
self.assertEqual(res, TIER2_THRESHOLD)
uops = get_opnames(ex)
self.assertNotIn("_CHECK_ATTR_CLASS", uops)
self.assertNotIn("_GUARD_TYPE_VERSION", uops)

def test_cached_load_special(self):
class CM:
def __enter__(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Deopt ``LOAD_ATTR_CLASS_WITH_METACLASS_CHECK`` opcode on ``__class__``
reassigning.
16 changes: 16 additions & 0 deletions Modules/_testinternalcapi/test_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2976,6 +2976,11 @@ dummy_func(
}

op(_LOAD_ATTR_CLASS, (descr/4, owner -- attr)) {
PyTypeObject *descr_type = Py_TYPE(descr);
PyObject *owner_o = PyStackRef_AsPyObjectBorrow(owner);
EXIT_IF((descr_type->tp_flags & Py_TPFLAGS_IMMUTABLETYPE) == 0
&& descr_type != (PyTypeObject *)owner_o);

STAT_INC(LOAD_ATTR, hit);
assert(descr != NULL);
attr = PyStackRef_FromPyObjectNew(descr);
Expand Down
9 changes: 9 additions & 0 deletions Python/executor_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading