8000 [3.13] gh-132747: Fix `NULL` dereference when calling a method's `__g… · python/cpython@bb59fde · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit bb59fde

Browse files
[3.13] gh-132747: Fix NULL dereference when calling a method's __get__ manually (GH-132772) (#132786)
(cherry picked from commit fa70bf8) Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
1 parent d8b9011 commit bb59fde

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

Lib/test/test_types.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,25 @@ def test_method_descriptor_types(self):
631631
self.assertIsInstance(int.from_bytes, types.BuiltinMethodType)
632632
self.assertIsInstance(int.__new__, types.BuiltinMethodType)
633633

634+
def test_method_descriptor_crash(self):
635+
# gh-132747: The default __get__() implementation in C was unable
636+
# to handle a second argument of None when called from Python
637+
import _io
638+
import io
639+
import _queue
640+
641+
to_check = [
642+
# (method, instance)
643+
(_io._TextIOBase.read, io.StringIO()),
644+
(_queue.SimpleQueue.put, _queue.SimpleQueue()),
645+
(str.capitalize, "nobody expects the spanish inquisition")
646+
]
647+
648+
for method, instance in to_check:
649+
with self.subTest(method=method, instance=instance):
650+
bound = method.__get__(instance)
651+
self.assertIsInstance(bound, types.BuiltinMethodType)
652+
634653
def test_ellipsis_type(self):
635654
self.assertIsInstance(Ellipsis, types.EllipsisType)
636655

Lines changed: 2 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ method_get(PyObject *self, PyObject *obj, PyObject *type)
144144
return NULL;
145145
}
146146
if (descr->d_method->ml_flags & METH_METHOD) {
147-
if (PyType_Check(type)) {
147+
if (type == NULL || PyType_Check(type)) {
148148
return PyCMethod_New(descr->d_method, obj, NULL, descr->d_common.d_type);
149149
} else {
150150
PyErr_Format(PyExc_TypeError,

0 commit comments

Comments
 (0)
0