Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
22 changes: 22 additions & 0 deletions Lib/test/test_descr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1803,6 +1803,28 @@ class SubSpam(spam.spamlist): pass
spam_cm.__get__(None, list)
self.assertEqual(str(cm.exception), expected_errmsg)

def test_method_get_meth_method_invalid_type(self):
# gh-146615: method_get() for METH_METHOD descriptors used to pass
# Py_TYPE(type)->tp_name as the %V fallback instead of the separate
# %s argument, causing a missing argument for %s and a crash.
# Verify the error message is correct when __get__() is called with a
# non-type as the second argument.
#
# METH_METHOD|METH_FASTCALL|METH_KEYWORDS is the only flag combination
# that enters the affected branch in method_get().

import xxlimited
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be tested without the use of the optional xxlimited module. For example:

array.array.extend.__get__(array.array('I'), 'not_a_type')

Of course, using METH_METHOD|METH_FASTCALL|METH_KEYWORDS in the implementation of array.array.extend is CPython specific, so you can try several other classes and methods in hope that they are implemented in such way. If none of them raises a TypeError, then skip the test (skipTest()). Or maybe mark the test CPython-only?

Copy link
Copy Markdown
Member Author

@sunmy2019 sunmy2019 Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mark the test CPython-only?

I think this is reasonable. This problem only occurs in C modules. It cannot trigger in pure Python code.

I checked

array.array.extend.__get__(array.array('I'), 'not_a_type')

will not raise error in Pypy.


xxo = xxlimited.Xxo()
descr = xxlimited.Xxo.demo

with self.assertRaises(TypeError) as cm:
descr.__get__(xxo, "not_a_type")
self.assertEqual(
str(cm.exception),
"descriptor 'demo' needs a type, not 'str', as arg 2",
)

def test_staticmethods(self):
# Testing static methods...
class C(object):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix a crash in :meth:`~object.__get__` for :c:expr:`METH_METHOD` descriptors
when an invalid (non-type) object is passed as the second argument.
Patch by Steven Sun.
2 changes: 1 addition & 1 deletion Objects/descrobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ method_get(PyObject *self, PyObject *obj, PyObject *type)
} else {
PyErr_Format(PyExc_TypeError,
"descriptor '%V' needs a type, not '%s', as arg 2",
descr_name((PyDescrObject *)descr),
descr_name((PyDescrObject *)descr), "?",
Py_TYPE(type)->tp_name);
return NULL;
}
Expand Down
Loading