Support path-like objects in shutil.which()

オープン 初心者向け
#161,788 コメント 7 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

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

評価

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

調査の方向性

まず shutil.which() と、bytes およびパスのようなコマンドを対象とするテストを読む。提示された BytesPath の例で TypeError を再現し、コマンドと検索パスの型が一致しているか確認する。bytes を基盤とするパスのようなコマンドが、同等の直接的な bytes コマンドと同じように動作すれば完了。関連する shutil テストを実行する。

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

説明

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
主要言語
Python
スター
78.2k
フォーク
36.6k
平均マージ
19時間 57分
マージ済み PR(30日)
630

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

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

python/cpython のほかの issue

python/cpython の issue をすべて見る

似ている issue

Python の issue をもっと見る

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

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