@@ -72,7 +72,7 @@ def dis(
7272 errmsg ,
7373 x = None ,
7474 start_line = - 1 ,
75- end_line = None ,
75+ end_line = - 1 ,
7676 relative_pos = False ,
7777 style = "none" ,
7878 start_offset = 0 ,
@@ -197,7 +197,7 @@ def dis_from_file(
197197 errmsg ,
198198 filename ,
199199 start_line = - 1 ,
200- end_line = None ,
200+ end_line = - 1 ,
201201 style = "none" ,
202202 include_header = False ,
203203 asm_format = "extended" ,
@@ -212,8 +212,9 @@ def dis_from_file(
212212 msg (f"Starting line { start_line } not found; adjusting up to { new_start } " )
213213 start_line = new_start
214214
215- code_object = next ((k for k , val in linecache_info .code_map .items () if val == filename ), None )
215+ code_object = next ((value for value in linecache_info .code_map .values () if value . co_filename == filename ), None )
216216 if code_object is not None :
217+
217218 dis (msg , msg_nocr , section , errmsg ,
218219 x = code_object ,
219220 start_line = start_line ,
@@ -433,14 +434,13 @@ def null_print(_):
433434
434435 labels = findlabels (code , opc )
435436
436- if start_line > cur_line :
437+ if start_line > cur_line or start_offset > 0 :
437438 msg_nocr = null_print
438439 msg = null_print
439440 else :
440441 msg_nocr = orig_msg_nocr
441442 msg = orig_msg
442443
443-
444444 offset = - 1
445445 for instr in get_instructions_bytes (code , opc ):
446446 instructions .append (instr )
@@ -493,93 +493,6 @@ def null_print(_):
493493
494494 return code , offset
495495
496- def disassemble_instruction (
497- orig_msg : Callable ,
498- orig_msg_nocr : Callable ,
499- code ,
500- offset ,
501- lasti = - 1 ,
502- cur_line = 0 ,
503- end_line = None ,
504- relative_pos = False ,
505- varnames = (),
506- names = (),
507- constants = (),
508- cells = (),
509- line_starts = {},
510- style = "none" ,
511- opc = PYTHON_OPCODES ,
512- asm_format = "extended" ,
513- ) -> tuple :
514- """Disassemble byte string of code. If end_line is negative
515- it counts the number of statement line starts to use."""
516-
517- def null_print (_ ):
518- return None
519-
520- instructions = []
521- if end_line is None :
522- end_line = 10000
523- elif relative_pos :
524- end_line += start_line - 1
525- pass
526-
527- labels = findlabels (code , opc )
528-
529- if start_line > cur_line :
530- msg_nocr = null_print
531- msg = null_print
532- else :
533- msg_nocr = orig_msg_nocr
534- msg = orig_msg
535-
536- offset = - 1
537- for instr in get_instructions_bytes (
538- code , opc , varnames , names , constants , cells , line_starts , labels
539- ):
540- instructions .append (instr )
541-
542- offset = instr .offset
543-
544- if end_offset and offset > end_offset :
545- break
546-
547- # Python 3.11 introduces "CACHE" and the convention seems to be
548- # to not print these normally.
549- if instr .opname == "CACHE" and asm_format not in (
550- "extended_bytes" ,
551- "bytes" ,
552- ):
553- continue
554-
555- # Column: Source code line number
556- if instr .starts_line :
557- cur_line = instr .starts_line
558- if start_line and (
559- (start_line > cur_line ) or start_offset and start_offset > offset
560- ):
561- continue
562-
563- if (cur_line > end_line ) or (end_offset and offset > end_offset ):
564- break
565- elif start_offset and offset and start_offset <= offset :
566- continue
567-
568- print_instruction (
569- instr ,
570- instructions ,
571- msg ,
572- msg_nocr ,
573- labels = labels ,
574- lasti = lasti ,
575- line_starts = line_starts ,
576- style = style ,
577- opc = opc ,
578- asm_format = asm_format ,
579- )
580-
581- return code , offset
582-
583496# Demo it
584497if __name__ == "__main__" :
585498
@@ -604,11 +517,14 @@ def fib(x):
604517 return 1
605518 return fib (x - 1 ) + fib (x - 2 )
606519
607- dis_from_file (msg , msg_nocr , section , errmsg , __file__ , start_line = 45 )
608- # curframe = inspect.currentframe()
609- # dis(msg, msg_nocr, errmsg, section, curframe,
610- # start_line=10, end_line=40, highlight='dark')
520+ # dis_from_file(msg, msg_nocr, section, errmsg, __file__, start_line=45)
611521 # print('-' * 40)
522+
523+ curframe = inspect .currentframe ()
524+ dis (msg , msg_nocr , errmsg , section , curframe , start_offset = 4 , end_offset = 10 )
525+ print ('-' * 40 )
526+
527+ import sys ; sys .exit (0 )
612528 # does nothing because start_offset is too high:
613529 # dis(msg, msg_nocr, errmsg, section, curframe,
614530 # start_offset=10, end_offset=20, highlight='dark')
0 commit comments