@@ -315,17 +315,25 @@ def bound_map_from_inst(
315
315
if count < 5 :
316
316
return None
317
317
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
329
337
330
338
# If we get here, all instructions before tokens[i] are LOAD_CONST and
331
339
# we can replace add a boundary marker and change LOAD_CONST to
@@ -524,7 +532,7 @@ def ingest(
524
532
if try_tokens is not None :
525
533
new_tokens = try_tokens
526
534
continue
527
- elif opname in ("BUILD_MAP" ,):
535
+ elif opname in ("BUILD_MAP" ,) and self . version >= ( 3 , 5 ) :
528
536
try_tokens = self .bound_map_from_inst (
529
537
self .insts ,
530
538
new_tokens ,
0 commit comments