Support path-like objects in shutil.which()

Aberta Para iniciantes
#161,788 7 comentários 0 reações 0 responsáveis Ver no GitHub

Mantenedores costumam responder em até 1 dia

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
2/5
Tempo estimado
1-3 horas
Facilidade para iniciantes
78/100
Tipo de issue
Bug
Clareza
Claramente especificada
Status de atividade
Ativa
Stack de tecnologia
python
Domínio
backend

Direção de pesquisa

Comece lendo shutil.which() e os testes para comandos do tipo bytes e semelhantes a caminhos. Reproduza o TypeError com o exemplo BytesPath fornecido e, em seguida, verifique se o comando e o caminho de busca usam tipos correspondentes. O trabalho estará concluído quando comandos semelhantes a caminhos baseados em bytes se comportarem como os comandos diretos equivalentes de bytes; execute os testes pertinentes de shutil.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

stdlib type-feature

shutil.which() accepts a bytes command and returns a bytes path. It also accepts path-like command objects, but a path-like object whose __fspath__() method returns bytes raises TypeError instead of behaving like the equivalent direct bytes value.

import shutil


class BytesPath:
    def __fspath__(self):
        return b"python3"


print(shutil.which(b"python3"))
print(shutil.which(BytesPath()))

On CPython main, the first call returns a bytes path and the second fails:

b'/usr/bin/python3'
TypeError: Can't mix strings and bytes in path components

which() checks whether cmd is bytes before resolving the path-like object. It then decodes PATH to str, while os.path.split() has resolved the command to bytes. Calling os.fspath(cmd) before choosing the string or bytes path keeps the command and search path on the same type.

CPython versions tested on
  • CPython main branch (3.16.0a2+)
Operating systems tested on
  • Linux ARM64
Linked PRs
  • gh-161789
Linguagem predominante
Python
Estrelas
78.2k
Forks
36.6k
Merge médio
19h 57min
PRs com merge (30d)
630

Preparar o ambiente

Abrir no Codespaces

Inicia o contêiner de desenvolvimento do projeto no navegador, com a sua própria conta do GitHub.

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de python/cpython

Todas as issues de python/cpython

Issues semelhantes

Mais issues de Python

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.