Skip to content

Commit 9efb16b

Browse files
committed
Fix crash in method_get for METH_METHOD descriptors with invalid type argument
1 parent accae92 commit 9efb16b

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

Lib/test/test_descr.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1803,6 +1803,28 @@ class SubSpam(spam.spamlist): pass
18031803
spam_cm.__get__(None, list)
18041804
self.assertEqual(str(cm.exception), expected_errmsg)
18051805

1806+
def test_method_get_meth_method_invalid_type(self):
1807+
# gh-146615: method_get() for METH_METHOD descriptors used to pass
1808+
# Py_TYPE(type)->tp_name as the %V fallback instead of the separate
1809+
# %s argument, causing a missing argument for %s and a crash.
1810+
# Verify the error message is correct when __get__() is called with a
1811+
# non-type as the second argument.
1812+
#
1813+
# METH_METHOD|METH_FASTCALL|METH_KEYWORDS is the only flag combination
1814+
# that enters the affected branch in method_get().
1815+
1816+
import xxlimited
1817+
1818+
xxo = xxlimited.Xxo()
1819+
descr = xxlimited.Xxo.demo
1820+
1821+
with self.assertRaises(TypeError) as cm:
1822+
descr.__get__(xxo, "not_a_type")
1823+
self.assertEqual(
1824+
str(cm.exception),
1825+
"descriptor 'demo' needs a type, not 'str', as arg 2",
1826+
)
1827+
18061828
def test_staticmethods(self):
18071829
# Testing static methods...
18081830
class C(object):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a crash in :meth:`~object.__get__` for :c:expr:`METH_METHOD` descriptors
2+
when an invalid (non-type) object is passed as the second argument.
3+
Patch by Steven Sun.

0 commit comments

Comments
 (0)