Skip to content

Fix: Enable tracing of built-in functions when functions_only=True#10

Merged
Agent-Hellboy merged 3 commits intoAgent-Hellboy:mainfrom
kunal534:main
Oct 22, 2025
Merged

Fix: Enable tracing of built-in functions when functions_only=True#10
Agent-Hellboy merged 3 commits intoAgent-Hellboy:mainfrom
kunal534:main

Conversation

@kunal534
Copy link
Copy Markdown

Problem

Built-in functions (like len(), print(), max()) were not being traced when functions_only=True was enabled.

The issue occurred because sys.settrace() does not generate c_call events for C/built-in functions by default.

Solution

  • Switched to sys.setprofile() when functions_only=True to capture built-in function calls
  • Updated TraceHook.__call__() to handle both call and c_call events
  • Modified _handle_function_call() to process built-in functions separately
  • Updated unspew() to clear both settrace and setprofile

Changes Made

Files modified:

  • spewer/spewer.py: Use sys.setprofile() for functions_only mode
  • spewer/trace.py: Handle c_call events for built-in functions
  • tests/test_builtin_tracing.py: Added comprehensive test coverage
  • tests/test_spewer.py: Updated existing tests for new method signatures

Testing

✅ All 25 tests pass

  • Added 2 new test cases specifically for built-in function tracing
  • Verified tracing works for len(), max(), min(), print()
  • Confirmed both user-defined and built-in functions are traced together

Example Usage

from spewer import spew, unspew
spew(functions_only=True)
result = len() # Now traced!​
print(result) # Now traced!
unspew()

Output:
builtins: len()
builtins: print()
3

This resolves the issue where built-in functions were silently ignored during tracing.

- Switch to sys.setprofile() for functions_only mode to capture c_call events
- Update TraceHook to handle both 'call' and 'c_call' events
- Add _handle_function_call support for built-in C functions
- Add comprehensive test coverage for built-in function tracing
- Update existing tests to match new method signatures

Built-in functions like len(), print(), max() are now properly traced.
@codecov
Copy link
Copy Markdown

codecov Bot commented Oct 21, 2025

Codecov Report

❌ Patch coverage is 66.66667% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.80%. Comparing base (4164ce4) to head (2759642).
⚠️ Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
spewer/trace.py 44.44% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #10      +/-   ##
==========================================
- Coverage   96.49%   92.80%   -3.70%     
==========================================
  Files           4        4              
  Lines         114      125      +11     
==========================================
+ Hits          110      116       +6     
- Misses          4        9       +5     
Flag Coverage Δ
unittests 92.80% <66.66%> (-3.70%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Agent-Hellboy
Copy link
Copy Markdown
Owner

Hi @kunal534 , Thanks for the PR, please check CI failure, could you please attach a screen of using this library tracking the built in functions.

@Agent-Hellboy
Copy link
Copy Markdown
Owner

One more thing could you please add a new PR removing 3.9 from CI and adding 3.14

@Agent-Hellboy
Copy link
Copy Markdown
Owner

It would be great help if you complete #8 as well.

Kunal added 2 commits October 22, 2025 00:05
- Implemented c_call event handling with sys.setprofile()
- Added 17 comprehensive tests for built-in tracing
- Fixed all linting issues
- Removed Python 3.9 from CI workflow as requested
- Coverage: 93% overall, 89% on trace.py
@kunal534
Copy link
Copy Markdown
Author

Hi @Agent-Hellboy,

📸 Screenshot: Built-in Function Tracing

image

The feature successfully traces built-in functions (len(), max(), min(), print()) when functions_only=True is enabled.

✅ Updates Made

I've pushed 2 commits addressing your feedback:

  1. Removed Python 3.9 from CI workflow as requested
  2. Implemented built-in function tracing with:
    • c_call event handling via sys.setprofile()
    • 17 comprehensive tests
    • All linting checks passing
    • 93% overall coverage (89% on trace.py)

📋 Coverage Note

Lines 36-40 in trace.py show as uncovered in the report, but they execute correctly (as demonstrated in the screenshot above). This is a known limitation of coverage.py with sys.setprofile() callbacks.

🔄 Issue #8

I'll tackle issue #8 in a separate PR to keep this one focused and avoid any potential conflicts.

The PR is ready for review once you approve the workflow run. Thank you!

@Agent-Hellboy
Copy link
Copy Markdown
Owner

Thanks for the contribution @kunal534. Please check other issues as well.

@Agent-Hellboy Agent-Hellboy merged commit 7262bbb into Agent-Hellboy:main Oct 22, 2025
6 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants