Skip to content

Commit 5f2368e

Browse files
committed
Fix get_pickle_protocol to properly detect protocol version from file header
1 parent f1158d8 commit 5f2368e

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

Mailman/Utils.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1789,8 +1789,19 @@ def get_pickle_protocol(filename):
17891789
"""
17901790
try:
17911791
with open(filename, 'rb') as fp:
1792-
# Try to load the pickle file to get its protocol version
1793-
data = pickle.load(fp, fix_imports=True, encoding='latin1')
1794-
return pickle.format_version
1795-
except (IOError, pickle.UnpicklingError):
1792+
# Read the first byte to determine protocol version
1793+
first_byte = fp.read(1)
1794+
if not first_byte:
1795+
return None
1796+
# The first byte of a pickle file indicates the protocol version
1797+
# For protocol 0, it's '0', for protocol 1 it's '1', etc.
1798+
# For protocol 2 and higher, it's a binary value
1799+
if first_byte[0] == ord('0'):
1800+
return 0
1801+
elif first_byte[0] == ord('1'):
1802+
return 1
1803+
else:
1804+
# For protocol 2 and higher, the first byte is the protocol number
1805+
return first_byte[0]
1806+
except (IOError, IndexError):
17961807
return None

0 commit comments

Comments
 (0)