Jump to content

how do I read a python compiled file?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
17 replies to this topic

#1
Guest_x42_*

Guest_x42_*
  • Guests
I have an idea that will change how open source is done (I am not sure if someone already thought of this though), but I need to be able to write an interpreter that can execute python compiled files (you know the .pyc files?):pinguin:

#2
CatatonicMan

CatatonicMan

    Newbie

  • Members
  • PipPip
  • 13 posts
The interpreter already can (and does) execute python bytecode (the .pyc files). As far as I know, (almost?) all Python code requires an interpreter to run.

Are you referring to decompiling the bytecode to get a human-readable version (.pyc to .py)?

#3
manux

manux

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 234 posts
You can use the dis module to "disassemble" pyc bytecode.(it will basically output a readable format of the tokens in the pyc).
You can write a virtual machine for that though I would recommend just downloading Python's source and copying/reading it to understand how it works.
And please tell us more about your idea ;)

#4
Guest_x42_*

Guest_x42_*
  • Guests
no I am talking about making a program that will use the compiled code so as to add on more possible features for a program, without changing the source code

#5
Guest_x42_*

Guest_x42_*
  • Guests
thanks for the answer, but I don't want to read it, I want to make a function that can interpret python, and I want it to work quickly so I decided to go with python compiled files which I think might be quicker for a program to interpret

#6
manux

manux

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 234 posts
well let's say you have a module named foo.py
def bar():

    return 12
Importing foo from another module will create a file called foo.pyc which is the actual bytecode.
If you know the functions defined in foo, here we have bar, then you can use it normally:
import foo

print foo.bar()#prints 12
If you don't, you can always use dir(foo) to list the attributes of the module, i.e. it's methods/classes/variables, but you'll have to guess by the name, or by trial and error, which is which.

You don't need a function that interprets a .pyc, you just need python to import the module.

Edit: By the way, if you want to use python from C/C++ you can use the Python/C API
You might also want to read:
Extending and Embedding the Python Interpreter - Python v2.6.5 documentation

#7
Guest_x42_*

Guest_x42_*
  • Guests
something that you are going to need to know when answering my questions: I don't care If I don't need to do it, I will do it just for the sake of knowing how to do it, and I have an idea that will make open source much simpler

#8
manux

manux

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 234 posts
Well, I'm happy to hear that you have positive plans for the open source domain.

If you want to do it* for the sake of knowing how to do it, let me recommend you to download the CPython source code, and look into it, especially the dynamic import part, I don't know your level in computer programming, but if you have any questions regarding the way CPython implements the language, feel free to post in their mailing list, or here if you want, but we might not know the answers ;)
*"it" being reading compiled bytecode.

#9
Guest_x42_*

Guest_x42_*
  • Guests
ok so here is how it started, I was trying to make a video for youtube, but I needed to reverse a video clip, now this was when I still used windows, so I tried it on windows movie maker, but windows movie maker does not allow you to reverse clips, so I tried a bunch of different programs, most of them open source, but none of them had the ability to reverse clips, so I tried editing the source code, but it was too hard to read, so I got an idea: what if you could add special effects without changing the source code, maybe if you knew some of the mathematics behind special effects, and you knew python, you could write up a small script that takes a video object and changes it, like maybe by blurring all the images, or something, then use your python interpreter to compile it into the byte code of pyc files, and then put it in a certain directory that the video editor knows to go to to find new special effects, it would be in byte code because that can be interpreted faster, and is easier to make an interpreter for, this way no one ever has to go through all the trouble of figuring out the entire source code just to know how to add on to it

Edited by x42, 23 June 2010 - 02:48 PM.
typo


#10
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
And that is called an add-in, add on, or plug in (plugin). There are hundreds of plugin managers on the market, lots and lots of programs use it. Some let you write plugins in C or C++, some in a scripting language (like Python). Take Mozilla Firefox, for instance. And - since we are talking about video tools - Sony Vegas.

Just saying.
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#11
manux

manux

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 234 posts
The current problem with plugins, most of the time, is that there is no or few documentation to the API of the given program. Sometimes it's not even an real API, you have to figure things out by yourself by browsing the (undocumented) source :(

#12
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
Yes, that seems to apply to most software. Nobody wants do document anything, they just code.
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa