Learnings about CodeType
I wanted to start collecting my learning about the code object, as I created this library. Unfortunately, I had this idea after I was mostly done, so it is by no means complete.
This is the list of local variables names. It is accessed in the evaluation loop by
Are all the
co_varnames used by these operations? Or is it important to save unused
co_varnames so they get copied to the frame and used in some other way?#
Not every co_varname is used. Most codes actually included unused varnames!
This is because these are used for the function arguments. It also includes the local variables… Weird they are put in the same one!
We cannot eliminate unused ones. That is actually enforced during bytecode creation, to make sure we have enough
co_varnames for the args.
co_varnames after the argument names used in the code?#
Mostly yes. The only place they don’t get used is if they are optimized away, like in a
if False: ... block. So we
can safely remove all the unused varnames, which are not arguments.
Are all the code with arguments “functions”? i.e. do they have the
Yes all code data with args are functions!
If we look in
dis we see that any of the “hasfree” commands will index into
cell_names = co.co_cellvars + co.co_freevars.
The ones marked as using it are:
Let’s take a look at
ceval.c to see where they are used.
LOAD_CLASSDEREF is the clearest, in that it requires the arg to index into
and be larger than
The rest all index into
freevars directly, which is defined as
f->f_localsplus + co->co_nlocals.
f_localsplus contains both the
varnames locals plus the cells and freevars.
nlocals == len(varnames), this offsets the array to start looking at the
cellvars and freevars. This is created in