jenkins-bot has submitted this change and it was merged.
Change subject: [IMPROV] Pickle: Open files in binary mode ......................................................................
[IMPROV] Pickle: Open files in binary mode
With Python 3 files must be open in binary mode to get pickled. This change also adds 'with' statements to all changes. In wikidataquery.py this closes the files.
Change-Id: I155d42aeaaed22365309425d59bfb195cd3eddcf --- M pywikibot/data/wikidataquery.py M scripts/interwiki.py M scripts/watchlist.py M scripts/weblinkchecker.py M scripts/welcome.py 5 files changed, 32 insertions(+), 34 deletions(-)
Approvals: John Vandenberg: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/data/wikidataquery.py b/pywikibot/data/wikidataquery.py index 62f750f..2794892 100644 --- a/pywikibot/data/wikidataquery.py +++ b/pywikibot/data/wikidataquery.py @@ -484,13 +484,13 @@ now = time.time()
if ((now - mtime) / 60) < self.cacheMaxAge: - - try: - data = pickle.load(open(cacheFile, 'r')) - except pickle.UnpicklingError: - pywikibot.warning(u"Couldn't read cached data from %s" - % cacheFile) - data = None + with open(cacheFile, 'rb') as f: + try: + data = pickle.load(f) + except pickle.UnpicklingError: + pywikibot.warning(u"Couldn't read cached data from %s" + % cacheFile) + data = None
return data
@@ -517,10 +517,11 @@ if not os.path.exists(self.cacheDir): os.makedirs(self.cacheDir)
- try: - pickle.dump(data, open(cacheFile, 'w')) - except IOError: - pywikibot.warning(u"Failed to write cache file %s" % cacheFile) + with open(cacheFile, 'wb') as f: + try: + pickle.dump(data, f) + except IOError: + pywikibot.warning(u"Failed to write cache file %s" % cacheFile)
def getDataFromHost(self, queryStr): """ diff --git a/scripts/interwiki.py b/scripts/interwiki.py index 85394d7..ae17496 100755 --- a/scripts/interwiki.py +++ b/scripts/interwiki.py @@ -356,6 +356,9 @@ import pywikibot from pywikibot import config, i18n, pagegenerators, textlib, interwiki_graph, titletranslate
+if sys.version_info[0] > 2: + unicode = str + docuReplacements = { '&pagegenerators_help;': pagegenerators.parameterHelp } @@ -2117,10 +2120,9 @@ mode = 'appended' else: mode = 'written' - f = open(dumpfn, mode[0]) titles = [s.originPage.title() for s in self.subjects] - pickle.dump(titles, f) - f.close() + with open(dumpfn, mode[0] + 'b') as f: + pickle.dump(titles, f) pywikibot.output(u'Dump %s (%s) %s.' % (site.lang, site.family.name, mode)) return dumpfn
@@ -2526,9 +2528,8 @@ u'%s-%s.pickle' % (site.family.name, site.lang) ) try: - f = open(dumpFileName, 'r') - dumpedTitles = pickle.load(f) - f.close() + with open(dumpFileName, 'rb') as f: + dumpedTitles = pickle.load(f) except (EOFError, IOError): dumpedTitles = [] pages = [pywikibot.Page(site, title) for title in dumpedTitles] diff --git a/scripts/watchlist.py b/scripts/watchlist.py index 31236ad..fcb2200 100755 --- a/scripts/watchlist.py +++ b/scripts/watchlist.py @@ -57,9 +57,8 @@ except OSError: # no saved watchlist exists yet, retrieve one refresh(site) - f = open(fn, 'r') - watchlist = pickle.load(f) - f.close() + with open(fn, 'rb') as f: + watchlist = pickle.load(f) # create cached copy cache[site] = watchlist return watchlist @@ -100,13 +99,12 @@
# Save the watchlist to disk # The file is stored in the watchlists subdir. Create if necessary. - f = open(config.datafilepath('watchlists', - 'watchlist-%s-%s%s.dat' - % (site.family.name, site.code, - '-sysop' if sysop else '')), - 'w') - pickle.dump(watchlist, f) - f.close() + with open(config.datafilepath('watchlists', + 'watchlist-%s-%s%s.dat' + % (site.family.name, site.code, + '-sysop' if sysop else '')), + 'wb') as f: + pickle.dump(watchlist, f)
def refresh_all(new=False, sysop=False): diff --git a/scripts/weblinkchecker.py b/scripts/weblinkchecker.py index 262d695..5d1158e 100644 --- a/scripts/weblinkchecker.py +++ b/scripts/weblinkchecker.py @@ -543,9 +543,8 @@ # from time to time self.logCount = 0 try: - datfile = open(self.datfilename, 'r') - self.historyDict = pickle.load(datfile) - datfile.close() + with open(self.datfilename, 'rb') as datfile: + self.historyDict = pickle.load(datfile) except (IOError, EOFError): # no saved history exists yet, or history dump broken self.historyDict = {} @@ -621,9 +620,8 @@
def save(self): """ Save the .dat file to disk. """ - datfile = open(self.datfilename, 'w') - pickle.dump(self.historyDict, datfile) - datfile.close() + with open(self.datfilename, 'wb') as datfile: + pickle.dump(self.historyDict, datfile)
class DeadLinkReportThread(threading.Thread): diff --git a/scripts/welcome.py b/scripts/welcome.py index 1de697e..c603eba 100644 --- a/scripts/welcome.py +++ b/scripts/welcome.py @@ -996,7 +996,7 @@ import pickle as cPickle else: import cPickle - with open(filename, 'w') as f: + with open(filename, 'wb') as f: cPickle.dump(bot.welcomed_users, f)
if __name__ == "__main__":
pywikibot-commits@lists.wikimedia.org