diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2022-07-28 06:52:00 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-28 06:52:00 (GMT) |
commit | 563f0584c9aa80457f3478c2470b603721479eb0 (patch) | |
tree | 60099946f54a6c2189f08d9505c0ddc9c86d0595 /Lib/test/test_getargs2.py | |
parent | fb422147d39eba4b56b9bb9d9be73d07e748dda9 (diff) | |
download | cpython-563f0584c9aa80457f3478c2470b603721479eb0.zip cpython-563f0584c9aa80457f3478c2470b603721479eb0.tar.gz cpython-563f0584c9aa80457f3478c2470b603721479eb0.tar.bz2 |
[3.10] gh-94938: Fix errror detection of unexpected keyword arguments (GH-94999) (GH-95354)
When keyword argument name is an instance of a str subclass with
overloaded methods __eq__ and __hash__, the former code could not find
the name of an extraneous keyword argument to report an error, and
_PyArg_UnpackKeywords() returned success without setting the
corresponding cell in the linearized arguments array. But since the number
of expected initialized cells is determined as the total number of passed
arguments, this lead to reading NULL as a keyword parameter value, that
caused SystemError or crash or other undesired behavior..
(cherry picked from commit ebad53a4dc1bb591820724a22cef9b8459185b5f)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Diffstat (limited to 'Lib/test/test_getargs2.py')
-rw-r--r-- | Lib/test/test_getargs2.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/Lib/test/test_getargs2.py b/Lib/test/test_getargs2.py index fd1952c..72b6d64 100644 --- a/Lib/test/test_getargs2.py +++ b/Lib/test/test_getargs2.py @@ -747,6 +747,33 @@ class KeywordOnly_TestCase(unittest.TestCase): "'\udc80' is an invalid keyword argument for this function"): getargs_keyword_only(1, 2, **{'\uDC80': 10}) + def test_weird_str_subclass(self): + class BadStr(str): + def __eq__(self, other): + return True + def __hash__(self): + # Guaranteed different hash + return str.__hash__(self) ^ 3 + with self.assertRaisesRegex(TypeError, + "invalid keyword argument for this function"): + getargs_keyword_only(1, 2, **{BadStr("keyword_only"): 3}) + with self.assertRaisesRegex(TypeError, + "invalid keyword argument for this function"): + getargs_keyword_only(1, 2, **{BadStr("monster"): 666}) + + def test_weird_str_subclass2(self): + class BadStr(str): + def __eq__(self, other): + return False + def __hash__(self): + return str.__hash__(self) + with self.assertRaisesRegex(TypeError, + "invalid keyword argument for this function"): + getargs_keyword_only(1, 2, **{BadStr("keyword_only"): 3}) + with self.assertRaisesRegex(TypeError, + "invalid keyword argument for this function"): + getargs_keyword_only(1, 2, **{BadStr("monster"): 666}) + class PositionalOnlyAndKeywords_TestCase(unittest.TestCase): from _testcapi import getargs_positional_only_and_keywords as getargs |