gh-122239: Add actual count in unbalanced unpacking error message when possible (#122244)

This commit is contained in:
Tushar Sadhwani 2024-09-10 20:37:30 +05:30 committed by GitHub
parent 07f0bf5aa4
commit 3597642ed5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 83 additions and 3 deletions

View file

@ -2148,6 +2148,17 @@ _PyEval_UnpackIterableStackRef(PyThreadState *tstate, _PyStackRef v_stackref,
return 1;
}
Py_DECREF(w);
if (PyList_CheckExact(v) || PyTuple_CheckExact(v)
|| PyDict_CheckExact(v)) {
ll = PyDict_CheckExact(v) ? PyDict_Size(v) : Py_SIZE(v);
if (ll > argcnt) {
_PyErr_Format(tstate, PyExc_ValueError,
"too many values to unpack (expected %d, got %zd)",
argcnt, ll);
goto Error;
}
}
_PyErr_Format(tstate, PyExc_ValueError,
"too many values to unpack (expected %d)",
argcnt);