[pypy-svn] r68378 - in pypy/branch/improve-kwd-args/pypy/interpreter: . test
pedronis at codespeak.net
pedronis at codespeak.net
Tue Oct 13 15:25:10 CEST 2009
Author: pedronis
Date: Tue Oct 13 15:25:09 2009
New Revision: 68378
Modified:
pypy/branch/improve-kwd-args/pypy/interpreter/argument.py
pypy/branch/improve-kwd-args/pypy/interpreter/test/test_argument.py
Log:
(cfbolz, pedronis): write unit tests for the error productions. aren't we nice.
Modified: pypy/branch/improve-kwd-args/pypy/interpreter/argument.py
==============================================================================
--- pypy/branch/improve-kwd-args/pypy/interpreter/argument.py (original)
+++ pypy/branch/improve-kwd-args/pypy/interpreter/argument.py Tue Oct 13 15:25:09 2009
@@ -219,7 +219,7 @@
scope_w[co_argcount] = self.space.newtuple(starargs_w)
elif avail > co_argcount:
raise ArgErrCount(avail, num_kwds,
- (co_argcount, has_vararg, has_kwarg),
+ co_argcount, has_vararg, has_kwarg,
defaults_w, 0)
# collect extra keyword arguments into the **kwarg
@@ -236,7 +236,7 @@
if missing:
raise ArgErrCount(avail, num_kwds,
- (co_argcount, has_vararg, has_kwarg),
+ co_argcount, has_vararg, has_kwarg,
defaults_w, missing)
return co_argcount + has_vararg + has_kwarg
@@ -459,28 +459,31 @@
class ArgErrCount(ArgErr):
- def __init__(self, nargs, nkwds, signature, defaults_w, missing_args):
- self.signature = signature
+ def __init__(self, got_nargs, nkwds, expected_nargs, has_vararg, has_kwarg,
+ defaults_w, missing_args):
+ self.expected_nargs = expected_nargs
+ self.has_vararg = has_vararg
+ self.has_kwarg = has_kwarg
+
self.num_defaults = len(defaults_w)
self.missing_args = missing_args
- self.num_args = nargs
+ self.num_args = got_nargs
self.num_kwds = nkwds
def getmsg(self, fnname):
args = None
- num_args, has_vararg, has_kwarg = self.signature
#args_w, kwds_w = args.unpack()
- if has_kwarg or (self.num_kwds and self.num_defaults):
+ if self.has_kwarg or (self.num_kwds and self.num_defaults):
msg2 = "non-keyword "
if self.missing_args:
- required_args = num_args - self.num_defaults
+ required_args = self.expected_nargs - self.num_defaults
nargs = required_args - self.missing_args
else:
nargs = self.num_args
else:
msg2 = ""
nargs = self.num_args + self.num_kwds
- n = num_args
+ n = self.expected_nargs
if n == 0:
msg = "%s() takes no %sargument (%d given)" % (
fnname,
@@ -488,7 +491,7 @@
nargs)
else:
defcount = self.num_defaults
- if defcount == 0 and not has_vararg:
+ if defcount == 0 and not self.has_vararg:
msg1 = "exactly"
elif not self.missing_args:
msg1 = "at most"
Modified: pypy/branch/improve-kwd-args/pypy/interpreter/test/test_argument.py
==============================================================================
--- pypy/branch/improve-kwd-args/pypy/interpreter/test/test_argument.py (original)
+++ pypy/branch/improve-kwd-args/pypy/interpreter/test/test_argument.py Tue Oct 13 15:25:09 2009
@@ -1,6 +1,7 @@
import py
from pypy.interpreter.argument import Arguments, ArgumentsForTranslation, ArgErr
-from pypy.interpreter.argument import ArgErrUnknownKwds, rawshape
+from pypy.interpreter.argument import ArgErrUnknownKwds, ArgErrMultipleValues
+from pypy.interpreter.argument import ArgErrCount, rawshape
from pypy.interpreter.error import OperationError
@@ -394,6 +395,46 @@
assert args.keywords_w[args.keywords.index('b')] == 3
+class TestErrorHandling(object):
+ def test_missing_args(self):
+ # got_nargs, nkwds, expected_nargs, has_vararg, has_kwarg,
+ # defaults_w, missing_args
+ err = ArgErrCount(1, 0, 0, False, False, [], 0)
+ s = err.getmsg('foo')
+ assert s == "foo() takes no argument (1 given)"
+ err = ArgErrCount(0, 0, 1, False, False, [], 1)
+ s = err.getmsg('foo')
+ assert s == "foo() takes exactly 1 argument (0 given)"
+ err = ArgErrCount(3, 0, 2, False, False, [], 0)
+ s = err.getmsg('foo')
+ assert s == "foo() takes exactly 2 arguments (3 given)"
+ err = ArgErrCount(1, 0, 2, True, False, [], 1)
+ s = err.getmsg('foo')
+ assert s == "foo() takes at least 2 arguments (1 given)"
+ err = ArgErrCount(3, 0, 2, True, False, ['a'], 0)
+ s = err.getmsg('foo')
+ assert s == "foo() takes at most 2 arguments (3 given)"
+ err = ArgErrCount(0, 1, 2, True, False, ['a'], 1)
+ s = err.getmsg('foo')
+ assert s == "foo() takes at least 1 non-keyword argument (0 given)"
+ err = ArgErrCount(2, 1, 1, False, True, [], 0)
+ s = err.getmsg('foo')
+ assert s == "foo() takes exactly 1 non-keyword argument (2 given)"
+
+
+ def test_unknown_keywords(self):
+ err = ArgErrUnknownKwds(1, ['a', 'b'], [True, False])
+ s = err.getmsg('foo')
+ assert s == "foo() got an unexpected keyword argument 'b'"
+ err = ArgErrUnknownKwds(2, ['a', 'b', 'c'], [True, False, False])
+ s = err.getmsg('foo')
+ assert s == "foo() got 2 unexpected keyword arguments"
+
+ def test_multiple_values(self):
+ err = ArgErrMultipleValues('bla')
+ s = err.getmsg('foo')
+ assert s == "foo() got multiple values for keyword argument 'bla'"
+
def make_arguments_for_translation(space, args_w, keywords_w={},
w_stararg=None, w_starstararg=None):
return ArgumentsForTranslation(space, args_w, keywords_w.keys(),
@@ -402,6 +443,15 @@
class TestArgumentsForTranslation(object):
+ def test_prepend(self):
+ space = DummySpace()
+ args = ArgumentsForTranslation(space, ["0"])
+ args1 = args.prepend("thingy")
+ assert args1 is not args
+ assert args1.arguments_w == ["thingy", "0"]
+ assert args1.keywords is args.keywords
+ assert args1.keywords_w is args.keywords_w
+
def test_unmatch_signature(self):
space = DummySpace()
args = make_arguments_for_translation(space, [1,2,3])
@@ -495,6 +545,15 @@
w_starstararg={'e': 5, 'd': 7})
assert rawshape(args) == (2, ('g', ), True, True)
+ def test_copy_and_shape(self):
+ space = DummySpace()
+ args = ArgumentsForTranslation(space, ['a'], ['x'], [1],
+ ['w1'], {'y': 'w2'})
+ args1 = args.copy()
+ args.combine_if_necessary()
+ assert rawshape(args1) == (1, ('x',), True, True)
+
+
def test_flatten(self):
space = DummySpace()
args = make_arguments_for_translation(space, [1,2,3])
@@ -577,3 +636,4 @@
shape = ((2, ('g', ), True, True), [1, 2, 9, [3, 4, 5], {'e': 5, 'd': 7}])
args = ArgumentsForTranslation.fromshape(space, *shape)
assert args.flatten() == shape
+
More information about the Pypy-commit
mailing list