gh-94936: C getters: co_varnames, co_cellvars, co_freevars (#95008)

This commit is contained in:
Ken Jin 2022-08-04 21:53:31 +08:00 committed by GitHub
parent 0342c93a6b
commit 42b102bbf9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 128 additions and 15 deletions

View file

@ -5600,21 +5600,79 @@ test_code_api(PyObject *self, PyObject *Py_UNUSED(args))
if (co == NULL) {
return NULL;
}
PyObject *co_code = PyCode_GetCode(co);
if (co_code == NULL) {
Py_DECREF(co);
return NULL;
}
assert(PyBytes_CheckExact(co_code));
if (PyObject_Length(co_code) == 0) {
PyErr_SetString(PyExc_ValueError, "empty co_code");
Py_DECREF(co);
/* co_code */
{
PyObject *co_code = PyCode_GetCode(co);
if (co_code == NULL) {
goto fail;
}
assert(PyBytes_CheckExact(co_code));
if (PyObject_Length(co_code) == 0) {
PyErr_SetString(PyExc_ValueError, "empty co_code");
Py_DECREF(co_code);
goto fail;
}
Py_DECREF(co_code);
return NULL;
}
/* co_varnames */
{
PyObject *co_varnames = PyCode_GetVarnames(co);
if (co_varnames == NULL) {
goto fail;
}
if (!PyTuple_CheckExact(co_varnames)) {
PyErr_SetString(PyExc_TypeError, "co_varnames not tuple");
Py_DECREF(co_varnames);
goto fail;
}
if (PyTuple_GET_SIZE(co_varnames) != 0) {
PyErr_SetString(PyExc_ValueError, "non-empty co_varnames");
Py_DECREF(co_varnames);
goto fail;
}
Py_DECREF(co_varnames);
}
/* co_cellvars */
{
PyObject *co_cellvars = PyCode_GetCellvars(co);
if (co_cellvars == NULL) {
goto fail;
}
if (!PyTuple_CheckExact(co_cellvars)) {
PyErr_SetString(PyExc_TypeError, "co_cellvars not tuple");
Py_DECREF(co_cellvars);
goto fail;
}
if (PyTuple_GET_SIZE(co_cellvars) != 0) {
PyErr_SetString(PyExc_ValueError, "non-empty co_cellvars");
Py_DECREF(co_cellvars);
goto fail;
}
Py_DECREF(co_cellvars);
}
/* co_freevars */
{
PyObject *co_freevars = PyCode_GetFreevars(co);
if (co_freevars == NULL) {
goto fail;
}
if (!PyTuple_CheckExact(co_freevars)) {
PyErr_SetString(PyExc_TypeError, "co_freevars not tuple");
Py_DECREF(co_freevars);
goto fail;
}
if (PyTuple_GET_SIZE(co_freevars) != 0) {
PyErr_SetString(PyExc_ValueError, "non-empty co_freevars");
Py_DECREF(co_freevars);
goto fail;
}
Py_DECREF(co_freevars);
}
Py_DECREF(co);
Py_DECREF(co_code);
Py_RETURN_NONE;
fail:
Py_DECREF(co);
return NULL;
}
static int