python / python/cpython

Duck-typing in `inspect.getfile`

Offen
#131,628 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

stdlib type-feature
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
35.9k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Running inspect.getsource(numpy.random.random_sample) yields

File ~\.conda\envs\sage-dev\Lib\inspect.py:943, in getfile(object)
    941 if iscode(object):
    942     return object.co_filename
--> 943 raise TypeError('module, class, method, function, traceback, frame, or '
    944                 'code object was expected, got {}'.format(
    945                 type(object).__name__))

since the cython function is not correct recognized as a "true" function (i.e. it is not derived from FunctionType). But since a Cython function correct provides __code__, all the information is actually there to properly get the source file.

Refs: https://github.com/cython/cython/blob/030e3886fa094ecae6121f8ccacaf814956dca5c/docs/src/userguide/limitations.rst#L34

While it is quite possible to emulate the interface of functions in Cython's own function type, and recent Cython releases have seen several improvements here, the "inspect" module does not consider a Cython implemented function a "function", because it tests the object type explicitly instead of comparing an abstract interface or an abstract base class. This has a negative impact on code that uses inspect to inspect function objects, but would require a change to Python itself.

and

At the very least, the inspect module should use more duck-typing internally. For example, consider this code from "getfile":

    if ismethod(object):

        object = object.__func__

    if isfunction(object):

        object = object.__code__

    if istraceback(object):

        object = object.tb_frame

    if isframe(object):

        object = object.f_code

    if iscode(object):

        return object.co_filename

Originally posted by @jdemeyer in #74257

Linked PRs
  • gh-131632

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 mit inspect.getfile und den im Issue gezeigten isfunction/iscode-Prüfungen. Vergleiche, wie Objekte behandelt werden, die code bereitstellen, und prüfe dann den verknüpften PR gh-131632, bevor du mit irgendwelchen Arbeiten beginnst. Als erledigt gilt, wenn die gemeldete Cython-Funktion ihre Quelldatei auflösen kann, ohne das bestehende Verhalten von inspect.getfile zu beeinträchtigen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Feature
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 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.