This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: pickle.dump and load error message when file isn't opened in binary mode are not useful
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.9
process
Status: closed Resolution: duplicate
Dependencies: Superseder:
Assigned To: Nosy List: boris, serhiy.storchaka
Priority: normal Keywords:

Created on 2019-09-19 20:31 by boris, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (2)
msg352813 - (view) Author: Борис Верховский (boris) * Date: 2019-09-19 20:31
pickle.load() and pickle.dump() take a file object as an argument. That file object must be opened in binary mode ("rb" or "wb"). If it's not, pickle raises a confusing error message.

When pickle.dump()'ing to a file opened in "w" mode instead of "wb" mode it'll raise a TypeError:


TypeError: write() argument must be str, not bytes


I thought it might be getting this because I was pickling bytes objects. 

For pickle.load()'ing from a file opened in "r" mode instead of "rb" mode, it'll raise a UnicodeDecodeError


UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte


There are a couple pages on the internet (just search "pickle" followed by either error message) with people being confused about this. 

pickle should catch these errors and report something more useful.
msg355813 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-11-01 12:18
This looks like a duplicate of issue24159.
History
Date User Action Args
2022-04-11 14:59:20adminsetgithub: 82407
2019-11-01 13:39:00borissetstatus: open -> closed
resolution: duplicate
stage: resolved
2019-11-01 12:18:36serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg355813
2019-11-01 08:19:56borissettype: enhancement
2019-09-19 20:31:01boriscreate