python / python/cpython

`tokenize.untokenize()` does not preserve the whitespace of the original source

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

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

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

説明

Bug report

Bug description:

tokenize.untokenize() replaces the whitespace between tokens and after indents with spaces:

>>> import tokenize
>>> code = b"if False:\n\tprint(\n\t\t'hello')"
>>> readline = iter(code.splitlines(keepends=True)).__next__
>>> tokenize.untokenize(tokenize.tokenize(readline))
b"if False:\n\tprint(\n\t 'hello')"

Note the second tab before 'hello' becomes a space.

The culprit is Untokenizer.add_whitespace which is called from the loop inside Untokenizer.untokenize whenever a 5-tuple iterator is used. The fifth item of the tuple is the original line; therefore, the loop could pass it to add_whitespace to reconstruct the original whitespace.

CPython versions tested on:

3.10, CPython main branch

Operating systems tested on:

Windows

Linked PRs
  • gh-156073

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

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

はじめの一歩

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

調査の方向性

tokenize.tokenize() と tokenize.untokenize() を使って例を再現し、その後 Lib/tokenize.py の Untokenizer.add_whitespace と Untokenizer.untokenize 内のループを調べます。元の行情報が使用され、'hello' の前の 2 つ目のタブが保持されれば完了です。

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

評価

技術スタック
python
領域
devtools
issue の種類
バグ
難易度
2/5
見積もり時間
1〜3時間
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
25/100

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

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