The main interpreter loop is in this function and we can call this function the virtual machine of Cpython. The main loop of this program begins at line number 1057 which is a giant switch case of all opcodes of python. The opcodes are declared in the file Include/opcode.h.

Let us write a simple python program

a = 100

b = 50

print a

print b

and save it in a file test.py.

Execute the command python -m dis test.py

The output is as follows:

1 0 LOAD_CONST 0 (100)
3 STORE_NAME 0 (a)

2 6 LOAD_CONST 1 (50)
9 STORE_NAME 1 (b)

3 12 LOAD_NAME 0 (a)
15 PRINT_ITEM
16 PRINT_NEWLINE

4 17 LOAD_NAME 1 (b)
20 PRINT_ITEM
21 PRINT_NEWLINE
22 LOAD_CONST 2 (None)
25 RETURN_VALUE

This is the textual representation of the bytecode of our program. Now here is a short video of the debugging of the interpreter code for the above bytecode.

You are now familiar with how the compiler and the interpreter of Python works congratulations.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s