Skip to content

Commit 62fae72

Browse files
committed
add regression test for set
1 parent 08a4535 commit 62fae72

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

Lib/test/test_capi/test_opt.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4392,6 +4392,26 @@ def f(n):
43924392
self.assertIn("_TO_BOOL_DICT", uops)
43934393
self.assertNotIn("_TO_BOOL", uops)
43944394

4395+
def test_to_bool_set_with_dummy_entries(self):
4396+
"""Sets with dummy entries (after discard) must evaluate as falsy.
4397+
4398+
PySetObject does not use PyObject_VAR_HEAD; reading ob_size at that
4399+
offset accidentally reads `fill`, which counts both live *and* dummy
4400+
(deleted) entries. A set whose items have all been discarded must
4401+
still be falsy even after the JIT specialises TO_BOOL.
4402+
"""
4403+
def f(n):
4404+
result = []
4405+
for _ in range(n):
4406+
s = {1}
4407+
s.discard(1) # leaves a dummy entry; fill == 1, used == 0
4408+
result.append(bool(s))
4409+
return result
4410+
4411+
res, ex = self._run_with_optimizer(f, TIER2_THRESHOLD)
4412+
# Every element must be False: the set is empty after discard()
4413+
self.assertTrue(all(r is False for r in res))
4414+
43954415
def test_attr_promotion_failure(self):
43964416
# We're not testing for any specific uops here, just
43974417
# testing it doesn't crash.

0 commit comments

Comments
 (0)