[issue24159] Misleading TypeError when pickling bytes to a file opened as text

2019-11-02 Thread stein-k


Change by stein-k :


--
nosy: +stein-k

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue24159] Misleading TypeError when pickling bytes to a file opened as text

2019-11-01 Thread Борис Верховский

Борис Верховский  added the comment:

As I said in issue38226, the error message you get when you try to 
pickle.load() a file opened in "r" mode instead of "rb" mode,

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

is also confusing and can be improved.

--
nosy: +boris

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue24159] Misleading TypeError when pickling bytes to a file opened as text

2018-02-19 Thread Serhiy Storchaka

Change by Serhiy Storchaka :


--
versions: +Python 3.8 -Python 3.7

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue24159] Misleading TypeError when pickling bytes to a file opened as text

2017-02-19 Thread Serhiy Storchaka

Serhiy Storchaka added the comment:

I think it is worth to improve the error message in the write() method of 
binary files.

>>> sys.stdout.write(b'')
Traceback (most recent call last):
  File "", line 1, in 
TypeError: write() argument must be str, not bytes
>>> sys.stdout.buffer.write('')
Traceback (most recent call last):
  File "", line 1, in 
TypeError: a bytes-like object is required, not 'str'
>>> sys.stdout.buffer.raw.write('')
Traceback (most recent call last):
  File "", line 1, in 
TypeError: a bytes-like object is required, not 'str'

But this is large issue. Other file-like objects (GzipFile, ZipExtFile etc) 
should be updated too.

--
nosy: +serhiy.storchaka
type: behavior -> enhancement
versions: +Python 3.7 -Python 3.5

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue24159] Misleading TypeError when pickling bytes to a file opened as text

2015-05-11 Thread Ned Deily

Changes by Ned Deily n...@acm.org:


--
nosy: +alexandre.vassalotti, pitrou

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue24159
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue24159] Misleading TypeError when pickling bytes to a file opened as text

2015-05-10 Thread Jon Clements

Changes by Jon Clements jon...@googlemail.com:


--
nosy: +joncle

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue24159
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue24159] Misleading TypeError when pickling bytes to a file opened as text

2015-05-10 Thread Jason R. Coombs

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 rep...@bugs.python.org
http://bugs.python.org/issue24159
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com