https://github.com/python/cpython/commit/bb59fdec7184227c27715316de0f40c5aa61c326
commit: bb59fdec7184227c27715316de0f40c5aa61c326
branch: 3.13
author: Peter Bierma <zintensity...@gmail.com>
committer: JelleZijlstra <jelle.zijls...@gmail.com>
date: 2025-04-21T21:38:51Z
summary:

[3.13] gh-132747: Fix `NULL` dereference when calling a method's `__get__` 
manually (GH-132772) (#132786)

(cherry picked from commit fa70bf85931eff62cb24fb2f5b7e86c1dcf642d0)

Co-authored-by: Peter Bierma <zintensity...@gmail.com>

files:
A Misc/NEWS.d/next/Core and 
Builtins/2025-04-21-07-39-59.gh-issue-132747.L-cnej.rst
M Lib/test/test_types.py
M Objects/descrobject.c

diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py
index 332f0479c3732e..d5c613b75ec74a 100644
--- a/Lib/test/test_types.py
+++ b/Lib/test/test_types.py
@@ -631,6 +631,25 @@ def test_method_descriptor_types(self):
         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
index 00000000000000..c6d45b09f64519
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and 
Builtins/2025-04-21-07-39-59.gh-issue-132747.L-cnej.rst 
@@ -0,0 +1,2 @@
+Fix a crash when calling :meth:`~object.__get__` of a :term:`method` with a
+:const:`None` second argument.
diff --git a/Objects/descrobject.c b/Objects/descrobject.c
index 4eccd1704eb95a..d33152bb85489e 100644
--- a/Objects/descrobject.c
+++ b/Objects/descrobject.c
@@ -144,7 +144,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,

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to