Skip to content

Commit 0627215

Browse files
committed
BUILD_MAP is different pre 3.5
1 parent d0dc879 commit 0627215

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

uncompyle6/scanners/scanner3.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -315,17 +315,25 @@ def bound_map_from_inst(
315315
if count < 5:
316316
return None
317317

318-
collection_start = i - (count * 2)
319-
assert (count * 2) <= i
320-
321-
for j in range(collection_start, i, 2):
322-
if insts[j].opname not in ("LOAD_CONST",):
323-
return None
324-
if insts[j + 1].opname not in ("LOAD_CONST",):
325-
return None
326-
327-
collection_start = i - (2 * count)
328-
collection_enum = CONST_COLLECTIONS.index("CONST_MAP")
318+
if self.version >= (3, 5):
319+
# Newer Python BUILD_MAP argument's count is a
320+
# key and value pair so it is multiplied by two.
321+
collection_start = i - (count * 2)
322+
assert (count * 2) <= i
323+
324+
for j in range(collection_start, i, 2):
325+
if insts[j].opname not in ("LOAD_CONST",):
326+
return None
327+
if insts[j + 1].opname not in ("LOAD_CONST",):
328+
return None
329+
330+
collection_start = i - (2 * count)
331+
collection_enum = CONST_COLLECTIONS.index("CONST_MAP")
332+
# else: Older Python count is sum of all key and value pairs
333+
# Each pair is added individually like:
334+
# LOAD_CONST ("Max-Age")
335+
# LOAD_CONST ("max-age")
336+
# STORE_MAP
329337

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

0 commit comments

Comments
 (0)