]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] gh-146615: Fix crash in __get__() for METH_METHOD descriptors with invalid...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 31 Mar 2026 08:20:00 +0000 (10:20 +0200)
committerGitHub <noreply@github.com>
Tue, 31 Mar 2026 08:20:00 +0000 (08:20 +0000)
(cherry picked from commit 72d29ea363f1515115753653aeca735a1a817a7f)

Co-authored-by: sunmy2019 <59365878+sunmy2019@users.noreply.github.com>
Lib/test/test_descr.py
Misc/NEWS.d/next/Core_and_Builtins/2026-03-31-01-06-35.gh-issue-146615.fix-method-get.rst [new file with mode: 0644]
Objects/descrobject.c

index 99f3f1ba9991303074ded5ee884cc9359f7a33c4..798074a5f637f61cd398d0cf54f2b58405445721 100644 (file)
@@ -1712,6 +1712,28 @@ class ClassPropertiesAndMethods(unittest.TestCase):
             spam_cm.__get__(None, list)
         self.assertEqual(str(cm.exception), expected_errmsg)
 
+    @support.cpython_only
+    def test_method_get_meth_method_invalid_type(self):
+        # gh-146615: method_get() for METH_METHOD descriptors used to pass
+        # Py_TYPE(type)->tp_name as the %V fallback instead of the separate
+        # %s argument, causing a missing argument for %s and a crash.
+        # Verify the error message is correct when __get__() is called with a
+        # non-type as the second argument.
+        #
+        # METH_METHOD|METH_FASTCALL|METH_KEYWORDS is the only flag combination
+        # that enters the affected branch in method_get().
+        import io
+
+        obj = io.StringIO()
+        descr = io.TextIOBase.read
+
+        with self.assertRaises(TypeError) as cm:
+            descr.__get__(obj, "not_a_type")
+        self.assertEqual(
+            str(cm.exception),
+            "descriptor 'read' needs a type, not 'str', as arg 2",
+        )
+
     def test_staticmethods(self):
         # Testing static methods...
         class C(object):
diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-03-31-01-06-35.gh-issue-146615.fix-method-get.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-03-31-01-06-35.gh-issue-146615.fix-method-get.rst
new file mode 100644 (file)
index 0000000..7a205f1
--- /dev/null
@@ -0,0 +1,3 @@
+Fix a crash in :meth:`~object.__get__` for :c:expr:`METH_METHOD` descriptors
+when an invalid (non-type) object is passed as the second argument.
+Patch by Steven Sun.
index 10c465b95ac19283ba27428cd76f770f2da2518e..07b185165aaeef95497839da6d0534824bb78e8e 100644 (file)
@@ -150,7 +150,7 @@ method_get(PyObject *self, PyObject *obj, PyObject *type)
         } else {
             PyErr_Format(PyExc_TypeError,
                         "descriptor '%V' needs a type, not '%s', as arg 2",
-                        descr_name((PyDescrObject *)descr),
+                        descr_name((PyDescrObject *)descr), "?",
                         Py_TYPE(type)->tp_name);
             return NULL;
         }