Skip to content

Overhauled redirection and output capturing#1654

Merged
kmvanbrunt merged 18 commits intomainfrom
redirection_fixes
Apr 29, 2026
Merged

Overhauled redirection and output capturing#1654
kmvanbrunt merged 18 commits intomainfrom
redirection_fixes

Conversation

@kmvanbrunt
Copy link
Copy Markdown
Member

cmd2 redirection now only captures output directed to self.stdout (e.g., via self.poutput()). Standard print() calls write directly to sys.stdout and are not captured. However, print() calls within pyscripts and the interactive Python shell are treated as command output and sent to self.stdout, allowing them to be captured.

This means we no longer alter the system-wide sys.stdout stream.

To facilitate this change, our custom argparse code now writes all output to self.stdout instead of sys.stdout.

To support pyscript developers who don't have access to self.stdout, the print() function in that environment has been replaced with one that wraps self.print_to(). It writes to self.stdout by default and respects the allow_style setting. It also supports printing Rich objects. This means that run_pyscript script.py > out.txt will capture any print() calls in the script.

@kmvanbrunt kmvanbrunt requested a review from tleonhardt as a code owner April 29, 2026 00:42
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 29, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.49%. Comparing base (a3890a7) to head (770b819).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1654      +/-   ##
==========================================
- Coverage   99.49%   99.49%   -0.01%     
==========================================
  Files          21       21              
  Lines        4741     4733       -8     
==========================================
- Hits         4717     4709       -8     
  Misses         24       24              
Flag Coverage Δ
unittests 99.49% <100.00%> (-0.01%) ⬇️

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.

Comment thread cmd2/cmd2.py
tleonhardt
tleonhardt previously approved these changes Apr 29, 2026
tleonhardt
tleonhardt previously approved these changes Apr 29, 2026
tleonhardt
tleonhardt previously approved these changes Apr 29, 2026
@kmvanbrunt kmvanbrunt merged commit 79c984a into main Apr 29, 2026
53 of 54 checks passed
@kmvanbrunt kmvanbrunt deleted the redirection_fixes branch April 29, 2026 02:17
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