@@ -273,15 +273,15 @@ def get_instructions(x, *, first_line=None):
273
273
the disassembled code object.
274
274
"""
275
275
co = _get_code_object (x )
276
+ cell_names = co .co_cellvars + co .co_freevars
276
277
linestarts = dict (findlinestarts (co ))
277
278
if first_line is not None :
278
279
line_offset = first_line - co .co_firstlineno
279
280
else :
280
281
line_offset = 0
281
- return _get_instructions_bytes (co .co_code ,
282
- co ._varname_from_oparg ,
283
- co .co_names , co .co_consts ,
284
- linestarts , line_offset )
282
+ return _get_instructions_bytes (co .co_code , co .co_varnames , co .co_names ,
283
+ co .co_consts , cell_names , linestarts ,
284
+ line_offset )
285
285
286
286
def _get_const_info (const_index , const_list ):
287
287
"""Helper to get optional details about const references
@@ -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 , get_name , ** extrainfo ):
298
+ def _get_name_info (name_index , name_list ):
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 get_name is not None :
307
- argval = get_name ( name_index , ** extrainfo )
306
+ if name_list is not None :
307
+ argval = name_list [ name_index ]
308
308
argrepr = argval
309
309
else :
310
310
argrepr = repr (argval )
@@ -336,10 +336,8 @@ def parse_exception_table(code):
336
336
except StopIteration :
337
337
return entries
338
338
339
- def _get_instructions_bytes (code , varname_from_oparg = None ,
340
- names = None , constants = None ,
341
- linestarts = None , line_offset = 0 ,
342
- exception_entries = ()):
339
+ def _get_instructions_bytes (code , varnames = None , names = None , constants = None ,
340
+ cells = None , linestarts = None , line_offset = 0 , exception_entries = ()):
343
341
"""Iterate over the instructions in a bytecode string.
344
342
345
343
Generates a sequence of Instruction namedtuples giving the details of each
@@ -348,7 +346,6 @@ def _get_instructions_bytes(code, varname_from_oparg=None,
348
346
arguments.
349
347
350
348
"""
351
- get_name = None if names is None else names .__getitem__
352
349
labels = set (findlabels (code ))
353
350
for start , end , target , _ , _ in exception_entries :
354
351
for i in range (start , end ):
@@ -371,18 +368,20 @@ def _get_instructions_bytes(code, varname_from_oparg=None,
371
368
if op in hasconst :
372
369
argval , argrepr = _get_const_info (arg , constants )
373
370
elif op in hasname :
374
- argval , argrepr = _get_name_info (arg , get_name )
371
+ argval , argrepr = _get_name_info (arg , names )
375
372
elif op in hasjabs :
376
373
argval = arg * 2
377
374
argrepr = "to " + repr (argval )
378
375
elif op in hasjrel :
379
376
argval = offset + 2 + arg * 2
380
377
argrepr = "to " + repr (argval )
381
- elif op in haslocal or op in hasfree :
382
- argval , argrepr = _get_name_info (arg , varname_from_oparg )
378
+ elif op in haslocal :
379
+ argval , argrepr = _get_name_info (arg , varnames )
383
380
elif op in hascompare :
384
381
argval = cmp_op [arg ]
385
382
argrepr = argval
383
+ elif op in hasfree :
384
+ argval , argrepr = _get_name_info (arg , cells )
386
385
elif op == FORMAT_VALUE :
387
386
argval , argrepr = FORMAT_VALUE_CONVERTERS [arg & 0x3 ]
388
387
argval = (argval , bool (arg & 0x4 ))
@@ -399,11 +398,11 @@ def _get_instructions_bytes(code, varname_from_oparg=None,
399
398
400
399
def disassemble (co , lasti = - 1 , * , file = None ):
401
400
"""Disassemble a code object."""
401
+ cell_names = co .co_cellvars + co .co_freevars
402
402
linestarts = dict (findlinestarts (co ))
403
403
exception_entries = parse_exception_table (co )
404
- _disassemble_bytes (co .co_code , lasti ,
405
- co ._varname_from_oparg ,
406
- co .co_names , co .co_consts , linestarts , file = file ,
404
+ _disassemble_bytes (co .co_code , lasti , co .co_varnames , co .co_names ,
405
+ co .co_consts , cell_names , linestarts , file = file ,
407
406
exception_entries = exception_entries )
408
407
409
408
def _disassemble_recursive (co , * , file = None , depth = None ):
@@ -417,8 +416,8 @@ def _disassemble_recursive(co, *, file=None, depth=None):
417
416
print ("Disassembly of %r:" % (x ,), file = file )
418
417
_disassemble_recursive (x , file = file , depth = depth )
419
418
420
- def _disassemble_bytes (code , lasti = - 1 , varname_from_oparg = None ,
421
- names = None , constants = None , linestarts = None ,
419
+ def _disassemble_bytes (code , lasti = - 1 , varnames = None , names = None ,
420
+ constants = None , cells = None , linestarts = None ,
422
421
* , file = None , line_offset = 0 , exception_entries = ()):
423
422
# Omit the line number column entirely if we have no line number info
424
423
show_lineno = bool (linestarts )
@@ -435,8 +434,8 @@ def _disassemble_bytes(code, lasti=-1, varname_from_oparg=None,
435
434
offset_width = len (str (maxoffset ))
436
435
else :
437
436
offset_width = 4
438
- for instr in _get_instructions_bytes (code , varname_from_oparg , names ,
439
- constants , linestarts ,
437
+ for instr in _get_instructions_bytes (code , varnames , names ,
438
+ constants , cells , linestarts ,
440
439
line_offset = line_offset , exception_entries = exception_entries ):
441
440
new_source_line = (show_lineno and
442
441
instr .starts_line is not None and
@@ -518,16 +517,16 @@ def __init__(self, x, *, first_line=None, current_offset=None):
518
517
else :
519
518
self .first_line = first_line
520
519
self ._line_offset = first_line - co .co_firstlineno
520
+ self ._cell_names = co .co_cellvars + co .co_freevars
521
521
self ._linestarts = dict (findlinestarts (co ))
522
522
self ._original_object = x
523
523
self .current_offset = current_offset
524
524
self .exception_entries = parse_exception_table (co )
525
525
526
526
def __iter__ (self ):
527
527
co = self .codeobj
528
- return _get_instructions_bytes (co .co_code ,
529
- co ._varname_from_oparg ,
530
- co .co_names , co .co_consts ,
528
+ return _get_instructions_bytes (co .co_code , co .co_varnames , co .co_names ,
529
+ co .co_consts , self ._cell_names ,
531
530
self ._linestarts ,
532
531
line_offset = self ._line_offset ,
533
532
exception_entries = self .exception_entries )
@@ -555,9 +554,9 @@ def dis(self):
555
554
else :
556
555
offset = - 1
557
556
with io .StringIO () as output :
558
- _disassemble_bytes (co .co_code ,
559
- varname_from_oparg = co ._varname_from_oparg ,
557
+ _disassemble_bytes (co .co_code , varnames = co .co_varnames ,
560
558
names = co .co_names , constants = co .co_consts ,
559
+ cells = self ._cell_names ,
561
560
linestarts = self ._linestarts ,
562
561
line_offset = self ._line_offset ,
563
562
file = output ,
0 commit comments