Author: Antonio Cuni <[email protected]>
Branch: better-enforceargs
Changeset: r56109:6a5854e3d3cd
Date: 2012-07-17 18:04 +0200
http://bitbucket.org/pypy/pypy/changeset/6a5854e3d3cd/

Log:    add the possibility to disable the typechecking

diff --git a/pypy/rlib/objectmodel.py b/pypy/rlib/objectmodel.py
--- a/pypy/rlib/objectmodel.py
+++ b/pypy/rlib/objectmodel.py
@@ -108,12 +108,21 @@
 
 specialize = _Specialize()
 
-def enforceargs(*types):
+def enforceargs(*types, **kwds):
     """ Decorate a function with forcing of RPython-level types on arguments.
     None means no enforcing.
 
     XXX shouldn't we also add asserts in function body?
     """
+    typecheck = kwds.pop('typecheck', True)
+    if kwds:
+        raise TypeError, 'got an unexpected keyword argument: %s' % kwds.keys()
+    if not typecheck:
+        def decorator(f):
+            f._annenforceargs_ = types
+            return f
+        return decorator
+    #
     from pypy.annotation.signature import annotationoftype
     from pypy.annotation.model import SomeObject
     def decorator(f):
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
@@ -440,6 +440,14 @@
     # in RPython there is an implicit int->float promotion
     assert f(42) == 42
 
+def test_enforceargs_no_typecheck():
+    @enforceargs(int, str, None, typecheck=False)
+    def f(a, b, c):
+        return a, b, c
+    assert f._annenforceargs_ == (int, str, None)
+    assert f(1, 2, 3) == (1, 2, 3) # no typecheck
+
+
 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

Reply via email to