summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorKen Jin <28750310+Fidget-Spinner@users.noreply.github.com>2022-08-04 13:53:31 (GMT)
committerGitHub <noreply@github.com>2022-08-04 13:53:31 (GMT)
commit42b102bbf9a9ae6fae8f6710202fb7afeeac277c (patch)
tree40cd485e5df107445183aa3a25b4998f1727cba3 /Modules
parent0342c93a6b866118c894c4e1120fb4db316adebb (diff)
downloadcpython-42b102bbf9a9ae6fae8f6710202fb7afeeac277c.zip
cpython-42b102bbf9a9ae6fae8f6710202fb7afeeac277c.tar.gz
cpython-42b102bbf9a9ae6fae8f6710202fb7afeeac277c.tar.bz2
gh-94936: C getters: co_varnames, co_cellvars, co_freevars (#95008)
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_testcapimodule.c80
1 files changed, 69 insertions, 11 deletions
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index f56b36a..5175914 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -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