1. Pypy
  2. Untitled project
  3. pypy

Commits

Armin Rigo  committed bc636d3

Fix keyword arguments.

  • Participants
  • Parent commits dc18e93
  • Branches default

Comments (0)

Files changed (2)

File pypy/module/itertools/interp_itertools.py

View file
  • Ignore whitespace
 
 @unwrap_spec(ObjSpace, W_Root, Arguments)
 def W_IZipLongest___new__(space, w_subtype, __args__):
-    kwds = __args__.keywords
+    arguments_w, kwds_w = __args__.unpack()
     w_fillvalue = space.w_None
-    if kwds:
-        if kwds[0] == "fillvalue" and len(kwds) == 1:
-            w_fillvalue = __args__.keywords_w[0]
-        else:
+    if kwds_w:
+        if "fillvalue" in kwds_w:
+            w_fillvalue = kwds_w["fillvalue"]
+            del kwds_w["fillvalue"]
+        if kwds_w:
             raise OperationError(space.w_TypeError, space.wrap(
-                "izip_longest() got unexpected keyword argument"))
+                "izip_longest() got unexpected keyword argument(s)"))
 
     self = space.allocate_instance(W_IZipLongest, w_subtype)
-    self.__init__(space, space.w_None, __args__.arguments_w)
+    self.__init__(space, space.w_None, arguments_w)
     self.w_fillvalue = w_fillvalue
     self.active = len(self.iterators_w)
 
 
     def __init__(self, space, args_w, w_repeat):
         self.space = space
-        self.gears_w = [x for x in args_w] * space.int_w(w_repeat)
+        self.gears_w = args_w * space.int_w(w_repeat)
         self.num_gears = len(self.gears_w)
         # initialization of indicies to loop over
         self.indicies = [(0, space.int_w(space.len(w_gear)))
         return self.space.newtuple(l)
 
 
-def W_Product__new__(space, w_subtype, args_w):
-    star_args_w, kw_args_w = args_w.unpack()
-    if len(kw_args_w) > 1:
-        raise OperationError(space.w_TypeError,
-                             space.wrap("product() takes at most 1 argument (%d given)" %
-                             len(kw_args_w)))
-    w_repeat = kw_args_w.get('repeat', space.wrap(1))
+@unwrap_spec(ObjSpace, W_Root, Arguments)
+def W_Product__new__(space, w_subtype, __args__):
+    arguments_w, kwds_w = __args__.unpack()
+    w_repeat = space.wrap(1)
+    if kwds_w:
+        if 'repeat' in kwds_w:
+            w_repeat = kwds_w['repeat']
+            del kwds_w['repeat']
+        if kwds_w:
+            raise OperationError(space.w_TypeError, space.wrap(
+                "product() got unexpected keyword argument(s)"))
+
     r = space.allocate_instance(W_Product, w_subtype)
-    r.__init__(space, star_args_w[1:], w_repeat)
+    r.__init__(space, arguments_w, w_repeat)
     return space.wrap(r)
 
 W_Product.typedef = TypeDef(
     'product',
-    __new__ = interp2app(W_Product__new__,
-                         unwrap_spec=[ObjSpace, W_Root, Arguments]),
+    __new__ = interp2app(W_Product__new__),
     __iter__ = interp2app(W_Product.iter_w, unwrap_spec=['self']),
     next = interp2app(W_Product.next_w, unwrap_spec=['self']),
     __doc__ = """
             space.wrap("r must be non-negative")
         )
     indices = range(len(pool_w))
-    r = space.allocate_instance(W_Combinations, w_subtype)
-    r.__init__(space, pool_w, indices, r)
-    return space.wrap(r)
+    res = space.allocate_instance(W_Combinations, w_subtype)
+    res.__init__(space, pool_w, indices, r)
+    return space.wrap(res)
 
 W_Combinations.typedef = TypeDef("combinations",
     __new__ = interp2app(W_Combinations__new__),
         raise OperationError(space.w_ValueError,
                              space.wrap("r must be non-negative"))
     indices = [0] * len(pool_w)
-    r = space.allocate_instance(W_CombinationsWithReplacement, w_subtype)
-    r.__init__(space, pool_w, indices, r)
-    return space.wrap(r)
+    res = space.allocate_instance(W_CombinationsWithReplacement, w_subtype)
+    res.__init__(space, pool_w, indices, r)
+    return space.wrap(res)
 
 W_CombinationsWithReplacement.typedef = TypeDef("combinations_with_replacement",
     __new__ = interp2app(W_CombinationsWithReplacement__new__),

File pypy/module/itertools/test/test_itertools.py

View file
  • Ignore whitespace
                (2, 'b', 2, 'b')]
         assert list(prodlist) == ans
 
+        raises(TypeError, product, [], foobar=3)
+
     def test_product_diff_sizes(self):
         from itertools import product
         l = [1, 2]