python / python/cpython

Add escaping mode to PyUnicode_FromFormat(): %#c, %#s, %#S, %#U, %#V

オープン
#154,668 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

interpreter-core type-feature
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

Feature or enhancement

Error messages often embed a C string or an object via the s, S, U and V conversions in PyUnicode_FromFormat() and PyErr_Format(). If the string contains non-printable characters, the message can be mangled or misleading: embedded newlines and terminal escape sequences break the output, invisible characters hide the actual content. The R and A conversions escape, but add surrounding quotes and require a Python object.

I propose to add an escaping mode, enabled by the # flag for the c, s, S, U and V conversions. It escapes special and non-printable characters like repr() does, but without adding quotes: \\, \t, \r and \n; other non-printable ASCII characters as \xNN; other non-printable characters as \uNNNN or \UNNNNNNNN. With the additional + flag (%+#s), all non-ASCII characters are escaped as well, so the result is pure ASCII.

For the s and V conversions, bytes which cannot be decoded from UTF-8 are escaped as \xNN, while valid non-ASCII characters are always escaped as \uNNNN or \UNNNNNNNN (e.g. \u009f for valid \xc2\x9f), so they can always be distinguished.

The precision is applied to the string before escaping, the width -- after escaping.

Quotes are deliberately not escaped: the escaped string is intended to be embedded into a larger message without quotes. For quoted output use the R and A conversions.

Related: gh-154610 (escaping attribute names in AttributeError messages).

Linked PRs
  • gh-154669

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

名前付きの PyUnicode_FromFormat() および PyErr_Format() エントリポイントから始め、既存の R 変換と A 変換、および関連する gh-154610 issue を比較してください。作業はすでに gh-154669 にリンクされているため、独自に開始するのではなく、そちらを確認してください。完了条件には、指定された # と + のエスケープ、およびエスケープ前の精度とエスケープ後の幅のルールが含まれます。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
c, python
領域
backend-api-design
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。