File tree Expand file tree Collapse file tree 1 file changed +15
-4
lines changed Expand file tree Collapse file tree 1 file changed +15
-4
lines changed Original file line number Diff line number Diff line change @@ -1789,8 +1789,19 @@ def get_pickle_protocol(filename):
1789
1789
"""
1790
1790
try :
1791
1791
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 ):
1796
1807
return None
You can’t perform that action at this time.
0 commit comments