Skip to content

Restore caller output streams after CLI conversion - #384

Merged
mozillazg merged 4 commits into
mozillazg:developfrom
anishpentyala:fix/runner-stream-restoration-develop
Nov 22, 2026
Merged

mozillazg merged 4 commits into
mozillazg:developfrom
anishpentyala:fix/runner-stream-restoration-develop

Conversation

@anishpentyala

Copy link
Copy Markdown

PR 描述

The runner temporarily silences stdout/stderr while invoking converters. On success it currently restores sys.__stdout__/sys.__stderr__ rather than the streams the caller installed. If conversion raises, both streams remain replaced by NullWriter, so later diagnostics disappear.

Save the original caller streams and restore them in finally before printing converted results or propagating an exception. Add success cases for pinyin/slug and failure cases for ValueError/KeyboardInterrupt, checking exact stream identity, output suppression and exception identity. Add an Unreleased changelog entry.

Validation:

  • Full tests and module doctests: 1,050 passed and 2 skipped on CPython 3.13.18, 3.10.12 and 3.8.20.
  • Run with pytest -s and empty stdin, matching the project's capture-disabled configuration. Existing toneconvert tests read stdin and fail under pytest's default capture.
  • Reverting only the runtime change fails all four new regression cases.
  • Changed runtime/test-file Flake8 7.5.0 and git diff --check pass.

Logging-state behavior is unchanged. This does not make process-global output redirection thread-safe. No Python 2, Windows or remote CI result is claimed. Targeting develop as requested by the PR template.

待办事项

  • 符合代码规范
  • 单元测试
  • 文档 - changelog entry

@coderabbitai

coderabbitai Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: caa75f29-18bf-498a-9271-8c24afd6834c

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

anishpentyala and others added 4 commits November 22, 2026 21:25
Refactor output stream handling to use try-finally for restoration.
Added tests for runner functionality and error handling.
Added a new entry for the unreleased changes and fixed the output streams restoration note.
- main() 结束后恢复调用者的 logging 禁用级别(之前会永久关闭 CRITICAL 以下日志)
- NullWriter 增加 flush()/isatty(),避免被静默的第三方库调用时 AttributeError
- 注释说明必须恢复调用者安装的流而不能再写回 sys.__stdout__/__stderr__
- CHANGELOG 条目并入已有的 Unreleased 段并改为中文,补充 mozillazg#384 链接
- 测试用 fixture 消除重复的 setup,并断言 logging 状态被恢复
@mozillazg
mozillazg force-pushed the fix/runner-stream-restoration-develop branch from ed149d2 to 24a1e74 Compare November 22, 2026 13:28

@mozillazg mozillazg left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, Thanks for your contribution!

@mozillazg
mozillazg merged commit 574ab3a into mozillazg:develop Nov 22, 2026
23 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