From fae355fe6581dd6e38c81f9d66628ac1f1ee165a Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 15 Mar 2019 15:38:12 +0530 Subject: [PATCH] Replace use of pickle for icon theme cache on Linux --- src/calibre/utils/open_with/linux.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/calibre/utils/open_with/linux.py b/src/calibre/utils/open_with/linux.py index 39b314bce7..b7c150c669 100644 --- a/src/calibre/utils/open_with/linux.py +++ b/src/calibre/utils/open_with/linux.py @@ -6,13 +6,14 @@ from __future__ import (unicode_literals, division, absolute_import, __license__ = 'GPL v3' __copyright__ = '2015, Kovid Goyal ' -import re, shlex, os, cPickle +import re, shlex, os from collections import defaultdict from calibre import walk, guess_type, prints, force_unicode from calibre.constants import filesystem_encoding, cache_dir from calibre.utils.icu import numeric_sort_key as sort_key from calibre.utils.localization import canonicalize_lang, get_lang +from calibre.utils.serialize import msgpack_dumps, msgpack_loads from polyglot.builtins import string_or_bytes @@ -90,7 +91,7 @@ def find_icons(): '/usr/share/pixmaps'] ans = defaultdict(list) sz_pat = re.compile(r'/((?:\d+x\d+)|scalable)/') - cache_file = os.path.join(cache_dir(), 'icon-theme-cache.pickle') + cache_file = os.path.join(cache_dir(), 'icon-theme-cache.calibre_msgpack') exts = {'.svg', '.png', '.xpm'} def read_icon_theme_dir(dirpath): @@ -114,8 +115,9 @@ def find_icons(): try: with open(cache_file, 'rb') as f: - cache = cPickle.load(f) - mtimes, cache = cache['mtimes'], cache['data'] + cache = f.read() + cache = msgpack_loads(cache) + mtimes, cache = cache['mtimes'], cache['data'] except Exception: mtimes, cache = defaultdict(int), defaultdict(dict) @@ -151,9 +153,10 @@ def find_icons(): changed = True if changed: + data = msgpack_dumps({'data':cache, 'mtimes':mtimes}) try: with open(cache_file, 'wb') as f: - cPickle.dump({'data':cache, 'mtimes':mtimes}, f, -1) + f.write(data) except Exception: import traceback traceback.print_exc()