Skip to content

Commit fa70bf8

Browse files
gh-132747: Fix NULL dereference when calling a method's __get__ manually (#132772)
1 parent c8e0b6e commit fa70bf8

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

Lib/test/test_types.py

+19
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,25 @@ def test_method_descriptor_types(self):
653653
self.assertIsInstance(int.from_bytes, types.BuiltinMethodType)
654654
self.assertIsInstance(int.__new__, types.BuiltinMethodType)
655655

656+
def test_method_descriptor_crash(self):
657+
# gh-132747: The default __get__() implementation in C was unable
658+
# to handle a second argument of None when called from Python
659+
import _io
660+
import io
661+
import _queue
662+
663+
to_check = [
664+
# (method, instance)
665+
(_io._TextIOBase.read, io.StringIO()),
666+
(_queue.SimpleQueue.put, _queue.SimpleQueue()),
667+
(str.capitalize, "nobody expects the spanish inquisition")
668+
]
669+
670+
for method, instance in to_check:
671+
with self.subTest(method=method, instance=instance):
672+
bound = method.__get__(instance)
673+
self.assertIsInstance(bound, types.BuiltinMethodType)
674+
656675
def test_ellipsis_type(self):
657676
self.assertIsInstance(Ellipsis, types.EllipsisType)
658677

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a crash when calling :meth:`~object.__get__` of a :term:`method` with a
2+
:const:`None` second argument.

Objects/descrobject.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ method_get(PyObject *self, PyObject *obj, PyObject *type)
145145
return NULL;
146146
}
147147
if (descr->d_method->ml_flags & METH_METHOD) {
148-
if (PyType_Check(type)) {
148+
if (type == NULL || PyType_Check(type)) {
149149
return PyCMethod_New(descr->d_method, obj, NULL, descr->d_common.d_type);
150150
} else {
151151
PyErr_Format(PyExc_TypeError,

0 commit comments

Comments
 (0)