Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bb59fde

Browse filesBrowse 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
Copy full SHA for bb59fde

File tree

Expand file treeCollapse file tree

3 files changed

+22
-1
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+22
-1
lines changed

‎Lib/test/test_types.py

Copy file name to clipboardExpand all lines: Lib/test/test_types.py
+19Lines 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

+2Lines 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

Copy file name to clipboardExpand all lines: Objects/descrobject.c
+1-1Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.