Support cythonized hookspecs/hookimpls

Offen Anfängerfreundlich
#830 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
71/100
Issue-Typ
Feature
Klarheit
Größtenteils klar
Aktivitätsstatus
Aktiv
Tech-Stack
python

Rechercherichtung

Beginne in pluggy/_manager.py und lies _static_hook_attr, um zu sehen, wie Hook-Implementierungen erkannt werden. Das Issue schlägt eine Änderung zur Erkennung von Cython-Funktionsobjekten vor; prüfe die bestehenden Manager-Tests zur Hook-Erkennung und füge einen Regressionstest für eine kompilierte Hook-Implementierung hinzu. Als abgeschlossen gilt die Aufgabe, wenn der Regressionstest besteht und cythonisierte Hooks erkannt werden.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

I have a project with hookimpls located in a .pyx module that is compiled to a native extension. With v1.7.0, the PluginManager doesn't discover the hookimpls in the compiled module, because inspect.isfunction() doesn't see cyfunction objects as functions (in pre-1.7.0 versions, inspect.isroutine() treated them as builtin functions). Is it deliberate or did it just slipped through unnoticed (since this is admitted a rare use case)? Below a simple patch that adds back the functionality without resorting to isroutine - if you agree that supporting cythonized functions is a valid use case, happy to turn it into a PR with a regression test.

--- a/pluggy/_manager.py
+++ b/pluggy/_manager.py
@@ -61,6 +61,12 @@ def _instance_dict(obj: object) -> dict[str, Any]:
     return instance_dict if isinstance(instance_dict, dict) else {}
 
 
+def _is_function_or_cyfunction(obj: object) -> bool:
+    return inspect.isfunction(obj) or type(obj).__name__ in (
+        "cython_function_or_method",
+        "fused_cython_function",
+    )
+
 def _static_hook_attr(
     obj: object, name: str
 ) -> tuple[object, Callable[..., object]] | None:
@@ -93,7 +99,7 @@ def _static_hook_attr(
         if value is not _ABSENT:
             if isinstance(value, types.MethodType):
                 return value.__func__, value
-            if inspect.isfunction(value):
+            if _is_function_or_cyfunction(value):
                 return value, value
             return None
 
@@ -105,7 +111,7 @@ def _static_hook_attr(
         return static, cast(Callable[..., object], static.__get__(owner, owner))
     if isinstance(static, types.MethodType):
         return static.__func__, static
-    if inspect.isfunction(static):
+    if _is_function_or_cyfunction(static):
         if inspect.isclass(obj) or inspect.ismodule(obj):
             return static, static
         return static, static.__get__(obj, type(obj))
Vorherrschende Sprache
Python
Sterne
1.7k
Forks
200
Ø Merge
10 Std. 4 Min.
Gemergte PRs (30 T.)
13

Entwicklungsumgebung

Dieses Projekt bietet weder Dev-Container noch Dockerfile noch Beitragsleitfaden – die Einrichtung liegt bei Ihnen. Beginnen Sie mit der README; die allgemeinen Schritte stehen in unserem Leitfaden für den ersten Beitrag.

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus pytest-dev/pluggy

Alle Issues in pytest-dev/pluggy

Ähnliche Issues

Weitere Issues zu Python

Neue Issues direkt in Ihr Postfach

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