Skip to content

Commit e38a5d3

Browse files
add more tests
1 parent 38cd1d3 commit e38a5d3

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

Lib/test/test_capi/test_opt.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2378,6 +2378,24 @@ def testfunc(n):
23782378
self.assertIn("_BINARY_OP_SUBSCR_DICT_KNOWN_HASH", uops)
23792379
self.assertNotIn("_BINARY_OP_SUBSCR_DICT", uops)
23802380

2381+
def test_binary_op_subscr_defaultdict_known_hash(self):
2382+
# str, int, bytes, float, complex, tuple and any python object which has generic hash
2383+
import collections
2384+
2385+
def testfunc(n):
2386+
x = 0
2387+
d = collections.defaultdict(lambda: 1)
2388+
for _ in range(n):
2389+
x += d['a'] + d[1] + d[b'b'] + d[(1, 2)] + d[_GENERIC_KEY] + d[1.5] + d[1+2j]
2390+
return x
2391+
2392+
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
2393+
self.assertEqual(res, 7 * TIER2_THRESHOLD)
2394+
self.assertIsNotNone(ex)
2395+
uops = get_opnames(ex)
2396+
self.assertIn("_BINARY_OP_SUBSCR_DICT_KNOWN_HASH", uops)
2397+
self.assertNotIn("_BINARY_OP_SUBSCR_DICT", uops)
2398+
23812399
def test_store_subscr_dict_known_hash(self):
23822400
# str, int, bytes, float, complex, tuple and any python object which has generic hash
23832401
def testfunc(n):
@@ -2399,6 +2417,28 @@ def testfunc(n):
23992417
self.assertIn("_STORE_SUBSCR_DICT_KNOWN_HASH", uops)
24002418
self.assertNotIn("_STORE_SUBSCR_DICT", uops)
24012419

2420+
def test_store_subscr_defaultdict_known_hash(self):
2421+
import collections
2422+
2423+
def testfunc(n):
2424+
d = collections.defaultdict(lambda: 0)
2425+
for _ in range(n):
2426+
d['a'] += 1
2427+
d[1] += 2
2428+
d[b'b'] += 3
2429+
d[(1, 2)] += 4
2430+
d[_GENERIC_KEY] += 5
2431+
d[1.5] += 6
2432+
d[1+2j] += 7
2433+
return d['a'] + d[1] + d[b'b'] + d[(1, 2)] + d[_GENERIC_KEY] + d[1.5] + d[1+2j]
2434+
2435+
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
2436+
self.assertEqual(res, 28 * TIER2_THRESHOLD)
2437+
self.assertIsNotNone(ex)
2438+
uops = get_opnames(ex)
2439+
self.assertIn("_STORE_SUBSCR_DICT_KNOWN_HASH", uops)
2440+
self.assertNotIn("_STORE_SUBSCR_DICT", uops)
2441+
24022442
def test_contains_op(self):
24032443
def testfunc(n):
24042444
x = 0

Python/bytecodes.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,7 +1248,7 @@ dummy_func(
12481248
tier2 op(_BINARY_OP_SUBSCR_DICT_KNOWN_HASH, (dict_st, sub_st, hash/4 -- res, ds, ss)) {
12491249
PyObject *sub = PyStackRef_AsPyObjectBorrow(sub_st);
12501250
PyObject *dict = PyStackRef_AsPyObjectBorrow(dict_st);
1251-
1251+
assert(Py_TYPE(dict)->tp_as_mapping->mp_subscript == _PyDict_Subscript);
12521252
STAT_INC(BINARY_OP, hit);
12531253
PyObject *res_o = _PyDict_SubscriptKnownHash(dict, sub, (Py_hash_t)hash);
12541254
if (res_o == NULL) {
@@ -1401,8 +1401,7 @@ dummy_func(
14011401

14021402
tier2 op(_STORE_SUBSCR_DICT_KNOWN_HASH, (value, dict_st, sub, hash/4 -- st)) {
14031403
PyObject *dict = PyStackRef_AsPyObjectBorrow(dict_st);
1404-
1405-
assert(PyDict_CheckExact(dict));
1404+
assert(Py_TYPE(dict)->tp_as_mapping->mp_ass_subscript == _PyDict_StoreSubscript);
14061405
STAT_INC(STORE_SUBSCR, hit);
14071406
int err = _PyDict_SetItem_Take2_KnownHash((PyDictObject *)dict,
14081407
PyStackRef_AsPyObjectSteal(sub),

Python/executor_cases.c.h

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

0 commit comments

Comments
 (0)