]> granicus.if.org Git - python/commitdiff
save_dict(): Untangled most of the bin-vs-not-bin logic. Also used
authorTim Peters <tim.peters@gmail.com>
Tue, 28 Jan 2003 01:34:43 +0000 (01:34 +0000)
committerTim Peters <tim.peters@gmail.com>
Tue, 28 Jan 2003 01:34:43 +0000 (01:34 +0000)
iteritems() instead of materializing a (possibly giant) list of the
items.

Lib/pickle.py

index f8ecad592cf01065d91f823098fe40474eecbcb9..957a87fdba2c52ea48ae72d1eea85e0a7b431981 100644 (file)
@@ -508,29 +508,28 @@ class Pickler:
     def save_dict(self, object):
         write = self.write
         save  = self.save
+        items = object.iteritems()
 
         if self.bin:
             write(EMPTY_DICT)
-        else:
-            write(MARK + DICT)
-
-        self.memoize(object)
-
-        using_setitems = (self.bin and (len(object) > 1))
+            self.memoize(object)
+            if len(object) > 1:
+                write(MARK)
+                for key, value in items:
+                    save(key)
+                    save(value)
+                write(SETITEMS)
+                return
 
-        if using_setitems:
-            write(MARK)
+        else:   # proto 0 -- can't use EMPTY_DICT or SETITEMS
+            write(MARK + DICT)
+            self.memoize(object)
 
-        items = object.items()
+        # proto 0 or len(object) < 2
         for key, value in items:
             save(key)
             save(value)
-
-            if not using_setitems:
-                write(SETITEM)
-
-        if using_setitems:
-            write(SETITEMS)
+            write(SETITEM)
 
     dispatch[DictionaryType] = save_dict
     if not PyStringMap is None: