@@ -11,6 +11,8 @@ A Python debugging library for detailed code execution tracing. This library pro
1111
1212- ** Line-by-line execution tracing** : See exactly which lines are being executed
1313- ** Function/method call tracing** : Trace only function and method calls without line details
14+ - ** Return event tracing** : Track function return values and completion
15+ - ** Exception event tracing** : Monitor exception raising and handling
1416- ** Variable value inspection** : View the values of variables at each execution step
1517- ** Module filtering** : Trace only specific modules or all modules
1618- ** Context manager support** : Use with ` with ` statements for automatic cleanup
@@ -119,20 +121,87 @@ This will output:
119121```
120122__main__:15: my_function()
121123 args: x=10, y=20
124+ __main__:17: my_function() -> 30
125+ ```
126+
127+ ### Return Event Tracing
128+
129+ ``` python
130+ from spewer import SpewContext
131+
132+ # Trace function calls and returns
133+ with SpewContext(functions_only = True , show_values = True , trace_returns = True ):
134+ def calculate (x , y ):
135+ return x * y + 10
136+
137+ result = calculate(5 , 3 )
138+ ```
139+
140+ This will output:
141+ ```
142+ __main__:15: calculate()
143+ args: x=5, y=3
144+ __main__:16: calculate() -> 25
145+ ```
146+
147+ ### Exception Event Tracing
148+
149+ ``` python
150+ from spewer import SpewContext
151+
152+ # Trace function calls and exceptions
153+ with SpewContext(functions_only = True , show_values = True , trace_exceptions = True ):
154+ def risky_function (x , y ):
155+ if y == 0 :
156+ raise ValueError (" Cannot divide by zero" )
157+ return x / y
158+
159+ try :
160+ result = risky_function(10 , 0 )
161+ except ValueError as e:
162+ print (f " Caught: { e} " )
163+ ```
164+
165+ This will output:
166+ ```
167+ __main__:15: risky_function()
168+ args: x=10, y=0
169+ __main__:16: risky_function() -> ValueError('Cannot divide by zero')
170+ ```
171+
172+ ### Disable Specific Event Types
173+
174+ ``` python
175+ from spewer import SpewContext
176+
177+ # Only trace function calls, not returns or exceptions
178+ with SpewContext(functions_only = True , trace_returns = False , trace_exceptions = False ):
179+ def my_function ():
180+ return 42
181+
182+ result = my_function()
183+ ```
184+
185+ This will output:
186+ ```
187+ __main__:15: my_function()
188+ # No return event traced
122189```
123190
124191## API Reference
125192
126193### Functions
127194
128- #### ` spew(trace_names=None, show_values=False, functions_only=False) `
195+ #### ` spew(trace_names=None, show_values=False, functions_only=False, trace_returns=False, trace_exceptions=False ) `
129196
130197Install a trace hook which writes detailed logs about code execution.
131198
132199** Parameters:**
133200- ` trace_names ` (Optional[ List[ str]] ): List of module names to trace. If None, traces all modules.
134- - ` show_values ` (bool): Whether to show variable values during tracing.
135- - ` functions_only ` (bool): Whether to trace only function/method calls instead of line-by-line execution.
201+ - ` show_values ` (bool): Whether to show variable values during tracing. Default: False.
202+ - ` functions_only ` (bool): Whether to trace only function/method calls instead of line-by-line execution. Default: False.
203+ - ` trace_returns ` (bool): Whether to trace function return events. Default: False.
204+ - ` trace_exceptions ` (bool): Whether to trace exception events. Default: False.
136205
137206#### ` unspew() `
138207
@@ -142,23 +211,27 @@ Remove the trace hook installed by `spew()`.
142211
143212
144213
145- #### ` SpewContext(trace_names=None, show_values=False, functions_only=False) `
214+ #### ` SpewContext(trace_names=None, show_values=False, functions_only=False, trace_returns=False, trace_exceptions=False ) `
146215
147216Context manager for automatic spew/unspew operations.
148217
149218** Parameters:**
150219- ` trace_names ` (Optional[ List[ str]] ): List of module names to trace. If None, traces all modules.
151- - ` show_values ` (bool): Whether to show variable values during tracing.
152- - ` functions_only ` (bool): Whether to trace only function/method calls instead of line-by-line execution.
220+ - ` show_values ` (bool): Whether to show variable values during tracing. Default: False.
221+ - ` functions_only ` (bool): Whether to trace only function/method calls instead of line-by-line execution. Default: False.
222+ - ` trace_returns ` (bool): Whether to trace function return events. Default: False.
223+ - ` trace_exceptions ` (bool): Whether to trace exception events. Default: False.
153224
154- #### ` SpewConfig(trace_names=None, show_values=True, functions_only=False) `
225+ #### ` SpewConfig(trace_names=None, show_values=True, functions_only=False, trace_returns=False, trace_exceptions=False ) `
155226
156227Configuration class for spewer debugging. Provides validation and centralized configuration management.
157228
158229** Parameters:**
159230- ` trace_names ` (Optional[ List[ str]] ): List of module names to trace. If None, traces all modules.
160- - ` show_values ` (bool): Whether to show variable values during tracing.
161- - ` functions_only ` (bool): Whether to trace only function/method calls instead of line-by-line execution.
231+ - ` show_values ` (bool): Whether to show variable values during tracing. Default: True.
232+ - ` functions_only ` (bool): Whether to trace only function/method calls instead of line-by-line execution. Default: False.
233+ - ` trace_returns ` (bool): Whether to trace function return events. Default: False.
234+ - ` trace_exceptions ` (bool): Whether to trace exception events. Default: False.
162235
163236#### ` TraceHook(config) `
164237
@@ -169,6 +242,8 @@ Core trace hook implementation. This is the low-level class that handles the act
169242
170243## Example Output
171244
245+ ### Line-by-Line Tracing
246+
172247When tracing with ` show_values=True ` , you'll see output like:
173248
174249```
@@ -182,6 +257,20 @@ __main__:18: print(f"Result: {result}")
182257 result=30
183258```
184259
260+ ### Function-Only Tracing with Returns and Exceptions
261+
262+ When tracing with ` functions_only=True, show_values=True, trace_returns=True, trace_exceptions=True ` :
263+
264+ ```
265+ __main__:15: calculate()
266+ args: x=10, y=5
267+ __main__:16: calculate() -> 15
268+
269+ __main__:20: risky_function()
270+ args: x=10, y=0
271+ __main__:22: risky_function() -> ValueError('division by zero')
272+ ```
273+
185274## Notes
186275
187276- The library uses Python's ` sys.settrace() ` which can impact performance
0 commit comments