@@ -273,15 +273,15 @@ def get_instructions(x, *, first_line=None):
273273 the disassembled code object.
274274 """
275275 co = _get_code_object (x )
276+ cell_names = co .co_cellvars + co .co_freevars
276277 linestarts = dict (findlinestarts (co ))
277278 if first_line is not None :
278279 line_offset = first_line - co .co_firstlineno
279280 else :
280281 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 )
285285
286286def _get_const_info (const_index , const_list ):
287287 """Helper to get optional details about const references
@@ -295,16 +295,16 @@ def _get_const_info(const_index, const_list):
295295 argval = const_list [const_index ]
296296 return argval , repr (argval )
297297
298- def _get_name_info (name_index , get_name , ** extrainfo ):
298+ def _get_name_info (name_index , name_list ):
299299 """Helper to get optional details about named references
300300
301301 Returns the dereferenced name as both value and repr if the name
302302 list is defined.
303303 Otherwise returns the name index and its repr().
304304 """
305305 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 ]
308308 argrepr = argval
309309 else :
310310 argrepr = repr (argval )
@@ -336,10 +336,8 @@ def parse_exception_table(code):
336336 except StopIteration :
337337 return entries
338338
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 = ()):
343341 """Iterate over the instructions in a bytecode string.
344342
345343 Generates a sequence of Instruction namedtuples giving the details of each
@@ -348,7 +346,6 @@ def _get_instructions_bytes(code, varname_from_oparg=None,
348346 arguments.
349347
350348 """
351- get_name = None if names is None else names .__getitem__
352349 labels = set (findlabels (code ))
353350 for start , end , target , _ , _ in exception_entries :
354351 for i in range (start , end ):
@@ -371,18 +368,20 @@ def _get_instructions_bytes(code, varname_from_oparg=None,
371368 if op in hasconst :
372369 argval , argrepr = _get_const_info (arg , constants )
373370 elif op in hasname :
374- argval , argrepr = _get_name_info (arg , get_name )
371+ argval , argrepr = _get_name_info (arg , names )
375372 elif op in hasjabs :
376373 argval = arg * 2
377374 argrepr = "to " + repr (argval )
378375 elif op in hasjrel :
379376 argval = offset + 2 + arg * 2
380377 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 )
383380 elif op in hascompare :
384381 argval = cmp_op [arg ]
385382 argrepr = argval
383+ elif op in hasfree :
384+ argval , argrepr = _get_name_info (arg , cells )
386385 elif op == FORMAT_VALUE :
387386 argval , argrepr = FORMAT_VALUE_CONVERTERS [arg & 0x3 ]
388387 argval = (argval , bool (arg & 0x4 ))
@@ -399,11 +398,11 @@ def _get_instructions_bytes(code, varname_from_oparg=None,
399398
400399def disassemble (co , lasti = - 1 , * , file = None ):
401400 """Disassemble a code object."""
401+ cell_names = co .co_cellvars + co .co_freevars
402402 linestarts = dict (findlinestarts (co ))
403403 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 ,
407406 exception_entries = exception_entries )
408407
409408def _disassemble_recursive (co , * , file = None , depth = None ):
@@ -417,8 +416,8 @@ def _disassemble_recursive(co, *, file=None, depth=None):
417416 print ("Disassembly of %r:" % (x ,), file = file )
418417 _disassemble_recursive (x , file = file , depth = depth )
419418
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 ,
422421 * , file = None , line_offset = 0 , exception_entries = ()):
423422 # Omit the line number column entirely if we have no line number info
424423 show_lineno = bool (linestarts )
@@ -435,8 +434,8 @@ def _disassemble_bytes(code, lasti=-1, varname_from_oparg=None,
435434 offset_width = len (str (maxoffset ))
436435 else :
437436 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 ,
440439 line_offset = line_offset , exception_entries = exception_entries ):
441440 new_source_line = (show_lineno and
442441 instr .starts_line is not None and
@@ -518,16 +517,16 @@ def __init__(self, x, *, first_line=None, current_offset=None):
518517 else :
519518 self .first_line = first_line
520519 self ._line_offset = first_line - co .co_firstlineno
520+ self ._cell_names = co .co_cellvars + co .co_freevars
521521 self ._linestarts = dict (findlinestarts (co ))
522522 self ._original_object = x
523523 self .current_offset = current_offset
524524 self .exception_entries = parse_exception_table (co )
525525
526526 def __iter__ (self ):
527527 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 ,
531530 self ._linestarts ,
532531 line_offset = self ._line_offset ,
533532 exception_entries = self .exception_entries )
@@ -555,9 +554,9 @@ def dis(self):
555554 else :
556555 offset = - 1
557556 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 ,
560558 names = co .co_names , constants = co .co_consts ,
559+ cells = self ._cell_names ,
561560 linestarts = self ._linestarts ,
562561 line_offset = self ._line_offset ,
563562 file = output ,
0 commit comments