summaryrefslogtreecommitdiffstats
path: root/Include
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 /Include
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 'Include')
-rw-r--r--Include/cpython/code.h57
1 files changed, 44 insertions, 13 deletions
diff --git a/Include/cpython/code.h b/Include/cpython/code.h
index 575a4b7..990f367 100644
--- a/Include/cpython/code.h
+++ b/Include/cpython/code.h
@@ -17,31 +17,62 @@ typedef struct _PyOpcache _PyOpcache;
/* Bytecode object */
struct PyCodeObject {
PyObject_HEAD
+
+ /* Note only the following fields are used in hash and/or comparisons
+ *
+ * - co_name
+ * - co_argcount
+ * - co_posonlyargcount
+ * - co_kwonlyargcount
+ * - co_nlocals
+ * - co_stacksize
+ * - co_flags
+ * - co_firstlineno
+ * - co_code
+ * - co_consts
+ * - co_names
+ * - co_varnames
+ * - co_freevars
+ * - co_cellvars
+ *
+ * This is done to preserve the name and line number for tracebacks
+ * and debuggers; otherwise, constant de-duplication would collapse
+ * identical functions/lambdas defined on different lines.
+ */
+
+ /* These fields are set with provided values on new code objects. */
+
+ // The hottest fields (in the eval loop) are grouped here at the top.
+ PyObject *co_code; /* instruction opcodes */
+ PyObject *co_consts; /* list (constants used) */
+ PyObject *co_names; /* list of strings (names used) */
+ int co_flags; /* CO_..., see below */
+ // The rest are not so impactful on performance.
int co_argcount; /* #arguments, except *args */
int co_posonlyargcount; /* #positional only arguments */
int co_kwonlyargcount; /* #keyword only arguments */
- int co_nlocals; /* #local variables */
int co_stacksize; /* #entries needed for evaluation stack */
- int co_flags; /* CO_..., see below */
int co_firstlineno; /* first source line number */
- PyObject *co_code; /* instruction opcodes */
- PyObject *co_consts; /* list (constants used) */
- PyObject *co_names; /* list of strings (names used) */
PyObject *co_varnames; /* tuple of strings (local variable names) */
- PyObject *co_freevars; /* tuple of strings (free variable names) */
PyObject *co_cellvars; /* tuple of strings (cell variable names) */
- /* The rest aren't used in either hash or comparisons, except for co_name,
- used in both. This is done to preserve the name and line number
- for tracebacks and debuggers; otherwise, constant de-duplication
- would collapse identical functions/lambdas defined on different lines.
- */
- Py_ssize_t *co_cell2arg; /* Maps cell vars which are arguments. */
+ PyObject *co_freevars; /* tuple of strings (free variable names) */
PyObject *co_filename; /* unicode (where it was loaded from) */
PyObject *co_name; /* unicode (name, for reference) */
PyObject *co_linetable; /* string (encoding addr<->lineno mapping) See
Objects/lnotab_notes.txt for details. */
- int co_nlocalsplus; /* Number of locals + free + cell variables */
PyObject *co_exceptiontable; /* Byte string encoding exception handling table */
+
+ /* These fields are set with computed values on new code objects. */
+
+ Py_ssize_t *co_cell2arg; /* Maps cell vars which are arguments. */
+ // These are redundant but offer some performance benefit.
+ int co_nlocalsplus; /* number of local + cell + free variables */
+ int co_nlocals; /* number of local variables */
+ int co_ncellvars; /* number of cell variables */
+ int co_nfreevars; /* number of free variables */
+
+ /* The remaining fields are zeroed out on new code objects. */
+
PyObject *co_weakreflist; /* to support weakrefs to code objects */
/* Scratch space for extra data relating to the code object.
Type is a void* to keep the format private in codeobject.c to force