Limit Pickle protocol to version supported by Python 2.

This commit is contained in:
Itamar Turner-Trauring 2021-06-16 11:53:25 -04:00
parent f39eeb4741
commit 3f98349cd9
4 changed files with 12 additions and 4 deletions

View File

@ -201,7 +201,9 @@ class CPUWatcher(service.MultiService, resource.Resource, Referenceable):
log.msg("error reading process %s (%s), ignoring" % (pid, name)) log.msg("error reading process %s (%s), ignoring" % (pid, name))
log.err() log.err()
try: try:
pickle.dump(self.history, open("history.pickle.tmp", "wb")) # Newer protocols won't work in Python 2; when it is dropped,
# protocol v4 can be used (added in Python 3.4).
pickle.dump(self.history, open("history.pickle.tmp", "wb"), protocol=2)
os.rename("history.pickle.tmp", "history.pickle") os.rename("history.pickle.tmp", "history.pickle")
except: except:
pass pass

0
newsfragments/3741.minor Normal file
View File

View File

@ -252,7 +252,9 @@ class ShareCrawler(service.MultiService):
self.state["last-complete-prefix"] = last_complete_prefix self.state["last-complete-prefix"] = last_complete_prefix
tmpfile = self.statefile + ".tmp" tmpfile = self.statefile + ".tmp"
with open(tmpfile, "wb") as f: with open(tmpfile, "wb") as f:
pickle.dump(self.state, f) # Newer protocols won't work in Python 2; when it is dropped,
# protocol v4 can be used (added in Python 3.4).
pickle.dump(self.state, f, protocol=2)
fileutil.move_into_place(tmpfile, self.statefile) fileutil.move_into_place(tmpfile, self.statefile)
def startService(self): def startService(self):

View File

@ -95,7 +95,9 @@ class LeaseCheckingCrawler(ShareCrawler):
if not os.path.exists(self.historyfile): if not os.path.exists(self.historyfile):
history = {} # cyclenum -> dict history = {} # cyclenum -> dict
with open(self.historyfile, "wb") as f: with open(self.historyfile, "wb") as f:
pickle.dump(history, f) # Newer protocols won't work in Python 2; when it is dropped,
# protocol v4 can be used (added in Python 3.4).
pickle.dump(history, f, protocol=2)
def create_empty_cycle_dict(self): def create_empty_cycle_dict(self):
recovered = self.create_empty_recovered_dict() recovered = self.create_empty_recovered_dict()
@ -319,7 +321,9 @@ class LeaseCheckingCrawler(ShareCrawler):
oldcycles = sorted(history.keys()) oldcycles = sorted(history.keys())
del history[oldcycles[0]] del history[oldcycles[0]]
with open(self.historyfile, "wb") as f: with open(self.historyfile, "wb") as f:
pickle.dump(history, f) # Newer protocols won't work in Python 2; when it is dropped,
# protocol v4 can be used (added in Python 3.4).
pickle.dump(history, f, protocol=2)
def get_state(self): def get_state(self):
"""In addition to the crawler state described in """In addition to the crawler state described in