Skip to content

Commit df69eac

Browse files
author
rocky
committed
Handle pyston and unkown bytecodes better
1 parent 5d2fc11 commit df69eac

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

xdis/codetype/code20.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# (C) Copyright 2017-2021, 2023-2024 by Rocky Bernstein
1+
# (C) Copyright 2017-2021, 2023-2025 by Rocky Bernstein
22
#
33
# This program is free software; you can redistribute it and/or
44
# modify it under the terms of the GNU General Public License
@@ -32,6 +32,9 @@
3232
)
3333
# co_firstlineno added since 1.x
3434

35+
# Early pyston 2.7 Code objects seem to be a subset of 2.0 code.
36+
# The fields it has are:
37+
# co_argcount, co_filename, co_firstline, co_flag, co_name, co_varnames
3538

3639
class Code2(Code15):
3740
"""Class for a Python2 code object used when a Python 3 interpreter is

xdis/load.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,6 @@ def load_module_from_file_object(
222222
# FIXME: use the internal routine below
223223
tuple_version = magic_int2tuple(magic_int)
224224
except KeyError:
225-
if magic_int in (2657, 22138):
226-
raise ImportError("This smells like Pyston which is not supported.")
227-
228225
if len(magic) >= 2:
229226
raise ImportError(
230227
"Unknown magic number %s in %s"
@@ -233,6 +230,10 @@ def load_module_from_file_object(
233230
else:
234231
raise ImportError(f"Bad magic number: '{magic}'")
235232

233+
if magic_int in (2657, 22138):
234+
version = magicint2version.get(magic_int, "")
235+
raise ImportError(f"Magic int {magic_int} ({version}) is not supported.")
236+
236237
if magic_int in (
237238
3010,
238239
3020,
@@ -300,7 +301,11 @@ def load_module_from_file_object(
300301
source_size = unpack("<I", fp.read(4))[0] # size mod 2**32
301302
pass
302303
else:
303-
timestamp = unpack("<I", ts)[0]
304+
305+
# Early Pyston targeting 2.7 doesn't seem to have a timestamp!
306+
if magic_int not in (2657,):
307+
timestamp = unpack("<I", ts)[0]
308+
304309
# Note: a higher magic number doesn't necessarily mean a later
305310
# release. At Python 3.0 the magic number decreased
306311
# significantly. Hence, the range below. Also note inclusion of
@@ -310,7 +315,7 @@ def load_module_from_file_object(
310315
if (
311316
(3200 <= magic_int < 20121)
312317
and version >= (1, 5)
313-
or magic_int in PYPY3_MAGICS
318+
or magic_int in list(PYPY3_MAGICS) + [2657]
314319
):
315320
source_size = unpack("<I", fp.read(4))[0] # size mod 2**32
316321

0 commit comments

Comments
 (0)