Author: Daniel Patrick <danieljudepatr...@gmail.com> Branch: py3.5 Changeset: r87224:85eef6bf8b2e Date: 2016-09-19 12:28 +0100 http://bitbucket.org/pypy/pypy/changeset/85eef6bf8b2e/
Log: Implement default keyword on min & max with tests diff --git a/pypy/module/__builtin__/functional.py b/pypy/module/__builtin__/functional.py --- a/pypy/module/__builtin__/functional.py +++ b/pypy/module/__builtin__/functional.py @@ -136,14 +136,24 @@ "%s() expects at least one argument", implementation_of) w_key = None + has_default = False if any_kwds: kwds = args.keywords - if kwds[0] == "key" and len(kwds) == 1: - w_key = args.keywords_w[0] - else: - raise oefmt(space.w_TypeError, - "%s() got unexpected keyword argument", - implementation_of) + for n in range(len(kwds)): + if kwds[n] == "key": + w_key = args.keywords_w[n] + elif kwds[n] == "default": + w_default = args.keywords_w[n] + has_default = True + else: + raise oefmt(space.w_TypeError, + "%s() got unexpected keyword argument", + implementation_of) + + if has_default and len(args_w) > 1: + raise oefmt(space.w_TypeError, + "Cannot specify a default for %s() with multiple positional arguments", + implementation_of) w_iter = space.iter(w_sequence) w_type = space.type(w_iter) @@ -170,7 +180,10 @@ w_max_item = w_item w_max_val = w_compare_with if w_max_item is None: - raise oefmt(space.w_ValueError, "arg is an empty sequence") + if has_default: + w_max_item = w_default + else: + raise oefmt(space.w_ValueError, "arg is an empty sequence") return w_max_item if unroll: min_max_impl = jit.unroll_safe(min_max_impl) diff --git a/pypy/module/__builtin__/test/test_functional.py b/pypy/module/__builtin__/test/test_functional.py --- a/pypy/module/__builtin__/test/test_functional.py +++ b/pypy/module/__builtin__/test/test_functional.py @@ -590,6 +590,12 @@ assert type(min(1, 1.0, 1)) is int assert type(min(1.0, 1, 1)) is float assert type(min(1, 1, 1.0)) is int + assert min([], default=-1) == -1 + assert min([1, 2], default=-1) == 1 + raises(TypeError, min, 0, 1, default=-1) + assert min([], default=None) == None + raises(TypeError, min, 1, default=0) + raises(TypeError, min, default=1) def test_max(self): assert max(1, 2) == 2 @@ -602,3 +608,9 @@ assert type(max(1, 1.0, 1)) is int assert type(max(1.0, 1, 1)) is float assert type(max(1, 1, 1.0)) is int + assert max([], default=-1) == -1 + assert max([1, 2], default=3) == 2 + raises(TypeError, min, 0, 1, default=-1) + assert max([], default=None) == None + raises(TypeError, max, 1, default=0) + raises(TypeError, max, default=1) _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit