Skip to content

py: file-writing API that bypasses newline translation - #265

Open
samtalki wants to merge 2 commits into
mainfrom
worktree-python-write-file
Open

py: file-writing API that bypasses newline translation#265
samtalki wants to merge 2 commits into
mainfrom
worktree-python-write-file

Conversation

@samtalki

Copy link
Copy Markdown
Member

Adds Network.write_file(path, to), DistNetwork.write_file(path, to), and an out= path on convert_file, all writing the serialized case to disk from Rust, byte exact.

Motivation: a user wrote a v33 .raw same-format echo through open(path, "w") on Windows. Python's text mode translates every \n to \r\n, so the echoed CRLF source landed on disk as \r\r\n. Single-line records survive that, but the four-line v33 transformer records lose their alignment, and PowerWorld rejects the case ("Reading Error in Transformer Data Object"). The library output was byte identical to the source; the corruption happened at the write. The new paths make the correct write the obvious one, and the to_format/convert_file docs now name the trap.

Covered by pytest: the CRLF echo reaches disk byte exact through both write_file and convert_file(out=...), plus the dist sibling.

🤖 Generated with Claude Code

Network.write_file(path, to) / DistNetwork.write_file(path, to) and a
convert_file(..., out=...) output path write the serialized case to
disk exactly as produced, in Rust. Writing to_format text through
open(path, "w") corrupts a CRLF source echo on Windows: text mode
turns each \r\n into \r\r\n, which breaks the fixed line count
records of a PSS/E raw file (PowerWorld rejects the case).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings July 22, 2026 21:42
@github-actions

github-actions Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Conversion Matrix

Legend

Cells show X/Y: observed warnings / expected warnings. Counts include source parse, target write, and target readback.

  • 🟢 0/0: no warnings and checked invariants held.
  • 🟡 X=Y: observed warnings match the reviewed expected count, and that count is nonzero.
  • 🔴 X!=Y or invariant failure: behavior changed. If warnings decreased because fidelity improved, update the expected counts in the same PR.
  • Expected counts are the *_WARNING_BASELINE arrays in powerio-cli/tests/conversion_matrix_report.rs; accept an intentional change by editing the matching source/target entry in the same PR.

Transmission

7 cases.

Source ↓ / target → MATPOWER .m PowerModels JSON PSS/E .raw PowerWorld .aux egret JSON pandapower JSON Surge JSON PSLF .epc
MATPOWER .m 🟢 0/0 🟢 0/0 🟡 8/8 🟡 8/8 🟢 0/0 🟡 5/5 🟢 0/0 🟡 11/11
PowerModels JSON 🟡 6/6 🟢 0/0 🟡 14/14 🟡 14/14 🟡 1/1 🟡 11/11 🟡 24/24 🟡 16/16
PSS/E .raw 🟡 13/13 🟡 1/1 🟢 0/0 🟡 1/1 🟡 1/1 🟡 3/3 🟡 1/1 🟡 10/10
PowerWorld .aux 🟡 12/12 🟢 0/0 🟢 0/0 🟢 0/0 🟢 0/0 🟡 2/2 🟢 0/0 🟡 5/5
egret JSON 🟢 0/0 🟢 0/0 🟡 8/8 🟡 8/8 🟢 0/0 🟡 5/5 🟢 0/0 🟡 11/11
pandapower JSON 🟡 6/6 🟢 0/0 🟡 11/11 🟡 11/11 🟡 5/5 🟢 0/0 🟢 0/0 🟡 11/11
Surge JSON 🟡 2/2 🟡 2/2 🟡 10/10 🟡 10/10 🟡 2/2 🟡 7/7 🟡 2/2 🟡 17/17
PSLF .epc 🟡 17/17 🟡 5/5 🟡 5/5 🟡 5/5 🟡 5/5 🟡 7/7 🟡 5/5 🟡 8/8
DeepMind OPFData JSON 🟡 3/3 🟡 2/2 🟡 5/5 🟡 5/5 🟡 3/3 🟡 4/4 🟡 2/2 🟡 4/4

Distribution

7 cases.

Source ↓ / target → OpenDSS .dss BMOPF JSON PMD JSON
OpenDSS .dss 🟢 0/0 🟡 187/187 🟡 120/120
BMOPF JSON 🟡 1/1 🟢 0/0 🟢 0/0
PMD JSON 🟡 22/22 🟡 71/71 🟢 0/0

Full warning details: download conversion-matrix-warning-details for this workflow run.

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.

Pull request overview

Adds Python-facing file-writing APIs that write serialized case text to disk from Rust to avoid Windows text-mode newline translation corrupting CRLF-retained source echoes (notably PSS/E v33 multi-line transformer records).

Changes:

  • Add Network.write_file(path, to) and DistNetwork.write_file(path, to) to write serialized output byte-exact via Rust.
  • Extend convert_file(..., out=...) to optionally write the converted text to an output path byte-exact.
  • Add pytest coverage to assert byte-for-byte disk output for both transmission (PSS/E) and dist (DSS) cases, plus changelog/docs updates warning about open(path, "w") on Windows.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
python/tests/test_powerio.py Adds tests ensuring CRLF-retained PSS/E echoes are written byte-exact via write_file and convert_file(out=...).
python/tests/test_dist.py Adds a dist-network echo test verifying byte-exact disk output via DistNetwork.write_file.
python/powerio/dist.py Adds DistNetwork.write_file Python wrapper method delegating to Rust for byte-exact writes.
python/powerio/_powerio.pyi Extends the PyO3 stub surface with write_file and convert_file(..., out=...).
python/powerio/init.pyi Updates public Python stubs for Network.write_file and convert_file(..., out=...).
python/powerio/init.py Adds Network.write_file wrapper and documents convert_file(..., out=...) newline-translation trap on Windows.
powerio-py/src/lib.rs Implements Rust-backed PyNetwork.write_file, PyDistNetwork.write_file, and convert_file(..., out=...) file writes via std::fs::write.
CHANGELOG.md Documents the new Python APIs and the Windows CRLF corruption pitfall.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread python/powerio/__init__.py Outdated
Comment thread python/powerio/dist.py Outdated
The wrappers annotated the fidelity-warning return as bare list while the
stubs say list[str]; both files use from __future__ import annotations, so
the subscripted form is safe on 3.9.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YPB7LVTeMcdBYnzA9QdpPe
Copilot AI review requested due to automatic review settings July 23, 2026 16:37

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.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated no new comments.

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.

3 participants