`tokenize.untokenize()` does not preserve the whitespace of the original source
Ninguém assumiu esta issue ainda.
- Linguagem predominante
- Python
- Estrelas
- 77.2k
- Forks
- 36k
- Métricas de merge de PRs
- Métricas de PR pendentes
Descrição
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
Guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Direção de pesquisa
Reproduza o exemplo com tokenize.tokenize() e tokenize.untokenize() e, em seguida, inspecione Untokenizer.add_whitespace e o loop em Untokenizer.untokenize em Lib/tokenize.py. Está concluído quando as informações originais das linhas são usadas para que a segunda tabulação antes de 'hello' seja preservada.
Escrita pelo modelo de indexação a partir do texto da issue.
Avaliação
- Stack de tecnologia
- python
- Domínio
- devtools
- Tipo de issue
- Bug
- Dificuldade
- 2/5
- Tempo estimado
- 1-3 horas
- Status de atividade
- Estagnada
- Clareza
- Claramente especificada
- Facilidade para iniciantes
- 25/100