self.assertFalse(f())
+class TestCallCache(unittest.TestCase):
+ def test_too_many_defaults_0(self):
+ def f():
+ pass
+
+ f.__defaults__ = (None,)
+ for _ in range(1025):
+ f()
+
+ def test_too_many_defaults_1(self):
+ def f(x):
+ pass
+
+ f.__defaults__ = (None, None)
+ for _ in range(1025):
+ f(None)
+ f()
+
+ def test_too_many_defaults_2(self):
+ def f(x, y):
+ pass
+
+ f.__defaults__ = (None, None, None)
+ for _ in range(1025):
+ f(None, None)
+ f(None)
+ f()
+
+
if __name__ == "__main__":
import unittest
unittest.main()
}
int argcount = code->co_argcount;
int defcount = func->func_defaults == NULL ? 0 : (int)PyTuple_GET_SIZE(func->func_defaults);
- assert(defcount <= argcount);
int min_args = argcount-defcount;
- if (nargs > argcount || nargs < min_args) {
+ // GH-105840: min_args is negative when somebody sets too many __defaults__!
+ if (min_args < 0 || nargs > argcount || nargs < min_args) {
SPECIALIZATION_FAIL(CALL, SPEC_FAIL_WRONG_NUMBER_ARGUMENTS);
return -1;
}