Skip to content

Commit 07b6f0b

Browse files
ericsnowcurrentlymiss-islington
authored andcommitted
pythongh-132775: Fix _PyFunctIon_VerifyStateless() (pythonGH-134900)
The problem we're fixing here is that we were using PyDict_Size() on "defaults", which it is actually a tuple. We're also adding some explicit type checks. This is a follow-up to pythongh-133221/pythongh-133528. (cherry picked from commit dafd141) Co-authored-by: Eric Snow <[email protected]>
1 parent 4670ddd commit 07b6f0b

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

Objects/funcobject.c

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,26 +1264,32 @@ _PyFunction_VerifyStateless(PyThreadState *tstate, PyObject *func)
12641264
}
12651265
// Disallow __defaults__.
12661266
PyObject *defaults = PyFunction_GET_DEFAULTS(func);
1267-
if (defaults != NULL && defaults != Py_None && PyDict_Size(defaults) > 0)
1268-
{
1269-
_PyErr_SetString(tstate, PyExc_ValueError, "defaults not supported");
1270-
return -1;
1267+
if (defaults != NULL) {
1268+
assert(PyTuple_Check(defaults)); // per PyFunction_New()
1269+
if (PyTuple_GET_SIZE(defaults) > 0) {
1270+
_PyErr_SetString(tstate, PyExc_ValueError,
1271+
"defaults not supported");
1272+
return -1;
1273+
}
12711274
}
12721275
// Disallow __kwdefaults__.
12731276
PyObject *kwdefaults = PyFunction_GET_KW_DEFAULTS(func);
1274-
if (kwdefaults != NULL && kwdefaults != Py_None
1275-
&& PyDict_Size(kwdefaults) > 0)
1276-
{
1277-
_PyErr_SetString(tstate, PyExc_ValueError,
1278-
"keyword defaults not supported");
1279-
return -1;
1277+
if (kwdefaults != NULL) {
1278+
assert(PyDict_Check(kwdefaults)); // per PyFunction_New()
1279+
if (PyDict_Size(kwdefaults) > 0) {
1280+
_PyErr_SetString(tstate, PyExc_ValueError,
1281+
"keyword defaults not supported");
1282+
return -1;
1283+
}
12801284
}
12811285
// Disallow __closure__.
12821286
PyObject *closure = PyFunction_GET_CLOSURE(func);
1283-
if (closure != NULL && closure != Py_None && PyTuple_GET_SIZE(closure) > 0)
1284-
{
1285-
_PyErr_SetString(tstate, PyExc_ValueError, "closures not supported");
1286-
return -1;
1287+
if (closure != NULL) {
1288+
assert(PyTuple_Check(closure)); // per PyFunction_New()
1289+
if (PyTuple_GET_SIZE(closure) > 0) {
1290+
_PyErr_SetString(tstate, PyExc_ValueError, "closures not supported");
1291+
return -1;
1292+
}
12871293
}
12881294
// Check the code.
12891295
PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);

0 commit comments

Comments
 (0)