Author: Antonio Cuni <anto.c...@gmail.com> Branch: py3k Changeset: r56791:b877003ac5a3 Date: 2012-08-22 11:57 +0200 http://bitbucket.org/pypy/pypy/changeset/b877003ac5a3/
Log: hg merge default diff --git a/pypy/rlib/objectmodel.py b/pypy/rlib/objectmodel.py --- a/pypy/rlib/objectmodel.py +++ b/pypy/rlib/objectmodel.py @@ -108,7 +108,7 @@ specialize = _Specialize() -def enforceargs(*types, **kwds): +def enforceargs(*types_, **kwds): """ Decorate a function with forcing of RPython-level types on arguments. None means no enforcing. @@ -117,15 +117,16 @@ typechecking by passing ``typecheck=False`` to @enforceargs. """ typecheck = kwds.pop('typecheck', True) - if kwds: - raise TypeError, 'got an unexpected keyword argument: %s' % kwds.keys() + if types_ and kwds: + raise TypeError, 'Cannot mix positional arguments and keywords' + if not typecheck: def decorator(f): - f._annenforceargs_ = types + f._annenforceargs_ = types_ return f return decorator # - def decorator(f): + def decorator(f): def get_annotation(t): from pypy.annotation.signature import annotation from pypy.annotation.model import SomeObject @@ -167,6 +168,10 @@ # not RPython. Instead, we generate a function with exactly the same # argument list srcargs, srcvarargs, srckeywords, defaults = inspect.getargspec(f) + if kwds: + types = tuple([kwds.get(arg) for arg in srcargs]) + else: + types = types_ assert len(srcargs) == len(types), ( 'not enough types provided: expected %d, got %d' % (len(types), len(srcargs))) 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 @@ -437,6 +437,12 @@ return a+b assert f(2) == 42 +def test_enforceargs_keywords(): + @enforceargs(b=int) + def f(a, b, c): + return a+b + assert f._annenforceargs_ == (None, int, None) + def test_enforceargs_int_float_promotion(): @enforceargs(float) def f(x): _______________________________________________ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit