python / python/cpython

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

Offen
#92,525 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

stdlib type-bug
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
36k
Ø Merge
1 T. 9 Std.
Gemergte PRs (30 T.)
558

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne in Lib/inspect.py bei der im Bericht referenzierten getfile-Logik, reproduziere dann das Verhalten mit einem impliziten Namespace-Paket und untersuche dessen path. Prüfe den verknüpften PR gh-92635 auf bereits vorhandene Arbeit; abgeschlossen ist die Aufgabe, wenn das Namespace-Paket nicht mehr als integriertes Modul gemeldet wird und die Regression durch Tests abgedeckt ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
tooling
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.