Author: Greg Price <[email protected]>
Branch: signatures
Changeset: r59307:74797b5db019
Date: 2012-12-02 14:16 -0800
http://bitbucket.org/pypy/pypy/changeset/74797b5db019/
Log: Apply signature to arguments, and add some basic types
diff --git a/pypy/annotation/description.py b/pypy/annotation/description.py
--- a/pypy/annotation/description.py
+++ b/pypy/annotation/description.py
@@ -1,5 +1,6 @@
from __future__ import absolute_import
import types, py
+from pypy.annotation.signature import enforce_signature_args
from pypy.objspace.flow.model import Constant, FunctionGraph
from pypy.objspace.flow.bytecode import cpython_code_signature
from pypy.objspace.flow.argument import rawshape, ArgErr
@@ -276,12 +277,17 @@
policy = self.bookkeeper.annotator.policy
self.specializer = policy.get_specializer(tag)
enforceargs = getattr(self.pyobj, '_annenforceargs_', None)
+ signature = getattr(self.pyobj, '_signature_', None)
+ if enforceargs and signature:
+ raise Exception("%r: signature and enforceargs cannot both be
used" % (self,))
if enforceargs:
if not callable(enforceargs):
from pypy.annotation.policy import Sig
enforceargs = Sig(*enforceargs)
self.pyobj._annenforceargs_ = enforceargs
enforceargs(self, inputcells) # can modify inputcells in-place
+ if signature:
+ enforce_signature_args(self, signature[0], inputcells) # mutates
inputcells
if getattr(self.pyobj, '_annspecialcase_',
'').endswith("call_location"):
return self.specializer(self, inputcells, op)
else:
diff --git a/pypy/annotation/signature.py b/pypy/annotation/signature.py
--- a/pypy/annotation/signature.py
+++ b/pypy/annotation/signature.py
@@ -130,3 +130,10 @@
s_arg,
s_input))
inputcells[:] = args_s
+
+
+def enforce_signature_args(funcdesc, argtypes, inputcells):
+ args_s = []
+ for i, argtype in enumerate(argtypes):
+ args_s.append(annotation(argtype, bookkeeper=funcdesc.bookkeeper))
+ inputcells[:] = args_s
diff --git a/pypy/annotation/types.py b/pypy/annotation/types.py
--- a/pypy/annotation/types.py
+++ b/pypy/annotation/types.py
@@ -1,1 +1,8 @@
from pypy.annotation import model
+
+
+def int():
+ return model.SomeInteger()
+
+def str():
+ return model.SomeString()
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
@@ -1,5 +1,6 @@
import py
from pypy.rlib.objectmodel import *
+from pypy.annotation import types, model
from pypy.translator.translator import TranslationContext, graphof
from pypy.rpython.test.tool import BaseRtypingTest, LLRtypeMixin, OORtypeMixin
from pypy.rpython.test.test_llinterp import interpret
@@ -487,7 +488,7 @@
assert TYPES == [lltype.Signed, lltype.Float]
-def test_signature_decorator():
+def test_signature_bookkeeping():
@signature('x', 'y', returns='z')
def f(a, b):
return a + len(b)
@@ -497,6 +498,19 @@
assert f.foo == 'foo'
assert f(1, 'hello') == 6
+def getsig(f):
+ # returns [param1, param2, ..., ret]
+ t = TranslationContext()
+ a = t.buildannotator()
+ g = a.annotate_helper(f, [model.s_ImpossibleValue]*f.func_code.co_argcount)
+ return [a.bindings[v] for v in g.startblock.inputargs] +
[a.bindings[g.getreturnvar()]]
+
+def test_signature_basic():
+ @signature(types.int(), types.str(), returns=types.int())
+ def f(a, b):
+ return a + len(b)
+ assert getsig(f) == [model.SomeInteger(), model.SomeString(),
model.SomeInteger()]
+
def getgraph(f, argtypes):
from pypy.translator.translator import TranslationContext, graphof
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit