python / python/cpython

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

Đang mở
#156,072 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

stdlib type-bug
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Tái hiện ví dụ bằng tokenize.tokenize() và tokenize.untokenize(), sau đó kiểm tra Untokenizer.add_whitespace và vòng lặp trong Untokenizer.untokenize ở Lib/tokenize.py. Hoàn tất khi thông tin dòng ban đầu được sử dụng để giữ lại tab thứ hai trước 'hello'.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
devtools
Loại issue
Lỗi
Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
25/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.