python / python/cpython

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

未關閉
#92,525 2 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

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. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

從報告中提到的 getfile 邏輯附近的 Lib/inspect.py 開始,接著使用隱式命名空間套件重現此行為,並檢查其 path。查看已連結的 PR gh-92635 以了解現有工作;完成的判定標準是該命名空間套件不再被報告為內建模組,且該回歸問題已由測試涵蓋。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
tooling
Issue 類型
缺陷
難度
3/5
預估耗時
1-2 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
25/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。