Skip to content

Commit bcf41c9

Browse files
committed
Cleanup exception handling
Cleanup the code so that it only has a single catch for UnicodeDecodeError
1 parent e675f82 commit bcf41c9

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

Diff for: pandas/io/pickle.py

+11-14
Original file line numberDiff line numberDiff line change
@@ -171,25 +171,22 @@ def read_pickle(
171171

172172
# 1) try standard library Pickle
173173
# 2) try pickle_compat (older pandas version) to handle subclass changes
174-
175-
excs_to_catch = (AttributeError, ImportError, ModuleNotFoundError)
174+
# 3) try pickle_compat with latin-1 encoding upon a UnicodeDecodeError
176175

177176
try:
178-
with warnings.catch_warnings(record=True):
179-
# We want to silence any warnings about, e.g. moved modules.
180-
warnings.simplefilter("ignore", Warning)
181-
return pickle.load(f)
182-
except excs_to_catch:
183-
# e.g.
184-
# "No module named 'pandas.core.sparse.series'"
185-
# "Can't get attribute '__nat_unpickle' on <module 'pandas._libs.tslib"
177+
excs_to_catch = (AttributeError, ImportError, ModuleNotFoundError)
186178
try:
179+
with warnings.catch_warnings(record=True):
180+
# We want to silence any warnings about, e.g. moved modules.
181+
warnings.simplefilter("ignore", Warning)
182+
return pickle.load(f)
183+
except excs_to_catch:
184+
# e.g.
185+
# "No module named 'pandas.core.sparse.series'"
186+
# "Can't get attribute '__nat_unpickle' on <module 'pandas._libs.tslib"
187187
return pc.load(f, encoding=None)
188-
except UnicodeDecodeError:
189-
# e.g. can occur for files written in py27; see GH#28645 and GH#31988
190-
return pc.load(f, encoding="latin-1")
191188
except UnicodeDecodeError:
192-
# e.g. can occur for files written in py27; see GH#28645
189+
# e.g. can occur for files written in py27; see GH#28645 and GH#31988
193190
return pc.load(f, encoding="latin-1")
194191
finally:
195192
f.close()

0 commit comments

Comments
 (0)