Skip to content

Escape backslashes when drawing PostScript text - #9996

Open
lllleolin-max wants to merge 1 commit into
python-pillow:mainfrom
lllleolin-max:fix/psdraw-text-escaping
Open

Escape backslashes when drawing PostScript text#9996
lllleolin-max wants to merge 1 commit into
python-pillow:mainfrom
lllleolin-max:fix/psdraw-text-escaping

Conversation

@lllleolin-max

Copy link
Copy Markdown
Contributor

PSDraw.text() escapes parentheses but leaves backslashes unchanged. Text such as C:\temp\new is therefore interpreted as containing PostScript escape sequences, while a trailing backslash escapes the closing delimiter and produces an unfinished string.

Changes proposed in this pull request:

  • Escape backslashes before escaping parentheses, following the literal text string rules in PostScript Language Reference, section 3.2.2.
  • Add exact-output regressions for Windows paths, a trailing backslash, backslashes adjacent to parentheses and a UNC path, with parentheses, Latin-1 text and empty-string controls. Include a release note.

Validation:

  • With unchanged main, Tests/test_psdraw.py: 5 failed, 5 passed. After the fix, Tests/test_psdraw.py and Tests/test_pdfparser.py: 18 passed, no skips; selftest.py: 59 passed.
  • A separate check writes complete PSDraw documents and decodes the emitted literal strings with the existing PdfParser: 5 of 8 cases fail before the fix, and all 8 round-trip afterwards. PDF and PostScript use the same literal-string escaping rules; this checks serialization, not rendering.
  • Black, Ruff, mypy, Bandit, Sphinx-lint and whitespace/end-of-file/line-ending checks passed. The new release-note section also passes Docutils parsing with warnings treated as errors.

Tests ran on Windows/Python 3.12.11 with the checkout's PSDraw and PdfParser modules and Pillow 12.3.0's prebuilt native extensions and remaining Python modules. Native extensions were not rebuilt. Ghostscript is unavailable here, so no interpreter/rendering check was run; the full test, pre-commit and Sphinx documentation suites were not run.

Prepared with AI assistance.

@radarhere radarhere added the 🤖-assisted AI-assisted label Sep 13, 2026
@radarhere

Copy link
Copy Markdown
Member

Hmm.

  1. This would completely block the use of PostScript escape sequences. Are there any escape sequences that are potentially useful?
  2. Is this potentially a breaking change? Before this PR, users might have figured out that four slashes \\\\ would render as \, but with PR, four slashes would suddenly start rendering as \\. Pillow values backwards-compatibility.

I wonder if it might be better to leave the current behaviour as it is, and instead add a new escape keyword argument. So ps.text((10, 20), text would continue to function as-is, but ps.text((10, 20), text, escape=True) would render the text as you expect.

What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🤖-assisted AI-assisted

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants