diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2022-08-04 14:16:52 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-04 14:16:52 (GMT) |
commit | f2926358d1cd70625222eaf4b541584d2f2a1272 (patch) | |
tree | 37ed377090939bb2f3bee77b1bdf7ed925d0a82d /Modules | |
parent | c521c5cdc3f766ac5ac3574fa142aeec11c14267 (diff) | |
download | cpython-f2926358d1cd70625222eaf4b541584d2f2a1272.zip cpython-f2926358d1cd70625222eaf4b541584d2f2a1272.tar.gz cpython-f2926358d1cd70625222eaf4b541584d2f2a1272.tar.bz2 |
gh-94936: C getters: co_varnames, co_cellvars, co_freevars (GH-95008)
(cherry picked from commit 42b102bbf9a9ae6fae8f6710202fb7afeeac277c)
Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com>
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_testcapimodule.c | 80 |
1 files changed, 69 insertions, 11 deletions
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index b0a4687..961616d 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -5952,21 +5952,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 |