summaryrefslogtreecommitdiffstats
path: root/Objects/funcobject.c
diff options
context:
space:
mode:
authorEric Snow <ericsnowcurrently@gmail.com>2021-05-26 19:15:40 (GMT)
committerGitHub <noreply@github.com>2021-05-26 19:15:40 (GMT)
commit6cc800d3634fdd002b986c3ffe6a3d5540f311a0 (patch)
treec6e67a6f76549102011931f2ee929b455ab40918 /Objects/funcobject.c
parente6c815d2e34be5fdf6dbe773f0781691746d2289 (diff)
downloadcpython-6cc800d3634fdd002b986c3ffe6a3d5540f311a0.zip
cpython-6cc800d3634fdd002b986c3ffe6a3d5540f311a0.tar.gz
cpython-6cc800d3634fdd002b986c3ffe6a3d5540f311a0.tar.bz2
bpo-43693: Clean up the PyCodeObject fields. (GH-26364)
* Move up the comment about fields using in hashing/comparision. * Group the fields more clearly. * Add co_ncellvars and co_nfreevars. * Raise ValueError if nlocals != len(varnames), rather than aborting.
Diffstat (limited to 'Objects/funcobject.c')
-rw-r--r--Objects/funcobject.c14
1 files changed, 7 insertions, 7 deletions
diff --git a/Objects/funcobject.c b/Objects/funcobject.c
index f0b0b67..dc27215 100644
--- a/Objects/funcobject.c
+++ b/Objects/funcobject.c
@@ -279,7 +279,8 @@ func_get_code(PyFunctionObject *op, void *Py_UNUSED(ignored))
static int
func_set_code(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
{
- Py_ssize_t nfree, nclosure;
+ Py_ssize_t nclosure;
+ int nfree;
/* Not legal to del f.func_code or to set it to anything
* other than a code object. */
@@ -294,7 +295,7 @@ func_set_code(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
return -1;
}
- nfree = PyCode_GetNumFree((PyCodeObject *)value);
+ nfree = ((PyCodeObject *)value)->co_nfreevars;
nclosure = (op->func_closure == NULL ? 0 :
PyTuple_GET_SIZE(op->func_closure));
if (nclosure != nfree) {
@@ -538,7 +539,7 @@ func_new_impl(PyTypeObject *type, PyCodeObject *code, PyObject *globals,
/*[clinic end generated code: output=99c6d9da3a24e3be input=93611752fc2daf11]*/
{
PyFunctionObject *newfunc;
- Py_ssize_t nfree, nclosure;
+ Py_ssize_t nclosure;
if (name != Py_None && !PyUnicode_Check(name)) {
PyErr_SetString(PyExc_TypeError,
@@ -550,9 +551,8 @@ func_new_impl(PyTypeObject *type, PyCodeObject *code, PyObject *globals,
"arg 4 (defaults) must be None or tuple");
return NULL;
}
- nfree = PyTuple_GET_SIZE(code->co_freevars);
if (!PyTuple_Check(closure)) {
- if (nfree && closure == Py_None) {
+ if (code->co_nfreevars && closure == Py_None) {
PyErr_SetString(PyExc_TypeError,
"arg 5 (closure) must be tuple");
return NULL;
@@ -566,10 +566,10 @@ func_new_impl(PyTypeObject *type, PyCodeObject *code, PyObject *globals,
/* check that the closure is well-formed */
nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);
- if (nfree != nclosure)
+ if (code->co_nfreevars != nclosure)
return PyErr_Format(PyExc_ValueError,
"%U requires closure of length %zd, not %zd",
- code->co_name, nfree, nclosure);
+ code->co_name, code->co_nfreevars, nclosure);
if (nclosure) {
Py_ssize_t i;
for (i = 0; i < nclosure; i++) {