mirror of
https://github.com/python/cpython.git
synced 2025-11-10 02:21:40 +00:00
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.
This commit is contained in:
parent
e6c815d2e3
commit
6cc800d363
7 changed files with 148 additions and 83 deletions
|
|
@ -17,31 +17,62 @@ typedef struct _PyOpcache _PyOpcache;
|
|||
/* Bytecode object */
|
||||
struct PyCodeObject {
|
||||
PyObject_HEAD
|
||||
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 */
|
||||
|
||||
/* 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_stacksize; /* #entries needed for evaluation stack */
|
||||
int co_firstlineno; /* first source line number */
|
||||
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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue