Author: Greg Price <[email protected]>
Branch: signatures
Changeset: r59304:cbd9520d99f4
Date: 2012-12-02 13:01 -0800
http://bitbucket.org/pypy/pypy/changeset/cbd9520d99f4/
Log: Simple nop signature decorator, just bookkeeping so far
diff --git a/pypy/rlib/objectmodel.py b/pypy/rlib/objectmodel.py
--- a/pypy/rlib/objectmodel.py
+++ b/pypy/rlib/objectmodel.py
@@ -193,6 +193,26 @@
return result
return decorator
+def signature(*paramtypes, **kwargs):
+ """Decorate a function to specify its type signature.
+
+ Usage:
+ @signature(param1type, param2type, ..., returns=returntype)
+ def foo(...)
+
+ The arguments paramNtype and returntype should be instances
+ of the classes in pypy.annotation.types.
+ """
+ returntype = kwargs.pop('returns', None)
+ if returntype is None:
+ raise TypeError, "signature: parameter 'returns' required"
+
+ def decorator(f):
+ f._signature_ = (paramtypes, returntype)
+ return f
+ return decorator
+
+
# ____________________________________________________________
diff --git a/pypy/rlib/test/test_objectmodel.py
b/pypy/rlib/test/test_objectmodel.py
--- a/pypy/rlib/test/test_objectmodel.py
+++ b/pypy/rlib/test/test_objectmodel.py
@@ -486,6 +486,18 @@
TYPES = [v.concretetype for v in graph.getargs()]
assert TYPES == [lltype.Signed, lltype.Float]
+
+def test_signature_decorator():
+ @signature('x', 'y', returns='z')
+ def f(a, b):
+ return a + len(b)
+ f.foo = 'foo'
+ assert f._signature_ == (('x', 'y'), 'z')
+ assert f.func_name == 'f'
+ assert f.foo == 'foo'
+ assert f(1, 'hello') == 6
+
+
def getgraph(f, argtypes):
from pypy.translator.translator import TranslationContext, graphof
from pypy.translator.backendopt.all import backend_optimizations
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit