[pypy-commit] pypy better-enforceargs: improve the typechecking to be more similar to what the annotator actually does
antocuni
noreply at buildbot.pypy.org
Tue Jul 17 18:10:58 CEST 2012
Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: better-enforceargs
Changeset: r56108:8ffbf0ff76d4
Date: 2012-07-17 18:02 +0200
http://bitbucket.org/pypy/pypy/changeset/8ffbf0ff76d4/
Log: improve the typechecking to be more similar to what the annotator
actually does
diff --git a/pypy/rlib/objectmodel.py b/pypy/rlib/objectmodel.py
--- a/pypy/rlib/objectmodel.py
+++ b/pypy/rlib/objectmodel.py
@@ -7,6 +7,7 @@
import sys
import types
import math
+import inspect
# specialize is a decorator factory for attaching _annspecialcase_
# attributes to functions: for example
@@ -113,11 +114,20 @@
XXX shouldn't we also add asserts in function body?
"""
- import inspect
+ from pypy.annotation.signature import annotationoftype
+ from pypy.annotation.model import SomeObject
def decorator(f):
+ def get_annotation(t):
+ if isinstance(t, SomeObject):
+ return t
+ return annotationoftype(t)
def typecheck(*args):
- for t, arg in zip(types, args):
- if t is not None and not isinstance(arg, t):
+ for expected_type, arg in zip(types, args):
+ if expected_type is None:
+ continue
+ s_expected = get_annotation(expected_type)
+ s_argtype = get_annotation(type(arg))
+ if not s_expected.contains(s_argtype):
raise TypeError
#
# we cannot simply wrap the function using *args, **kwds, because it's
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
@@ -433,6 +433,12 @@
return a+b
assert f(2) == 42
+def test_enforceargs_int_float_promotion():
+ @enforceargs(float)
+ def f(x):
+ return x
+ # in RPython there is an implicit int->float promotion
+ assert f(42) == 42
def getgraph(f, argtypes):
from pypy.translator.translator import TranslationContext, graphof
More information about the pypy-commit
mailing list