From 6aafdf7f4bafd483230f339380864fff207b00cc Mon Sep 17 00:00:00 2001 From: chenzhirong <826531489@qq.com> Date: Wed, 12 Nov 2025 16:36:59 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20smart=5Fwrap=E8=A3=85=E9=A5=B0=E5=99=A8?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AF=B9=E8=B1=A1=E5=87=BD=E6=95=B0=E7=9A=84?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/smart_wrapper.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/core/smart_wrapper.py b/core/smart_wrapper.py index 2730261..07d63ea 100644 --- a/core/smart_wrapper.py +++ b/core/smart_wrapper.py @@ -1,6 +1,6 @@ import asyncio import inspect -from functools import wraps +from functools import wraps, partial from typing import Any, Callable """ 这个装饰器是用于同步代码,使其支持异步调用,避免阻塞事件事件循环 @@ -26,6 +26,19 @@ class SmartWrapper: def __init__(self, func: Callable): self.func = func self._is_coroutine = asyncio.iscoroutinefunction(func) + wraps(func)(self) # 使用 wraps 保留原始函数的元信息 + + def __get__(self, instance, owner): + """ + 描述符协议实现,用于支持实例方法装饰 + """ + if instance is None: + # 如果通过类访问 (e.g., MyClass.my_method),返回 self + return self + + # 如果通过实例访问 (e.g., obj.my_method),返回一个绑定了实例的新方法 + # partial 会将 instance 作为第一个参数,预填充到 __call__ 中 + return partial(self.__call__, instance) def __call__(self, *args, **kwargs): """智能调用:自动检测并适配调用环境"""