Skip to content

Preserve inplace input until processing and encoding succeed - #102

Open
codeofwxz wants to merge 1 commit into
cdown:developfrom
codeofwxz:fix-inplace-output-before-errors
Open

codeofwxz wants to merge 1 commit into
cdown:developfrom
codeofwxz:fix-inplace-output-before-errors

Conversation

@codeofwxz

Copy link
Copy Markdown

Related #60.

The tools open an inplace destination with wb before consuming the lazy subtitle parser. A parsing or processing error therefore leaves the original file empty. Encoding errors can also occur after truncation.

Keep the inplace output path unopened until the complete output has been composed and encoded. A shared write_output helper performs that final write; all seven writing tools use it, including mux's unchanged non-inplace path. Ordinary stdout and explicit output streams retain the existing encoding/fallback logic.

This protects the original input from parsing, conversion, and encoding failures. It does not provide atomic replacement or protection against write errors, process termination during writing, or power loss. The destination is still opened directly, preserving the existing inode, permissions, and link-following behavior. No runtime dependencies or new Python requirements are introduced.

Validation:

  • Real subprocess tests cover the six inplace tools, successful stdout/file/inplace output equivalence, parsing and conversion errors, encoding failures, UTF-8/UTF-16 output, hard links, parameter conflicts, and ignore-parsing-errors. The initial regression set had 10 failures on the baseline; it passes after the change.
  • Python 3.9.13 with the repository's test dependency ranges: 68 passed, 1 skipped; doctest run: 74 passed, 1 skipped.
  • Python 3.14.4 with current pytest/Hypothesis: 68 passed, 1 skipped; the configured srt.py branch coverage target is 100%. This coverage target does not measure the CLI helpers.
  • The skipped test creates a symbolic link, which this Windows environment does not permit. Hard-link behavior passed. Actual Python 2.7 and the full OS/version matrix were not run; a Python 2.7 grammar check accepted the helper, CLI scripts, and new test file.
  • Black and the configured Bandit check pass. The configured Pylint command reports the same three diagnostics on the unchanged baseline (one removed-option warning and two positional-argument warnings). Pytype was not run.

All test subtitles are synthetic. Repository maintenance is currently infrequent; no maintainer response or acceptance is assumed.

Copilot AI lite review requested due to automatic review settings September 12, 2026 15:50

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

All reviewed files are covered and no blocking issues were identified.

Pull request overview

This pull request prevents in-place files from being truncated before processing and encoding succeed.

Changes:

  • Adds shared deferred-output handling.
  • Updates all seven writing tools.
  • Adds comprehensive CLI regression tests.
File summaries
File Summary
tests/test_tools.py Adds regression coverage for output modes, failures, encodings, and links.
srt_tools/utils.py Implements deferred in-place writing.
srt_tools/srt-process Uses the shared output helper.
srt_tools/srt-normalise Uses the shared output helper.
srt_tools/srt-mux Uses the shared output helper.
srt_tools/srt-lines-matching Uses the shared output helper.
srt_tools/srt-linear-timeshift Uses the shared output helper.
srt_tools/srt-fixed-timeshift Uses the shared output helper.
srt_tools/srt-deduplicate Uses the shared output helper.
Review details
  • Files reviewed: 9/9 changed files
  • Comments generated: 0
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

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