[New-bugs-announce] [issue24159] Misleading TypeError when pickling bytes to a file opened as text

Jason R. Coombs report at bugs.python.org
Sun May 10 16:34:20 CEST 2015


New submission from Jason R. Coombs:

I had a piece of code which I distilled to this:

import pickle
with open('out.pickle', 'w') as out:
    pickle.dump(out, b'data')

Running that code raises this error:

TypeError: must be str, not bytes

The error is raised at the dump call with no additional context. Based on the error, my reaction is to think that pickled doesn't support bytes objects in pickles.

On further examination, it's not actually that the bytes cannot be pickled, but that the 'dump' call requires that the file be opened in binary mode ('wb'), but because of the error message essentially says "expecting a text string" and because it's unclear that the error is raised during the write to the stream and because the JSON library expects an output stream to be opened in text mode, the error message is misleading.

At least [two other people think the behavior could be clearer](http://stackoverflow.com/questions/13906623/using-pickle-dump-typeerror-must-be-str-not-bytes#comment43761528_13906715).

Would it be possible and reasonable to trap a TypeError at the call to `.write` and replace or augment the message with something like "file must be opened in binary mode"?

On second thought, perhaps the culprit isn't pickle here, but the stream writer. Perhaps the `.write` method should provide a clearer message indicating the context at which it's expecting str and not bytes.

----------
components: IO
messages: 242858
nosy: jason.coombs
priority: low
severity: normal
status: open
title: Misleading TypeError when pickling bytes to a file opened as text
type: behavior
versions: Python 3.5

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue24159>
_______________________________________


More information about the New-bugs-announce mailing list