Skip to content

Python: make the memory markdown escape invertible for backslash runs - #9728

Open
Manohar Paturi (ManoharPaturi) wants to merge 1 commit into
microsoft:mainfrom
ManoharPaturi:fix/memory-escape-roundtrip
Open

Manohar Paturi (ManoharPaturi) wants to merge 1 commit into
microsoft:mainfrom
ManoharPaturi:fix/memory-escape-roundtrip

Conversation

@ManoharPaturi

Copy link
Copy Markdown
Contributor

Motivation & Context

_escape_markdown_line prepended one backslash to any line starting with # or \#, but _unescape_markdown_line only stripped a backslash from a body starting with exactly \#. A line that already began with an escaped hash therefore gained a backslash on every write and read cycle, and the corruption compounded across the rereads that auto loading and consolidation perform. Reproduced on main with a one line round trip: \# literal fact came back as \\# literal fact.

Description & Review Guide

  • What are the major changes? The escape maps a run of n backslashes before a hash to 2n + 1, and the reader reduces an odd run back to the original n. A plain # line still gets the single backslash it always had, which is the n = 0 case.
  • What is the impact of these changes? Every backslash run length round trips exactly. Even runs are not produced by this writer, so the reader passes them through untouched; that also keeps lines written by the previous scheme from being mangled further.
  • What do you want reviewers to focus on? The round trip test covers a plain hash line, a single escaped hash, and a two backslash run, and fails on main.

Related Issue

Fixes #9727

AI Assistance

  • No material AI assistance was used.
  • This is an AI-assisted contribution. I reviewed, understood, and verified all submitted content and accept responsibility for it.

Escaping a line that already started with backslash hash added one more backslash the reader never stripped, so every reload corrupted the line a little more. The escape now maps a run of n backslashes before a hash to 2n plus 1, and the reader reduces an odd run back to the original count; even runs were not written here and pass through untouched. Plain hash lines keep the existing single backslash behavior.

Signed-off-by: Manohar Paturi <186662190+ManoharPaturi@users.noreply.github.com>

This branch was previously deployed

1 inactive deployment
github-app-auth — b6f347c0 Deployed Oct 26, 2026 by ManoharPaturi via team_check #7562
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: memory lines starting with backslash hash gain a backslash on every round trip

1 participant