]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-132747: Fix `NULL` dereference when calling a method's `__get__` manually (#132772)
authorPeter Bierma <zintensitydev@gmail.com>
Mon, 21 Apr 2025 21:09:34 +0000 (17:09 -0400)
committerGitHub <noreply@github.com>
Mon, 21 Apr 2025 21:09:34 +0000 (14:09 -0700)
Lib/test/test_types.py
Misc/NEWS.d/next/Core_and_Builtins/2025-04-21-07-39-59.gh-issue-132747.L-cnej.rst [new file with mode: 0644]
Objects/descrobject.c

index e5d80ee8eb7aca89ef840f175db03de201238dab..3552b6b4ef846c9151d299702c1ce7a562ebf752 100644 (file)
@@ -653,6 +653,25 @@ class TypesTests(unittest.TestCase):
         self.assertIsInstance(int.from_bytes, types.BuiltinMethodType)
         self.assertIsInstance(int.__new__, types.BuiltinMethodType)
 
+    def test_method_descriptor_crash(self):
+        # gh-132747: The default __get__() implementation in C was unable
+        # to handle a second argument of None when called from Python
+        import _io
+        import io
+        import _queue
+
+        to_check = [
+            # (method, instance)
+            (_io._TextIOBase.read, io.StringIO()),
+            (_queue.SimpleQueue.put, _queue.SimpleQueue()),
+            (str.capitalize, "nobody expects the spanish inquisition")
+        ]
+
+        for method, instance in to_check:
+            with self.subTest(method=method, instance=instance):
+                bound = method.__get__(instance)
+                self.assertIsInstance(bound, types.BuiltinMethodType)
+
     def test_ellipsis_type(self):
         self.assertIsInstance(Ellipsis, types.EllipsisType)
 
diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-04-21-07-39-59.gh-issue-132747.L-cnej.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-04-21-07-39-59.gh-issue-132747.L-cnej.rst
new file mode 100644 (file)
index 0000000..c6d45b0
--- /dev/null
@@ -0,0 +1,2 @@
+Fix a crash when calling :meth:`~object.__get__` of a :term:`method` with a
+:const:`None` second argument.
index 268af0b217cd98c0899c0f4c5c1d7ca667f40d82..5ff36edd3ddb72ddc78180aca0ef4500d26576db 100644 (file)
@@ -145,7 +145,7 @@ method_get(PyObject *self, PyObject *obj, PyObject *type)
         return NULL;
     }
     if (descr->d_method->ml_flags & METH_METHOD) {
-        if (PyType_Check(type)) {
+        if (type == NULL || PyType_Check(type)) {
             return PyCMethod_New(descr->d_method, obj, NULL, descr->d_common.d_type);
         } else {
             PyErr_Format(PyExc_TypeError,