https://github.com/python/cpython/commit/9076cd085c35ae401e0e3d577759b2a937ecf085
commit: 9076cd085c35ae401e0e3d577759b2a937ecf085
branch: 3.15
author: Miss Islington (bot) <[email protected]>
committer: sobolevn <[email protected]>
date: 2026-11-06T08:31:39Z
summary:

[3.15] gh-161387: Check the type of the decimal context `ContextVar` 
(GH-161399) (#161465)

gh-161387: Check the type of the decimal context `ContextVar` (GH-161399)
(cherry picked from commit 4b50f88ba146d71f03f917215d95a320708151e9)

Co-authored-by: 0z5a <[email protected]>
Co-authored-by: sobolevn <[email protected]>

files:
A Misc/NEWS.d/next/Library/2026-11-05-10-05-00.gh-issue-161387.def456.rst
M Lib/test/test_decimal.py
M Modules/_decimal/_decimal.c

diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py
index 96cfae7d4b838b..742be41b581809 100644
--- a/Lib/test/test_decimal.py
+++ b/Lib/test/test_decimal.py
@@ -3744,6 +3744,27 @@ def test_crashing_signal_dict_flags(self):
 @requires_cdecimal
 class CContextAPItests(ContextAPItests, unittest.TestCase):
     decimal = C
+
+    def test_current_context_var_wrong_type(self):
+        # gh-161387
+        if not self.decimal.HAVE_CONTEXTVAR:
+            self.skipTest("_decimal built without context variables")
+        value = object()
+        context_vars = [var for var in contextvars.copy_context()
+                        if var.name == "decimal_context"]
+        self.assertTrue(context_vars)
+        tokens = [var.set(value) for var in context_vars]
+
+        def restore():
+            for var, token in zip(context_vars, tokens):
+                var.reset(token)
+
+        self.addCleanup(restore)
+        with self.assertRaisesRegex(TypeError, "got object"):
+            self.decimal.Decimal(1) + self.decimal.Decimal(2)
+        for var in context_vars:
+            self.assertIs(var.get(), value)
+
 class PyContextAPItests(ContextAPItests, unittest.TestCase):
     decimal = P
 
diff --git 
a/Misc/NEWS.d/next/Library/2026-11-05-10-05-00.gh-issue-161387.def456.rst 
b/Misc/NEWS.d/next/Library/2026-11-05-10-05-00.gh-issue-161387.def456.rst
new file mode 100644
index 00000000000000..f445980563853b
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-11-05-10-05-00.gh-issue-161387.def456.rst
@@ -0,0 +1,2 @@
+Fix a crash in :mod:`decimal` when the internal ``decimal_context``
+:class:`contextvars.ContextVar` holds a value that is not a context.
diff --git a/Modules/_decimal/_decimal.c b/Modules/_decimal/_decimal.c
index 7f9aa24b960a94..96c26ebdce7695 100644
--- a/Modules/_decimal/_decimal.c
+++ b/Modules/_decimal/_decimal.c
@@ -1962,6 +1962,13 @@ current_context(decimal_state *state)
     }
 
     if (tl_context != NULL) {
+        if (!PyDecContext_Check(state, tl_context)) {
+            PyErr_Format(PyExc_TypeError,
+                         "current decimal context must be a Context "
+                         "instance, got %T", tl_context);
+            Py_DECREF(tl_context);
+            return NULL;
+        }
         return tl_context;
     }
 

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to