mirror of
https://github.com/python/cpython.git
synced 2025-10-10 00:43:41 +00:00
save_tuple(): Minor rewriting, and added a comment about the subtlety
created by recursive tuples.
This commit is contained in:
parent
209ad95b00
commit
f558da0f90
1 changed files with 13 additions and 8 deletions
|
@ -454,21 +454,26 @@ class Pickler:
|
||||||
save = self.save
|
save = self.save
|
||||||
memo = self.memo
|
memo = self.memo
|
||||||
|
|
||||||
d = id(object)
|
|
||||||
|
|
||||||
write(MARK)
|
write(MARK)
|
||||||
|
|
||||||
for element in object:
|
for element in object:
|
||||||
save(element)
|
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:
|
if self.bin:
|
||||||
write(POP_MARK + self.get(memo[d][0]))
|
write(POP_MARK + get)
|
||||||
return
|
else: # proto 0 -- POP_MARK not available
|
||||||
|
write(POP * (len(object) + 1) + get)
|
||||||
write(POP * (len(object) + 1) + self.get(memo[d][0]))
|
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# No recursion (including the empty-tuple case).
|
||||||
self.write(TUPLE)
|
self.write(TUPLE)
|
||||||
self.memoize(object)
|
self.memoize(object)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue