Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Lib/test/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a crash when calling :meth:`~object.__get__` of a :term:`method` with a
:const:`None` second argument.
2 changes: 1 addition & 1 deletion Objects/descrobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading