Handle decorators correctly.

This commit is contained in:
Kunal Parmar
2012-06-10 00:26:28 -07:00
parent 6a1eba1622
commit 0fd7bccf52
3 changed files with 54 additions and 1 deletions

View File

@@ -366,8 +366,24 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
kwparamList.push_front(std::make_pair(key, val));
}
for (int i=0; i<pparams; i++) {
pparamList.push_front(stack.top());
PycRef<ASTNode> param = stack.top();
stack.pop();
if (param->type() == ASTNode::NODE_FUNCTION) {
PycRef<ASTNode> code = param.cast<ASTFunction>()->code();
PycRef<PycCode> code_src = code.cast<ASTObject>()->object().cast<PycCode>();
PycRef<PycString> function_name = code_src->name();
if (function_name->isEqual("<lambda>")) {
pparamList.push_front(param);
} else {
// Decorator used
PycRef<ASTNode> name = new ASTName(function_name);
curblock->append(new ASTStore(param, name));
pparamList.push_front(name);
}
} else {
pparamList.push_front(param);
}
}
PycRef<ASTNode> func = stack.top();
stack.pop();

37
tests/test_decorators.py Normal file
View File

@@ -0,0 +1,37 @@
@staticmethod
def square(n):
return n * n
# From: http://wiki.python.org/moin/PythonDecoratorLibrary
def synchronized(lock):
"""Synchronization decorator."""
def wrap(f):
def new_function(*args, **kw):
lock.acquire()
try:
return f(*args, **kw)
finally:
lock.release()
return new_function
return wrap
from threading import Lock
cache_lock = Lock()
class Cache(object):
def __init__(self):
self._name = 'default'
@classmethod
@synchronized(cache_lock)
def cache(cls):
return cls()
@property
def name(self):
return self._name
@name.setter
def name(self, new_name):
self._name = new_name

BIN
tests/test_decorators.pyc Normal file

Binary file not shown.