Skip to content

Commit 589b9f2

Browse files
committed
Test that the wrong-typed context is not leaked
1 parent 360d166 commit 589b9f2

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

‎Lib/test/test_decimal.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3771,18 +3771,22 @@ def test_current_context_var_wrong_type(self):
37713771
# gh-161387
37723772
if not self.decimal.HAVE_CONTEXTVAR:
37733773
self.skipTest("_decimal built without context variables")
3774+
value = object()
37743775
context_vars = [var for var in contextvars.copy_context()
37753776
if var.name == "decimal_context"]
37763777
self.assertTrue(context_vars)
3777-
tokens = [var.set(1) for var in context_vars]
3778+
tokens = [var.set(value) for var in context_vars]
37783779

37793780
def restore():
37803781
for var, token in zip(context_vars, tokens):
37813782
var.reset(token)
37823783

37833784
self.addCleanup(restore)
3784-
with self.assertRaises(TypeError):
3785-
self.decimal.Decimal(1) + self.decimal.Decimal(2)
3785+
refcount = sys.getrefcount(value)
3786+
for _ in range(3):
3787+
with self.assertRaises(TypeError):
3788+
self.decimal.Decimal(1) + self.decimal.Decimal(2)
3789+
self.assertEqual(sys.getrefcount(value), refcount)
37863790

37873791
class PyContextAPItests(ContextAPItests, unittest.TestCase):
37883792
decimal = P

0 commit comments

Comments
 (0)