Skip to content

Commit

Permalink
BUILD_MAP is different pre 3.5
Browse files Browse the repository at this point in the history
  • Loading branch information
rocky committed Jul 13, 2024
1 parent d0dc879 commit 0627215
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions uncompyle6/scanners/scanner3.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,17 +315,25 @@ def bound_map_from_inst(
if count < 5:
return None

collection_start = i - (count * 2)
assert (count * 2) <= i

for j in range(collection_start, i, 2):
if insts[j].opname not in ("LOAD_CONST",):
return None
if insts[j + 1].opname not in ("LOAD_CONST",):
return None

collection_start = i - (2 * count)
collection_enum = CONST_COLLECTIONS.index("CONST_MAP")
if self.version >= (3, 5):
# Newer Python BUILD_MAP argument's count is a
# key and value pair so it is multiplied by two.
collection_start = i - (count * 2)
assert (count * 2) <= i

for j in range(collection_start, i, 2):
if insts[j].opname not in ("LOAD_CONST",):
return None
if insts[j + 1].opname not in ("LOAD_CONST",):
return None

collection_start = i - (2 * count)
collection_enum = CONST_COLLECTIONS.index("CONST_MAP")
# else: Older Python count is sum of all key and value pairs
# Each pair is added individually like:
# LOAD_CONST ("Max-Age")
# LOAD_CONST ("max-age")
# STORE_MAP

# If we get here, all instructions before tokens[i] are LOAD_CONST and
# we can replace add a boundary marker and change LOAD_CONST to
Expand Down Expand Up @@ -524,7 +532,7 @@ def ingest(
if try_tokens is not None:
new_tokens = try_tokens
continue
elif opname in ("BUILD_MAP",):
elif opname in ("BUILD_MAP",) and self.version >= (3, 5):
try_tokens = self.bound_map_from_inst(
self.insts,
new_tokens,
Expand Down

0 comments on commit 0627215

Please sign in to comment.