Skip to content

Commit 476e92d

Browse files
committed
Improve Python 2 compatibility in bin/check_db by trying UTF-8 then latin1 encoding
1 parent 642baf8 commit 476e92d

1 file changed

Lines changed: 8 additions & 12 deletions

File tree

bin/check_db

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,29 +82,25 @@ def testfile(dbfile, listname=None, verbose=0):
8282
# Try to load the pickle file
8383
try:
8484
with open(dbfile, 'rb') as fp:
85-
# First try to detect Python 2 pickle
85+
# Try loading with UTF-8 first, then fall back to latin1
8686
try:
8787
fp.seek(0)
88-
header = fp.read(2)
89-
is_py2_pickle = header.startswith(b'c') or header.startswith(b'C')
88+
data = pickle.load(fp, fix_imports=True, encoding='utf-8')
9089
if verbose:
91-
print(' Python 2 pickle detected: %s' % ('Yes' if is_py2_pickle else 'No'))
92-
except:
93-
is_py2_pickle = False
90+
print(' Successfully loaded with UTF-8 encoding')
91+
except UnicodeDecodeError:
92+
fp.seek(0)
93+
data = pickle.load(fp, fix_imports=True, encoding='latin1')
94+
if verbose:
95+
print(' Successfully loaded with latin1 encoding')
9496

95-
# Now load the actual data
96-
fp.seek(0)
97-
data = pickle.load(fp, fix_imports=True, encoding='latin1')
9897
if verbose:
9998
# Get pickle version info
10099
fp.seek(0)
101100
version = pickle.format_version
102101
protocol = pickle.HIGHEST_PROTOCOL
103102
print(' Pickle format version: %s' % version)
104103
print(' Pickle protocol: %d' % protocol)
105-
if is_py2_pickle:
106-
print(' WARNING: This file was likely written with Python 2')
107-
print(' String data may need special handling for Python 3 compatibility')
108104
except (EOFError, pickle.UnpicklingError) as e:
109105
print(' Error loading file %s for list %s: %s' %
110106
(os.path.basename(dbfile), listname or 'unknown', str(e)))

0 commit comments

Comments
 (0)