@@ -279,7 +279,7 @@ def get_instructions(x, *, first_line=None):
279
279
else :
280
280
line_offset = 0
281
281
return _get_instructions_bytes (co .co_code ,
282
- co ._get_localsplusnames (), co . co_nlocals ,
282
+ co ._varname_from_oparg ,
283
283
co .co_names , co .co_consts ,
284
284
linestarts , line_offset )
285
285
@@ -295,16 +295,16 @@ def _get_const_info(const_index, const_list):
295
295
argval = const_list [const_index ]
296
296
return argval , repr (argval )
297
297
298
- def _get_name_info (name_index , name_list ):
298
+ def _get_name_info (name_index , get_name , ** extrainfo ):
299
299
"""Helper to get optional details about named references
300
300
301
301
Returns the dereferenced name as both value and repr if the name
302
302
list is defined.
303
303
Otherwise returns the name index and its repr().
304
304
"""
305
305
argval = name_index
306
- if name_list is not None :
307
- argval = name_list [ name_index ]
306
+ if get_name is not None :
307
+ argval = get_name ( name_index , ** extrainfo )
308
308
argrepr = argval
309
309
else :
310
310
argrepr = repr (argval )
@@ -336,7 +336,7 @@ def parse_exception_table(code):
336
336
except StopIteration :
337
337
return entries
338
338
339
- def _get_instructions_bytes (code , localsplusnames = None , nlocals = 0 ,
339
+ def _get_instructions_bytes (code , varname_from_oparg = None ,
340
340
names = None , constants = None ,
341
341
linestarts = None , line_offset = 0 ,
342
342
exception_entries = ()):
@@ -348,6 +348,7 @@ def _get_instructions_bytes(code, localsplusnames=None, nlocals=0,
348
348
arguments.
349
349
350
350
"""
351
+ get_name = None if names is None else names .__getitem__
351
352
labels = set (findlabels (code ))
352
353
for start , end , target , _ , _ in exception_entries :
353
354
for i in range (start , end ):
@@ -370,20 +371,21 @@ def _get_instructions_bytes(code, localsplusnames=None, nlocals=0,
370
371
if op in hasconst :
371
372
argval , argrepr = _get_const_info (arg , constants )
372
373
elif op in hasname :
373
- argval , argrepr = _get_name_info (arg , names )
374
+ argval , argrepr = _get_name_info (arg , get_name )
374
375
elif op in hasjabs :
375
376
argval = arg * 2
376
377
argrepr = "to " + repr (argval )
377
378
elif op in hasjrel :
378
379
argval = offset + 2 + arg * 2
379
380
argrepr = "to " + repr (argval )
380
381
elif op in haslocal :
381
- argval , argrepr = _get_name_info (arg , localsplusnames )
382
+ argval , argrepr = _get_name_info (arg , varname_from_oparg )
382
383
elif op in hascompare :
383
384
argval = cmp_op [arg ]
384
385
argrepr = argval
385
386
elif op in hasfree :
386
- argval , argrepr = _get_name_info (arg + nlocals , localsplusnames )
387
+ argval , argrepr = _get_name_info (arg , varname_from_oparg ,
388
+ cell = True )
387
389
elif op == FORMAT_VALUE :
388
390
argval , argrepr = FORMAT_VALUE_CONVERTERS [arg & 0x3 ]
389
391
argval = (argval , bool (arg & 0x4 ))
@@ -403,7 +405,7 @@ def disassemble(co, lasti=-1, *, file=None):
403
405
linestarts = dict (findlinestarts (co ))
404
406
exception_entries = parse_exception_table (co )
405
407
_disassemble_bytes (co .co_code , lasti ,
406
- co ._get_localsplusnames (), co . co_nlocals ,
408
+ co ._varname_from_oparg ,
407
409
co .co_names , co .co_consts , linestarts , file = file ,
408
410
exception_entries = exception_entries )
409
411
@@ -418,7 +420,7 @@ def _disassemble_recursive(co, *, file=None, depth=None):
418
420
print ("Disassembly of %r:" % (x ,), file = file )
419
421
_disassemble_recursive (x , file = file , depth = depth )
420
422
421
- def _disassemble_bytes (code , lasti = - 1 , localsplusnames = None , nlocals = 0 ,
423
+ def _disassemble_bytes (code , lasti = - 1 , varname_from_oparg = None ,
422
424
names = None , constants = None , linestarts = None ,
423
425
* , file = None , line_offset = 0 , exception_entries = ()):
424
426
# Omit the line number column entirely if we have no line number info
@@ -436,7 +438,7 @@ def _disassemble_bytes(code, lasti=-1, localsplusnames=None, nlocals=0,
436
438
offset_width = len (str (maxoffset ))
437
439
else :
438
440
offset_width = 4
439
- for instr in _get_instructions_bytes (code , localsplusnames , nlocals , names ,
441
+ for instr in _get_instructions_bytes (code , varname_from_oparg , names ,
440
442
constants , linestarts ,
441
443
line_offset = line_offset , exception_entries = exception_entries ):
442
444
new_source_line = (show_lineno and
@@ -527,7 +529,7 @@ def __init__(self, x, *, first_line=None, current_offset=None):
527
529
def __iter__ (self ):
528
530
co = self .codeobj
529
531
return _get_instructions_bytes (co .co_code ,
530
- co ._get_localsplusnames (), co . co_nlocals ,
532
+ co ._varname_from_oparg ,
531
533
co .co_names , co .co_consts ,
532
534
self ._linestarts ,
533
535
line_offset = self ._line_offset ,
@@ -557,8 +559,7 @@ def dis(self):
557
559
offset = - 1
558
560
with io .StringIO () as output :
559
561
_disassemble_bytes (co .co_code ,
560
- localsplusnames = co ._get_localsplusnames (),
561
- nlocals = co .co_nlocals ,
562
+ varname_from_oparg = co ._varname_from_oparg ,
562
563
names = co .co_names , constants = co .co_consts ,
563
564
linestarts = self ._linestarts ,
564
565
line_offset = self ._line_offset ,
0 commit comments