[Python-checkins] r74299 - in python/branches/release26-maint: Lib/test/test_defaultdict.py Misc/NEWS Modules/_collectionsmodule.c

raymond.hettinger python-checkins at python.org
Tue Aug 4 20:49:31 CEST 2009


Author: raymond.hettinger
Date: Tue Aug  4 20:49:26 2009
New Revision: 74299

Log:
Issue 6637: defaultdict.copy() failed with an empty factory.

Modified:
   python/branches/release26-maint/Lib/test/test_defaultdict.py
   python/branches/release26-maint/Misc/NEWS
   python/branches/release26-maint/Modules/_collectionsmodule.c

Modified: python/branches/release26-maint/Lib/test/test_defaultdict.py
==============================================================================
--- python/branches/release26-maint/Lib/test/test_defaultdict.py	(original)
+++ python/branches/release26-maint/Lib/test/test_defaultdict.py	Tue Aug  4 20:49:26 2009
@@ -59,6 +59,7 @@
         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 @@
         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)

Modified: python/branches/release26-maint/Misc/NEWS
==============================================================================
--- python/branches/release26-maint/Misc/NEWS	(original)
+++ python/branches/release26-maint/Misc/NEWS	Tue Aug  4 20:49:26 2009
@@ -70,6 +70,10 @@
 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.
 

Modified: python/branches/release26-maint/Modules/_collectionsmodule.c
==============================================================================
--- python/branches/release26-maint/Modules/_collectionsmodule.c	(original)
+++ python/branches/release26-maint/Modules/_collectionsmodule.c	Tue Aug  4 20:49:26 2009
@@ -1191,6 +1191,9 @@
 	   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 @@
 		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;


More information about the Python-checkins mailing list