Support cythonized hookspecs/hookimpls

未關閉 適合新手
#830 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
2/5
預估耗時
1-3 小時
新手友好度
71/100
Issue 類型
功能
描述清晰度
基本清楚
活躍度
活躍
技術堆疊
python

研究方向

從 pluggy/_manager.py 開始,閱讀 _static_hook_attr,了解如何辨識 hook 實作。Issue 提出了辨識 Cython 函式物件的變更方案;檢查現有 manager 測試中關於 hook 探索的部分,並為已編譯的 hook 實作新增回歸測試。回歸測試通過且能探索到經 Cython 編譯的 hook,即表示完成。

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

描述

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))
主要語言
Python
星號
1.7k
分支
200
平均合併
10 小時 4 分鐘
30 天內合併 PR
13

環境準備

這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

pytest-dev/pluggy 的其他 Issue

查看 pytest-dev/pluggy 的全部 Issue

相似的 Issue

更多 Python Issue

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

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