From 17c4795827e4f820c5286de591f701f5cd329f23 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner <inform@tiker.net> Date: Sun, 17 Jan 2021 23:49:31 -0600 Subject: [PATCH] Rewrite first_arg_dependent_memoize without decorator package, drop dep --- pyopencl/tools.py | 37 ++++++++++++++++++++----------------- setup.py | 1 - 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/pyopencl/tools.py b/pyopencl/tools.py index 5b814685..ff5c7965 100644 --- a/pyopencl/tools.py +++ b/pyopencl/tools.py @@ -32,7 +32,6 @@ from sys import intern # Do not add a pyopencl import here: This will add an import cycle. import numpy as np -from decorator import decorator from pytools import memoize, memoize_method from pyopencl._cl import bitlog2 # noqa: F401 from pytools.persistent_dict import KeyBuilder as KeyBuilderBase @@ -73,8 +72,7 @@ from pyopencl._cl import ( # noqa _first_arg_dependent_caches = [] -@decorator -def first_arg_dependent_memoize(func, cl_object, *args): +def first_arg_dependent_memoize(func): """Provides memoization for a function. Typically used to cache things that get created inside a :class:`pyopencl.Context`, e.g. programs and kernels. Assumes that the first argument of the decorated function is @@ -84,21 +82,26 @@ def first_arg_dependent_memoize(func, cl_object, *args): .. versionadded:: 2011.2 """ - try: - ctx_dict = func._pyopencl_first_arg_dep_memoize_dic - except AttributeError: - # FIXME: This may keep contexts alive longer than desired. - # But I guess since the memory in them is freed, who cares. - ctx_dict = func._pyopencl_first_arg_dep_memoize_dic = {} - _first_arg_dependent_caches.append(ctx_dict) + def first_arg_memoized_wrapper(cl_object, *args): + try: + ctx_dict = func._pyopencl_first_arg_dep_memoize_dic + except AttributeError: + # FIXME: This may keep contexts alive longer than desired. + # But I guess since the memory in them is freed, who cares. + ctx_dict = func._pyopencl_first_arg_dep_memoize_dic = {} + _first_arg_dependent_caches.append(ctx_dict) - try: - return ctx_dict[cl_object][args] - except KeyError: - arg_dict = ctx_dict.setdefault(cl_object, {}) - result = func(cl_object, *args) - arg_dict[args] = result - return result + try: + return ctx_dict[cl_object][args] + except KeyError: + arg_dict = ctx_dict.setdefault(cl_object, {}) + result = func(cl_object, *args) + arg_dict[args] = result + return result + + from functools import update_wrapper + update_wrapper(first_arg_memoized_wrapper, func) + return first_arg_memoized_wrapper context_dependent_memoize = first_arg_dependent_memoize diff --git a/setup.py b/setup.py index bf63e71c..ba268a6a 100644 --- a/setup.py +++ b/setup.py @@ -247,7 +247,6 @@ def main(): install_requires=[ "numpy", "pytools>=2017.6", - "decorator>=3.2.0", "appdirs>=1.4.0", # "Mako>=0.3.6", ], -- GitLab