Please anyone explain about python bytecode.I converted .pyc file to hex code by hexdump command in linux. But couldn't able to understand how hex code are arranged.Actually I building a python virtual machine in C.I don't know how read those hexcode? How to execute the .pyc file in C?
This is the source code
def add(a, b):
return a + b
add(2,3)
this the hexdump output of .pyc file
0000000 f303 0a0d 30b2 5936 0063 0000 0000 0000
0000010 0300 0000 4000 0000 7300 001a 0000 0064
0000020 8400 0000 005a 6500 0000 0164 6400 0002
0000030 0283 0100 0364 5300 0428 0000 6300 0002
0000040 0000 0002 0000 0002 0000 0043 0000 0873
0000050 0000 7c00 0000 017c 1700 2853 0001 0000
0000060 284e 0000 0000 0228 0000 7400 0001 0000
0000070 7461 0001 0000 2862 0000 0000 0028 0000
0000080 7300 0007 0000 6574 7473 702e 7479 0003
0000090 0000 6461 0364 0000 7300 0002 0000 0100
00000a0 0269 0000 6900 0003 0000 284e 0001 0000
00000b0 0252 0000 2800 0000 0000 0028 0000 2800
00000c0 0000 0000 0773 0000 7400 7365 2e74 7970
00000d0 0874 0000 3c00 6f6d 7564 656c 033e 0000
00000e0 7300 0002 0000 0309
00000e8
I know that this content from .pyc file contains a magic number, a time stamp and the pseudo-compiled code. What I am trying to create is a python byte code Interpreter. I have tried using the dis module and marshal module to unpack the .pyc file, and got a lot of information regarding how various things are working in the stack machine.
But is it possible to create a micro interpreter written purely in C language, without using the dis module, by going through the .pyc contents?