6

To what extent is python bytecode compatible between releases. I'm not talking about python2.x to python3.x but say... Python33 to python34?

I am not after it for 'security' I use Cython to convert the bulk of a program to C, I do however use pyc file as a means to store some constants and pyc is preferable as it provides a file format that isn't easily changed unofficially. If someone wants something changed they can request via internal procedures

Such a pyc file only contains variables which are Int,float,list,dict,string in stf python. One class but it acts more as a container/struct.

Is this a big no or is this a try and see as some very basic python bytecode data is being stored

Naib
  • 999
  • 7
  • 20
  • Actually pyc is easily changed unofficially. – Antimony Mar 17 '14 at 15:48
  • that is true yes but at the end of the day *IF* an engineer wanted to change something its actually easier to just ask – Naib Mar 18 '14 at 16:12
  • That depends on how easy asking is. Generally when someone obfuscates their code, they don't want to change it. – Antimony Mar 18 '14 at 16:43
  • As mentioned, its not code but constants. CONSTANTS that they can view openly anyway (the pyc/py is auto generated from vhdl but that's getting into specifics) and its meant to be read-only as it is the reflection of a specific FPGA release – Naib Mar 19 '14 at 08:27

1 Answers1

8

Python makes no guarantee about bytecode compatibility between versions. Don't rely on it.

In fact, a pyc file starts with a magic number that changes every time the marshalling code does, and python checks this number for compatibility. Since this code changes pretty much every version, so does the magic number. See Ned Batchelder's blog entry for details.

There are better ways of ensuring your files haven't been tampered with: checksums, for example.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Thankyou for the reply that makes sense. To be clear this isn't to prevent tampering & the concept of checksum is being used in the scripting & tampering. The deployment of pyc as oppose to py is because the application is stored in a RO location and the pyc are stored in a RO location (importlib being used). Why different... software process calls for it (Dimentions backend). if py were deployed Python would try to gen bytecode BUT it will not have write access – Naib Mar 18 '14 at 16:11
  • 1
    Generating bytecode is completely optional in Python: it won't complain if it can't write it, but you can turn it off completely with the -B option or the PYTHONDONTWRITEBYTECODE environment var, see [the documentation](http://docs.python.org/2/using/cmdline.html#cmdoption-B). – Daniel Roseman Mar 18 '14 at 16:13
  • ahhh, I forgot about that. that might simplify things. – Naib Mar 18 '14 at 16:18