MCPcopy Index your code
hub / github.com/python/cpython / save_tuple

Method save_tuple

Lib/pickle.py:965–1020  ·  view source on GitHub ↗
(self, obj)

Source from the content-addressed store, hash-verified

963 dispatch[str] = save_str
964
965 def save_tuple(self, obj):
966 if not obj: # tuple is empty
967 if self.bin:
968 self.write(EMPTY_TUPLE)
969 else:
970 self.write(MARK + TUPLE)
971 return
972
973 n = len(obj)
974 save = self.save
975 memo = self.memo
976 if n <= 3 and self.proto >= 2:
977 for i, element in enumerate(obj):
978 try:
979 save(element)
980 except BaseException as exc:
981 exc.add_note(f'when serializing {_T(obj)} item {i}')
982 raise
983 # Subtle. Same as in the big comment below.
984 if id(obj) in memo:
985 get = self.get(memo[id(obj)][0])
986 self.write(POP * n + get)
987 else:
988 self.write(_tuplesize2code[n])
989 self.memoize(obj)
990 return
991
992 # proto 0 or proto 1 and tuple isn't empty, or proto > 1 and tuple
993 # has more than 3 elements.
994 write = self.write
995 write(MARK)
996 for i, element in enumerate(obj):
997 try:
998 save(element)
999 except BaseException as exc:
1000 exc.add_note(f'when serializing {_T(obj)} item {i}')
1001 raise
1002
1003 if id(obj) in memo:
1004 # Subtle. d was not in memo when we entered save_tuple(), so
1005 # the process of saving the tuple's elements must have saved
1006 # the tuple itself: the tuple is recursive. The proper action
1007 # now is to throw away everything we put on the stack, and
1008 # simply GET the tuple (it's already constructed). This check
1009 # could have been done in the "for element" loop instead, but
1010 # recursive tuples are a rare thing.
1011 get = self.get(memo[id(obj)][0])
1012 if self.bin:
1013 write(POP_MARK + get)
1014 else: # proto 0 -- POP_MARK not available
1015 write(POP * (n+1) + get)
1016 return
1017
1018 # No recursion.
1019 write(TUPLE)
1020 self.memoize(obj)
1021
1022 dispatch[tuple] = save_tuple

Callers

nothing calls this directly

Calls 7

getMethod · 0.95
memoizeMethod · 0.95
enumerateFunction · 0.85
_TFunction · 0.85
idFunction · 0.85
writeFunction · 0.70
writeMethod · 0.45

Tested by

no test coverage detected