【发布时间】:2019-07-25 00:00:52
【问题描述】:
util.py:
import inspect
class Singleton(type):
_instances=[]
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
class MetaResult(Singleton):
def __getattribute__(cls, name):
return super().__getattribute__(name)
class Result(metaclass=MetaResult):
@staticmethod
def res_decorator(func):
def funcwrap(*args, **kwargs):
sig = inspect.signature(func)
bound_sig = sig.bind(*args, **kwargs)
bound_sig.apply_defaults()
#additional code to extract function arguments
return funcwrap
check_params.py
from util import Result as _Result
from abc import ABCMeta as _ABCMeta
class paramparse(metaclass=_ABCMeta)
@classmethod
@_Result.res_decorator
def parse_flash_params(cls, flash_config_path):
#some code
现在,我使用以下设置对文件 check_params.py 进行 cythonize:
cythonize.py
import os as _os
from pathlib import Path as _Path
from distutils.core import setup as _setup
from Cython.Distutils import build_ext as _build_ext
files_to_compile = []
def cython_build(source_path):
for dirpath, _, fnames in os.walk(source_path):
for fname in [x for x in fnames if f.endswith('.py'):
fname = _Path(fname)
files_to_compile.append(fname)
for e in files_to_compile:
e.cython_directives = {'binding':True, 'language_level':3}
_setup(name="Proj1",cmdclass={'build_ext':_build_ext}, ext_modules=files_to_compile)
cythonized 为: python cythonize.py --path C:\directory_where_check_params_exist
生成一个 pyd 文件,尝试在该文件上运行以下单元测试:
现在,在单元测试中使用:
unit_test_check_params.py
from check_params import * #getting error here , details outside the code
# unit tests written here
check_params.pyx:112:在初始化 check_params
???
电子
TypeError: Class-level classmethod() 只能在 method_descriptor 或实例方法上调用。
因此,当我调试它时,错误显示为由于 check_params.py 中装饰器 (def parse_flash_params) 上的类方法描述符引起的
如果您需要更多信息,请告诉我。
【问题讨论】:
-
什么是
Singletonclass1.decorator1?请发布我们可以实际运行的内容,在运行时实际重现问题。 -
它是Singletonclass1中定义的一个装饰器,它是一个单例类。我认为我们可以先从尝试一个简单的装饰器开始..
-
@classmethod在 Cython 中运行良好。一个简单的装饰器在 Cython 中运行良好。一个简单的装饰器与 classmethod 相结合可以按任何顺序正常工作。这个问题是无法回答的。阅读minimal reproducible example 并进行编辑。 -
嗨@DavidW,请检查更新的问题。我尽量做到最小化,同时仍然试图有意义。
-
您的 cythonize.py 有一些语法错误,因此无法正常工作。和 check_params 一样