[pypy-commit] pypy default: Test and fix.

arigo noreply at buildbot.pypy.org
Mon Dec 5 20:32:30 CET 2011


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r50180:92d4f8f7bcfb
Date: 2011-12-05 20:31 +0100
http://bitbucket.org/pypy/pypy/changeset/92d4f8f7bcfb/

Log:	Test and fix.

diff --git a/pypy/module/_collections/app_defaultdict.py b/pypy/module/_collections/app_defaultdict.py
--- a/pypy/module/_collections/app_defaultdict.py
+++ b/pypy/module/_collections/app_defaultdict.py
@@ -13,12 +13,14 @@
 class defaultdict(dict):
     
     def __init__(self, *args, **kwds):
-        self.default_factory = None
-        if 'default_factory' in kwds:
-            self.default_factory = kwds.pop('default_factory')
-        elif len(args) > 0 and (callable(args[0]) or args[0] is None):
-            self.default_factory = args[0]
+        if len(args) > 0:
+            default_factory = args[0]
             args = args[1:]
+            if not callable(default_factory) and default_factory is not None:
+                raise TypeError("first argument must be callable")
+        else:
+            default_factory = None
+        self.default_factory = default_factory
         super(defaultdict, self).__init__(*args, **kwds)
  
     def __missing__(self, key):
diff --git a/pypy/module/_collections/test/test_defaultdict.py b/pypy/module/_collections/test/test_defaultdict.py
--- a/pypy/module/_collections/test/test_defaultdict.py
+++ b/pypy/module/_collections/test/test_defaultdict.py
@@ -19,11 +19,22 @@
 
     def test_keyerror_without_factory(self):
         from _collections import defaultdict
-        d1 = defaultdict()
-        for key in ['foo', (1,)]:
-            try:
-                d1[key]
-            except KeyError, err:
-                assert err.args[0] == key
-            else:
-                assert 0, "expected KeyError"
+        for d1 in [defaultdict(), defaultdict(None)]:
+            for key in ['foo', (1,)]:
+                try:
+                    d1[key]
+                except KeyError, err:
+                    assert err.args[0] == key
+                else:
+                    assert 0, "expected KeyError"
+
+    def test_noncallable(self):
+        from _collections import defaultdict
+        raises(TypeError, defaultdict, [('a', 5)])
+        d = defaultdict(None, [('a', 5)])
+        assert d.items() == [('a', 5)]
+
+    def test_kwds(self):
+        from _collections import defaultdict
+        d = defaultdict(default_factory=5)
+        assert d.keys() == ['default_factory']


More information about the pypy-commit mailing list