I was a little bored today and I started to google how the Python Virtual Machine works. I found a bunch of helpfull articles and kind of understand at least some of the very basic PVM code using the modules py_compile and dis. I was wondering how to write the actual PVM code to a file (without writing python code to a file and compiling it with py_compile) if i wanted to generate PVM code from another language - kind of like jython and the java interpreter. Ill probably just use it to implement a simple language like brainfsck but maybe branch out if I can/have the willpower. If anybody knows or at least can help point me in the right direction your help will be greatly apreciated.

Recommended Answers

All 4 Replies

You can disassemble Python byte code with module dis to something that looks like assembler code, but since there is no Python assembler it's just an analytical tool.

[...] but since there is no Python assembler it's just an analytical tool.

Some people might look at that and think opportunity! Google Summer of Code is coming up. Can daniweb.com be a mentoring organization?

One way to learn how to produce python byte code without python is to look in python's source code. See how the compile function is implemented in C, and it should give you a clue. Also I suppose that if you want to do the same from java, you should have a look in Jython's source code.
It doesn't solve the main question: is it useful to write python byte code directly instead of writing python code and compiling it ?
Another thing to consider is : what will you do if the syntax or semantics of python byte code changes with the next version of python ?

Thanks for your help and sorry if I did not make this clear but I need info on the .pyc files. I know they are binary and I need to know the structure of the files and the the byte codes for each instruction. The structure is somewhat explained in this article I just found but the auther is sounding quite confused himself: http://nedbatchelder.com/blog/200804/the_structure_of_pyc_files.html

Edit: Ill have to get a copy of python source code from python.org. Good idea to look into that. Hope I can understand enough of the c. thx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.