fix decorator problems in class methods (at least partially, there are problems with @staticmethod and @classmethod)

This commit is contained in:
David Halter
2013-08-09 02:11:06 +04:30
parent a6b1a247c2
commit 5cf39565ee

View File

@@ -209,7 +209,8 @@ class InstanceElement(use_metaclass(cache.CachedMetaClass, pr.Base)):
def get_decorated_func(self): def get_decorated_func(self):
""" Needed because the InstanceElement should not be stripped """ """ Needed because the InstanceElement should not be stripped """
func = self.var.get_decorated_func() #print 'gdf', self, self.is_decorated
func = self.var.get_decorated_func(self.instance)
if func == self.var: if func == self.var:
return self return self
return func return func
@@ -310,9 +311,8 @@ class Function(use_metaclass(cache.CachedMetaClass, pr.IsScope)):
self.base_func = func self.base_func = func
self.is_decorated = is_decorated self.is_decorated = is_decorated
@property
@cache.memoize_default() @cache.memoize_default()
def _decorated_func(self): def _decorated_func(self, instance=None):
""" """
Returns the function, that is to be executed in the end. Returns the function, that is to be executed in the end.
This is also the places where the decorators are processed. This is also the places where the decorators are processed.
@@ -323,10 +323,10 @@ class Function(use_metaclass(cache.CachedMetaClass, pr.IsScope)):
if not self.is_decorated: if not self.is_decorated:
for dec in reversed(self.base_func.decorators): for dec in reversed(self.base_func.decorators):
debug.dbg('decorator:', dec, f) debug.dbg('decorator:', dec, f)
dec_results = evaluate.follow_statement(dec) dec_results = set(evaluate.follow_statement(dec))
if not len(dec_results): if not len(dec_results):
debug.warning('decorator func not found: %s in stmt %s' % debug.warning('decorator not found: %s on %s' %
(self.base_func, dec)) (dec, self.base_func))
return None return None
if len(dec_results) > 1: if len(dec_results) > 1:
debug.warning('multiple decorators found', self.base_func, debug.warning('multiple decorators found', self.base_func,
@@ -334,6 +334,9 @@ class Function(use_metaclass(cache.CachedMetaClass, pr.IsScope)):
decorator = dec_results.pop() decorator = dec_results.pop()
# Create param array. # Create param array.
old_func = Function(f, is_decorated=True) old_func = Function(f, is_decorated=True)
if instance is not None:
old_func = InstanceElement(instance, old_func)
instance = None
wrappers = Execution(decorator, (old_func,)).get_return_types() wrappers = Execution(decorator, (old_func,)).get_return_types()
if not len(wrappers): if not len(wrappers):
@@ -350,12 +353,13 @@ class Function(use_metaclass(cache.CachedMetaClass, pr.IsScope)):
f = Function(f) f = Function(f)
return f return f
def get_decorated_func(self): def get_decorated_func(self, instance=None):
if self._decorated_func is None: decorated_func = self._decorated_func(instance)
if decorated_func is None:
raise DecoratorNotFound() raise DecoratorNotFound()
if self._decorated_func == self.base_func: if decorated_func == self.base_func:
return self return self
return self._decorated_func return decorated_func
def get_magic_method_names(self): def get_magic_method_names(self):
return builtin.Builtin.magic_function_scope.get_defined_names() return builtin.Builtin.magic_function_scope.get_defined_names()
@@ -368,8 +372,8 @@ class Function(use_metaclass(cache.CachedMetaClass, pr.IsScope)):
def __repr__(self): def __repr__(self):
dec = '' dec = ''
if self._decorated_func != self.base_func: if self._decorated_func() != self.base_func:
dec = " is " + repr(self._decorated_func) dec = " is " + repr(self._decorated_func())
return "<e%s of %s%s>" % (type(self).__name__, self.base_func, dec) return "<e%s of %s%s>" % (type(self).__name__, self.base_func, dec)