8000 gh-115999: Enable BINARY_SUBSCR_GETITEM for free-threaded build by corona10 · Pull Request #127737 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-115999: Enable BINARY_SUBSCR_GETITEM for free-threaded build #127737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Dec 19, 2024
Merged
Prev Previous commit
Next Next commit
fix
  • Loading branch information
corona10 committed Dec 16, 2024
commit 42ed55b8262a55a56e573210c83d6ba45d6d88e8
17 changes: 10 additions & 7 deletions Lib/test/test_opcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ def assert_races_do_not_crash(
for writer in writers:
writer.join()

@requires_specialization_ft
@requires_specialization
def test_binary_subscr_getitem(self):
def get_items():
class C:
Expand Down Expand Up @@ -1245,6 +1245,14 @@ def f(o, n):
f(test_obj, 1)
self.assertEqual(test_obj.b, 0)

# gh-115999: BINARY_SUBSCR_GETITEM will only cache __getitem__ methods that
# are deferred. We only defer functions defined at the top-level.
class CGetItem:
def __init__(self, val):
self.val = val
def __getitem__(self, item):
return self.val


class TestSpecializer(TestBase):

Expand Down Expand Up @@ -1521,12 +1529,7 @@ def binary_subscr_str_int():
self.assert_no_opcode(binary_subscr_str_int, "BINARY_SUBSCR")

def binary_subscr_getitems():
class C:
def __init__(self, val):
self.val = val
def __getitem__(self, item):
return self.val
items = [C(i) for i in range(100)]
items = [CGetItem(i) for i in range(100)]
8000 for i in range(100):
self.assertEqual(items[i][i], i)

Expand Down
8 changes: 5 additions & 3 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -5693,17 +5693,19 @@ _PyType_CacheInitForSpecialization(PyHeapTypeObject *type, PyObject *init,
}

int
_PyType_CacheGetItemForSpecialization(PyHeapTypeObject *ht, PyObject *descriptor, uint32_t version)
_PyType_CacheGetItemForSpecialization(PyHeapTypeObject *ht, PyObject *descriptor, uint32_t tp_version)
{
if (!descriptor || !version) {
if (!descriptor || !tp_version) {
return 0;
}
int can_cache;
BEGIN_TYPE_LOCK();
can_cache = ((PyTypeObject*)ht)->tp_version_tag == tp_version;
// This pointer is invalidated by PyType_Modified (see the comment on
// struct _specialization_cache):
PyFunctionObject *func = (PyFunctionObject *)descriptor;
can_cache = _PyFunction_GetVersionForCurrentState(func) == version;
uint32_t version = _PyFunction_GetVersionForCurrentState(func);
can_cache = _PyFunction_IsVersionValid(version);
#ifdef Py_GIL_DISABLED
can_cache = can_cache && _PyObject_HasDeferredRefcount(descriptor);
#endif
Expand Down
2 changes: 1 addition & 1 deletion Python/specialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -1797,8 +1797,8 @@ _Py_Specialize_BinarySubscr(
Py_DECREF(descriptor);
goto success;
}
Py_DECREF(descriptor);
}
Py_XDECREF(descriptor);
SPECIALIZATION_FAIL(BINARY_SUBSCR,
binary_subscr_fail_kind(container_type, sub));
fail:
Expand Down
Loading
0