如何用Python trace()模块追踪乘法表执行并输出标注结果
How to Trace Your Multiplication Table Program with Python's
trace Module Got it, let's fix this up so you can actually trace your multiplication table code and get annotated results. The main issue with your current code is that you created a Trace object, but never told it to run your target function—so it wasn't actually tracking any execution!
Here's the corrected, working version of your code, with explanations:
import sys import trace # Initialize the tracer: set trace=1 to enable execution tracing, count=1 for coverage stats tracer = trace.Trace( ignoredirs=[sys.prefix, sys.exec_prefix], # Skip standard library directories trace=1, # Enable line-by-line execution tracing count=1 # Track line execution counts for coverage ) def print_multiples(n, high): for i in range(1, high+1): print(n * i, end=" ") print() def print_mult_table(high): """ Return a multiplication table with n rows and columns.""" for i in range(1, high+1): print_multiples(i, high) # Use the tracer to run your target function—this is the key step you missed! tracer.runfunc(print_mult_table, 7) # Get the results object and write annotated output results = tracer.results() # Write coverage stats and annotated source files to the current directory results.write_results(show_missing=True, coverdir=".")
What's Changed & Why:
tracer.runfunc(print_mult_table, 7): This tells the tracer to execute yourprint_mult_tablefunction with the argument7, and track every line of code it runs. Without this, your tracer was just sitting idle.trace=1: Enables line-by-line execution tracing, so the module logs which lines are executed when. If you only care about coverage (not the step-by-step flow), you can leave this as0, but setting it to1gives you the execution trace output.
What You'll Get:
- Real-time Execution Trace: When you run the script, you'll see output like this (showing each line as it's executed):
--- modulename: your_script, funcname: print_mult_table your_script.py(13): for i in range(1, high+1): your_script.py(14): print_multiples(i, high) --- modulename: your_script, funcname: print_multiples your_script.py(8): for i in range(1, high+1): your_script.py(9): print(n * i, end=" ") ... [continues for all lines of the multiplication table execution] - Annotated Source Files: After running, you'll find a new file named
your_script.cover(replaceyour_scriptwith your actual filename) in your working directory. This file annotates each line with how many times it was executed, and marks any lines that weren't run (though in this case, all lines will be executed). For example:
The numbers on the left are the execution counts for each line.def print_multiples(n, high): 7 for i in range(1, high+1): 49 print(n * i, end=" ") 7 print() def print_mult_table(high): """ Return a multiplication table with n rows and columns.""" 7 for i in range(1, high+1): 7 print_multiples(i, high) 1 print_mult_table(7)
Bonus: If You Want Detailed Step-by-Step Annotations
If you want even more detailed output (like variable values), you might need to use a custom trace function with sys.settrace, but the trace module's built-in functionality is perfect for basic execution tracing and coverage annotation.
内容的提问来源于stack exchange,提问作者NationalRex22
相关产品推荐
相关产品推荐

