How do I use a pyc file created with an older version of Python?

Thomas Wouters thomas at xs4all.net
Wed Feb 14 10:35:59 EST 2001


On Mon, Feb 12, 2001 at 06:03:55PM +0100, Paul Moore wrote:

> This is probably a silly question, but I have an old pyc file
> generated under Python 1.5.? (I think), and I now use Python 2.0. I
> don't have the source easily accessible, unortunately, and I don't
> really want to install Python 1.5 as well, just for this one bit of
> code.

You'll have to do one of the latter two, unfortunately.

> Is it possible to convert a pyc from an older version of Python to a
> newer one, without the source? It seems like it should be possible, as
> a pyc is (I believe) just a magic number plus a marshalled code
> object. Assuming the code itself is compatible, could I just "fix" the
> magic number? Or would I need to "upgrade" the marshalled code
> somehow? Or is this not possible, and I just have to get the source
> back...?

A .pyc file is indeed 'just' a magic number and a marshalled code object.
However, the magic number is there for a reason: the actual bytecode
contained in the marshalled object *changed meaning*. For instance, in the
1.5.2 -> 2.0 change, the 'IMPORT_FROM' bytecode was split into 'IMPORT_FROM'
and 'IMPORT_STAR'. 'IMPORT_FROM' no longer handles the special case of the
"*" name, and if you try to run Python 1.5 (or older) bytecode that uses
'IMPORT_FROM' with a name of "*", you'll probably get a system error. Or
worse, it doesn't raise an error, but it doesn't import any of the desired
names either :)

There are more problems, though (before you think you're safe because the
code doesn't do 'from foo import *' :) between 1.5 and 2.0, the
UNPACK_TUPLE, UNPACK_LIST, UNPACK_ARG and UNPACK_VARARG opcodes were merged
into a single UNPACK_SEQUENCE opcode. UNPACK_TUPLE will still work, since it
has the same number and functionality as the 'new' UNPACK_SEQUENCE opcode,
but the other ones will raise a system error. Except for UNPACK_VARARG,
which is now DUP_TOPX, and would cause a lot of confusion :-) Several other
opcodes were removed as well.

So, bottom line, you're out of luck.

-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list