Skip to content

Preserve existing TOML files when UTF-8 encoding fails - #722

Open
GreedyC wants to merge 1 commit into
python-poetry:masterfrom
GreedyC:codex/encode-before-truncate
Open

GreedyC wants to merge 1 commit into
python-poetry:masterfrom
GreedyC:codex/encode-before-truncate

Conversation

@GreedyC

@GreedyC GreedyC commented Nov 26, 2026

Copy link
Copy Markdown

Summary

TOMLFile.write() opens and truncates the destination before encoding the document. If a string contains an unpaired surrogate, UTF-8 encoding raises after the original file has already been emptied.

Encode the complete output before opening the file, then write those bytes. A regression test verifies that an encoding error preserves the original bytes. Existing line-ending tests also pass.

Duplicate Check

Validation

The new regression fails on the original code. All nine TOMLFile tests and Ruff check/format pass. After initializing the toml-test fixture submodule, the complete test suite passed: 1,483 tests.

Agent Drafting Metadata

  • Agent: Codex
  • Model: GPT-6.1
  • Notes: AI-assisted investigation, implementation and test preparation, requested by the repository contributor. No human review is claimed.

@human-attestation

Copy link
Copy Markdown

🛡️ Human Attestation required. A human needs to confirm this PR once before it can merge.

Attest this PR — sign in with GitHub, pass the challenge, and confirm one of the statements.

@frostming

Copy link
Copy Markdown
Contributor

Please complete the human attestation and add an entry to CHANGELOG.md.

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