Handle decorators correctly.
This commit is contained in:
18
ASTree.cpp
18
ASTree.cpp
@@ -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
37
tests/test_decorators.py
Normal 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
BIN
tests/test_decorators.pyc
Normal file
Binary file not shown.
Reference in New Issue
Block a user