Alberto Perrella Alberto Perrella - 1 year ago 94
Python Question

How to create a code object in python?

I'd like to create a new code object with the function types.CodeType() .

There is almost no documentation about this and the existing one says "not for faint of heart"

Tell me what i need and give me some information about each argument passed to types.CodeType ,

possibly posting an example.


In normal use cases you will just need the builtin function compile()

You should use types.CodeType() only if you want to create new instructions that couldn't be obtained writing normal source code and that require direct access to bytecode.

Answer Source

Disclaimer :
Documentation in this answer is not official and may be incorrect.

This answer is valid only for python version 3.x


In order to create a code object you have to pass to the function CodeType() the following arguments:

        argcount,             #   integer
        kwonlyargcount,       #   integer
        nlocals,              #   integer
        stacksize,            #   integer
        flags,                #   integer
        codestring,           #   bytes
        consts,               #   tuple
        names,                #   tuple
        varnames,             #   tuple
        filename,             #   string
        name,                 #   string
        firstlineno,          #   integer
        lnotab,               #   bytes
        freevars,             #   tuple
        cellvars              #   tuple

Now i will try to explain what is the meaning of each argument.

Number of arguments to be passed to the function (*args and **kwargs are not included).

Number of keyword-only arguments.

Number of local variables ,
namely all variables and parameters(*args and **kwargs included) except global names.

stacksize The amount of stack (virtual machine stack) required by the code ,
if you want to understand how it works , see official Documentation.

A bitmap that says something about the code object:
1 –> code was optimized
2 –> newlocals: there is a new local namespace(for example a function)
4 –> the code accepts an arbitrary number of positional arguments (*args is used)
8 –> the code accepts an arbitrary number of keyworded arguments (*kwargs is used)
32 –> the code is a generator

othes flags are used in older python versions or are activated to say what is imported from __ future __

A sequence of bytes representing bytecode instructions
if you want a better understanding , see Documentation (same as above)

A tuple containing literals used by the bytecode (for example pre-computed numbers, tuples,and strings)

A tuple containing names used by the bytecode
this names are global variables, functions and classes or also attributes loaded from objects

A tuple containing local names used by the bytecode (arguments first, then local variables)

It is the filename from which the code was compiled.
It can be whatever you want,you are free to lie about this. ;)

It gives the name of the function. Also this can be whatever you want,but be careful:
this is the name shown in the traceback,if the name is unclear,the traceback could be unclear,
just think about how lambdas can be annoying.

The first line of the function (for debug purpose if you compiled source code)

A mapping of bytes that correlates bytecode offsets to line numbers.
(i think also this is for debug purpose,there is few documentation about this)

A tuple containing the names of free variables.
Free variables are variables declared in the namespace where the code object was defined, they are used when nested functions are declared;
this doesn't happen at module level because in that case free variables are also global variables.

A tuple containing names of local variables referenced by nested functions.

Examples :
following examples should clarify the meaning of what has been said above.

Note: in finished code objects attributes mentioned above have the co_ prefix,
and a function stores its executable body in the __code__ attribute

1st Example

def F(a,b):
    global c

print(F.__code__.co_nlocals , F.__code__.co_varnames)


5 ('a', 'b', 'k', 'w', 'p')
('c' ,)
(None, 10, 1, 'two'. 3, (1, 'two', 3))
  1. there are two arguments passed to this function ("a","b")

  2. this function has two parameters("a","b") and three local variables("k","w","p")

  3. disassembling the function bytecode we obtain this:

    3         0 LOAD_FAST                0 (a)             #stack:  ["a"] 
              3 LOAD_GLOBAL              0 (c)             #stack:  ["a","c"]
              6 BINARY_MULTIPLY                            #stack:  [result of a*c]
              7 STORE_FAST               2 (k)             #stack:  []
    4        10 LOAD_CONST               1 (10)            #stack:  [10]
             13 STORE_FAST               3 (w)             #stack:  []
    5        16 LOAD_CONST               5 ((1, 'two', 3)) #stack:  [(1,"two",3)]
             19 STORE_FAST               4 (p)             #stack:  []
             22 LOAD_CONST               0 (None)          #stack:  [None]
             25 RETURN_VALUE                               #stack:  []

    as you can notice chile executing the function we never have more than three elements in the stack (tuple counts as its lenght in this case)

  4. flag's value is dec 67 = bin 1000011 = bin 1000000 +10 +1 = dec 64 +2 +1 ,so we understand that

    • the code is optimized(as most of the automatically generated code is)
    • while executing the function bytecode local namespace changes
    • 64? Actually i don't know what is its meaning
  5. the only global name that is used in the function is "c" , it is stored in co_names

  6. every explicit literal we use is stored in co_consts:

    • None is the return value of the function
    • we explicitly assign the number 10 to w
    • we explicitly assign (1, 'two', 3) to p
    • if the tuple is a constant each element of that tuple is a constant,so 1,"two",3 are constants

2nd example


def F():

    def G():
        return (FunctionVar,ModuleVar)




('print', '__code__', 'co_freevars', 'co_names', 'ModuleVar')

the meaning of the output is this:

first and second line are printed when F is executed,so they show co_freevars and co_names of G code:
"FunctionVar" is in the namespace of F function,where G was created,
"ModuleVar" instead is a module variable,so it is considered as global.

following three lines are about co_cellvars,co_freevars and co_names attributes of F code:
"FunctionVar" is referenced in the G nested function ,so it is marked as a cellvar,
"ModuleVar" is in the namespace where F was created,but it is a module variable,
so it is not marked as freevar,but it is found in global names.
also the builtin function print is marked in names , and all the names of attributes used in F.

3rd example

This is a working code object initialization,
this is unuseful but you can do everything you want with this function.

MyCode= CodeType(
        bytes([101, 0, 0,    #Load print function
               101, 1, 0,    #Load name 'a'
               101, 2, 0,    #Load name 'b'
               23,           #Take first two stack elements and store their sum
               131, 1, 0,    #Call first element in the stack with one positional argument
               1,            #Pop top of stack
               101, 0, 0,    #Load print function
               101, 1, 0,    #Load name 'a'
               101, 2, 0,    #Load name 'b'
               20,           #Take first two stack elements and store their product
               131, 1, 0,    #Call first element in the stack with one positional argument
               1,            #Pop top of stack
               100, 0, 0,    #Load constant None
               83]),         #Return top of stack
        ('print', 'a', 'b'),
        () )

exec(MyCode) # code prints the sum and the product of "a" and "b"


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download