summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2003-01-28 02:09:55 (GMT)
committerTim Peters <tim.peters@gmail.com>2003-01-28 02:09:55 (GMT)
commitf558da0f905d8598b05bed0a5046ca72efe1f5af (patch)
treeef03e37ded195254979c282755e058bf2e2b62af
parent209ad95b000ccabd75972c1b2281aeefad4bc354 (diff)
downloadcpython-f558da0f905d8598b05bed0a5046ca72efe1f5af.zip
cpython-f558da0f905d8598b05bed0a5046ca72efe1f5af.tar.gz
cpython-f558da0f905d8598b05bed0a5046ca72efe1f5af.tar.bz2
save_tuple(): Minor rewriting, and added a comment about the subtlety
created by recursive tuples.
-rw-r--r--Lib/pickle.py21
1 files changed, 13 insertions, 8 deletions
diff --git a/Lib/pickle.py b/Lib/pickle.py
index 62f7a58..25a5a55 100644
--- a/Lib/pickle.py
+++ b/Lib/pickle.py
@@ -454,21 +454,26 @@ class Pickler:
save = self.save
memo = self.memo
- d = id(object)
-
write(MARK)
-
for element in object:
save(element)
- if len(object) and d in memo:
+ if object and id(object) in memo:
+ # Subtle. d was not in memo when we entered save_tuple(), so
+ # the process of saving the tuple's elements must have saved
+ # the tuple itself: the tuple is recursive. The proper action
+ # now is to throw away everything we put on the stack, and
+ # simply GET the tuple (it's already constructed). This check
+ # could have been done in the "for element" loop instead, but
+ # recursive tuples are a rare thing.
+ get = self.get(memo[id(object)][0])
if self.bin:
- write(POP_MARK + self.get(memo[d][0]))
- return
-
- write(POP * (len(object) + 1) + self.get(memo[d][0]))
+ write(POP_MARK + get)
+ else: # proto 0 -- POP_MARK not available
+ write(POP * (len(object) + 1) + get)
return
+ # No recursion (including the empty-tuple case).
self.write(TUPLE)
self.memoize(object)