]> granicus.if.org Git - python/commitdiff
Issue 6637: defaultdict.copy() failed with an empty factory.
authorRaymond Hettinger <python@rcn.com>
Tue, 4 Aug 2009 18:49:26 +0000 (18:49 +0000)
committerRaymond Hettinger <python@rcn.com>
Tue, 4 Aug 2009 18:49:26 +0000 (18:49 +0000)
Lib/test/test_defaultdict.py
Misc/NEWS
Modules/_collectionsmodule.c

index 5935e052f9ab1b1c59dcb7afcac9fc1dc4ab049e..7ad239239a1b40ada9aa7168081e203fa4780d97 100644 (file)
@@ -59,6 +59,7 @@ class TestDefaultDict(unittest.TestCase):
         d1 = defaultdict()
         self.assertEqual(d1.default_factory, None)
         self.assertEqual(repr(d1), "defaultdict(None, {})")
+        self.assertEqual(eval(repr(d1)), d1)
         d1[11] = 41
         self.assertEqual(repr(d1), "defaultdict(None, {11: 41})")
         d2 = defaultdict(int)
@@ -111,6 +112,12 @@ class TestDefaultDict(unittest.TestCase):
         d4[12]
         self.assertEqual(d4, {42: [], 12: []})
 
+        # Issue 6637: Copy fails for empty default dict
+        d = defaultdict()
+        d['a'] = 42
+        e = d.copy()
+        self.assertEqual(e['a'], 42)
+
     def test_shallow_copy(self):
         d1 = defaultdict(foobar, {1: 1})
         d2 = copy.copy(d1)
index a9cb534f66b1d5888295139cc1c51a2ba339d354..289ee1d678da349ae792ae13d0a15dbb24d45fa9 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -70,6 +70,10 @@ Core and Builtins
 Library
 -------
 
+- Issue #6637: defaultdict.copy() did not work when the default factory
+  was left unspecified.  Also, the eval/repr round-trip would fail when
+  the default_factory was None.
+
 - Issue #1424152: Fix for httplib, urllib2 to support SSL while working through
   proxy. Original patch by Christopher Li, changes made by Senthil Kumaran.
 
index 002953203247cf61490170f5c45e43655156b4f4..faf033e37c9ed73b49cd29feb43fb07f72899e07 100644 (file)
@@ -1191,6 +1191,9 @@ defdict_copy(defdictobject *dd)
           whose class constructor has the same signature.  Subclasses that
           define a different constructor signature must override copy().
        */
+
+       if (dd->default_factory == NULL)
+               return PyObject_CallFunctionObjArgs((PyObject*)Py_TYPE(dd), Py_None, dd, NULL);
        return PyObject_CallFunctionObjArgs((PyObject*)Py_TYPE(dd),
                                            dd->default_factory, dd, NULL);
 }
@@ -1354,7 +1357,7 @@ defdict_init(PyObject *self, PyObject *args, PyObject *kwds)
                Py_ssize_t n = PyTuple_GET_SIZE(args);
                if (n > 0) {
                        newdefault = PyTuple_GET_ITEM(args, 0);
-                       if (!PyCallable_Check(newdefault)) {
+                       if (!PyCallable_Check(newdefault) && newdefault != Py_None) {
                                PyErr_SetString(PyExc_TypeError,
                                        "first argument must be callable");                           
                                return -1;