python / python/cpython

`inspect.getfile` doesn't properly handle PEP 420

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

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

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

説明

Bug report

inspect.getfile handles all __file__ is None modules as built-in. Cf. https://github.com/python/cpython/blob/f298ba1f2712ad10530a30bb225548a6889820b5/Lib/inspect.py#L898-L901

This leads to PEP 420 packages being treated as built-in, as implicit namespaces have file set to None, but __path__ set to the actual path.

Creating PEP 420 directory:

~ ❯❯❯ mkdir python_test
~ ❯❯❯ vim python_test/hello.py

Python interpreter

Python 3.9.7 (default, Oct 10 2021, 14:08:54)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import python_test
>>> import inspect
>>> inspect.getfile(python_test)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/python-3.9.7/lib/python3.9/inspect.py", line 660, in getfile
    raise TypeError('{!r} is a built-in module'.format(object))
TypeError: <module 'python_test' (namespace)> is a built-in module
>>> print(python_test.__file__)
None
>>> print(python_test.__path__)
_NamespacePath(['/home/thetechrobo/python_test'])

Your environment

  • CPython versions tested on: 3.9.7
  • Operating system and architecture: Elive Linux 3.8.27 64-bit (Debian Bullseye); custom Python installation

Apologies if this is a duplicate. I've searched for any related issues, but I may have missed one.

Linked PRs
  • gh-92635

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

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

はじめの一歩

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

調査の方向性

レポートで参照されている getfile のロジック周辺にある Lib/inspect.py から始め、暗黙的な namespace package でその挙動を再現し、path を調べてください。既存の作業についてはリンクされている PR gh-92635 を確認してください。namespace package が built-in module として報告されなくなり、回帰がテストでカバーされれば完了です。

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

評価

技術スタック
python
領域
tooling
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
25/100

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

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