https://github.com/python/cpython/commit/4b50f88ba146d71f03f917215d95a320708151e9
commit: 4b50f88ba146d71f03f917215d95a320708151e9
branch: main
author: 0z5a <[email protected]>
committer: sobolevn <[email protected]>
date: 2026-11-06T10:55:26+03:00
summary:

gh-161387: Check the type of the decimal context `ContextVar` (#161399)

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 c4b24617af7c43..ef7aa5358b5cc6 100644
--- a/Lib/test/test_decimal.py
+++ b/Lib/test/test_decimal.py
@@ -3780,6 +3780,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 098522b130ec87..3a128a13222f9b 100644
--- a/Modules/_decimal/_decimal.c
+++ b/Modules/_decimal/_decimal.c
@@ -2003,6 +2003,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