VM Architecture.

Oren Tirosh oren-py-l at hishome.net
Sun Aug 11 07:11:57 CEST 2002

On Sat, Aug 10, 2002 at 06:26:36PM +0000, Venkatesh Prasad Ranganath wrote:
> I am looking for documentation about Python VM architecture to 
> understand how does one compile python code to bytecode and how
> does the vm assimilate it. I need to know this to check if a project is 
> feasible. I could find the set of byt code
> instructions in the module index that comes in python documentation. 
> However, it uses some variables like co_names and stuff
> which I dould not find the documentation for. May be I have overlooked. 
> If somebody knows of any documentation about how the
> Python vm works, in terms of it's guts, can you please give a link to it?

I'm afraid that some of this stuff has no documentation except the source
code.  I find the Python source code quite readable after getting used to
its conventions and concepts. The introspection capabilities of Python and
the dis module should be very useful, too:

>>> def f(x):
...   a = hex(x)
>>> dir(f)
['__call__', '__class__', '__delattr__', '__dict__', '__doc__', '__get__',
'__getattribute__', '__hash__', '__init__', '__name__', '__new__',
'__reduce__', '__repr__', '__setattr__', '__str__', 'func_closure',
'func_code', 'func_defaults',
'func_dict', 'func_doc', 'func_globals', 'func_name']
>>> dir(f.func_code)
['__class__', '__cmp__', '__delattr__', '__doc__', '__getattribute__',
'__hash__', '__init__', '__new__', '__reduce__', '__repr__', '__setattr__',
'__str__', 'co_argcount', 'co_cellvars', 'co_code', 'co_consts',
'co_filename', 'co_firstlineno', 'co_flags', 'co_freevars', 'co_lnotab',
'co_name', 'co_names', 'co_nlocals', 'co_stacksize', 'co_varnames']
>>> f.func_code.co_names
('hex', 'x', 'a')
>>> import dis
>>> dis.dis(f)
          0 SET_LINENO               1

          3 SET_LINENO               2
          6 LOAD_GLOBAL              0 (hex)
          9 LOAD_FAST                0 (x)
         12 CALL_FUNCTION            1
         15 STORE_FAST               1 (a)
         18 LOAD_CONST               0 (None)
         21 RETURN_VALUE

co_names is one of the attributes of a code object. It is a tuple of the 
names used in the code.  The argument to LOAD_GLOBAL is an index into this 
name list. LOAD_FAST and STORE_FAST are for local variables that bypass 
name-based lookup for improved performance.

The Python VM is a pretty conventional stack machine. What makes it such 
a dynamic language is that this VM uses name-based lookup for almost 
everything (with the exception of local variables). The bytecode compiler 
doesn't care if you refer to a nonexistent name - it will be resolved at 
runtime. One of the important results of this is that a syntactically 
correct Python source can be deterministically compiled into bytecode 
without referring to the modules it imports or any other external 
dependencies.  Compare this to C where the generated code depends on the 
content of include files or Java where compiling a class requires the 
classes it dependes on. No complex build procedures or dependencies 
are required in Python.


More information about the Python-list mailing list