Python interpreter/compiler internals 
Author Message
 Python interpreter/compiler internals

Hi,

before I embark on a (long) journey throught the python code, I wonder
whether there is any documentation on the internals of python. For
instance a description of the virtual machine, or something like that,
the different architectural parts, etc. I've read the extension/embeding
document, but that still provides a very interface like description.

                                Gerco.

--
----------------------------------v------------------------------------
G.C. Ballintijn                   |      Oh, Fortuna

http://www.*-*-*.com/ ~gerco/       |      Statu variabilis.....



Tue, 07 Sep 1999 03:00:00 GMT  
 Python interpreter/compiler internals

Quote:

> before I embark on a (long) journey throught the python code, I wonder
> whether there is any documentation on the internals of python. For
> instance a description of the virtual machine, or something like that,
> the different architectural parts, etc. I've read the extension/embeding
> document, but that still provides a very interface like description.

I don't think there is such a description, yet - although the Python
books do cover writing extension modules. So, if you've understand how
to write a module, here is how you could proceed:
The dis module creates a 'disassembly' of the byte code, e.g.
Quote:
>>> import dis
>>> def f(i): return i+1
...
>>> dis.disco(f.func_code)

          0 SET_LINENO          1

          3 SET_LINENO          1
          6 LOAD_FAST           0 (i)
          9 LOAD_CONST          1 (1)
         12 BINARY_ADD    
         13 RETURN_VALUE  
         14 LOAD_CONST          0 (None)
         17 RETURN_VALUE  

This should give you an idea of the byte code features. In this
particular example, the interpreter pushes a local variable on the
stack (load_fast 0), then pushes a constant on the stack (1), then
returns with top-of-stack (i+1). The following 'return None' never
gets executed.
Next, you could have a look at the main loop (Python/ceval.c) and take
a particular byte code instruction and see how it is implemented. This
will eventually get you to the code where something non-trivial
happens.

Another path would be to investigate the various attributes of class,
function, method, and instance objects. The only thing you have to
know is that they either have a __members__ or a __methods__ attribute
(some have both):

Quote:
>>> f.__members__

['__doc__', '__name__', 'func_code', 'func_defaults', 'func_doc', 'func_globals', 'func_name']
Quote:
>>> f.func_code.__members__

['co_argcount', 'co_code', 'co_consts', 'co_filename', 'co_flags', 'co_name', 'co_names', 'co_nlocals', 'co_varnames']
Quote:
>>> f.func_code.co_consts  

(None, 1)

You can clearly see a relationship to the above byte code.

Hope this helps,
Martin

P.S. I feel it is funny to have a disassembler when there is no assembler.



Wed, 08 Sep 1999 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Python as an intermediate lang for compiler/interpreter development

2. How to get at interpreter internals ?

3. Prolog interpreter internals

4. Python interpreter in python?

5. Any Python interpreter's written in Python?

6. Python Interpreter with multiple Python Programs

7. failed assert when importing a boost.python-created library from an embedded python interpreter

8. Please HELP - compiler internal error

9. Internal C-compiler in CW

10. Inline Assembly: INTERNAL COMPILER ERROR

11. ifc: *Compiler Internal Error*

12. Internal compiler error with g77 on linux

 

 
Powered by phpBB® Forum Software