[New-bugs-announce] [issue38029] Should io.TextIOWrapper raise an error at instantiation if a StringIO is passed as 'buffer'?

Brian Skinn report at bugs.python.org
Wed Sep 4 12:09:55 EDT 2019


New submission from Brian Skinn <bskinn at alum.mit.edu>:

If I read the docs correctly, io.TextIOWrapper is meant to provide a str-typed interface to an underlying bytes stream.

If a TextIOWrapper is instantiated with the underlying buffer=io.StringIO(), it breaks:

>>> import io
>>> tw = io.TextIOWrapper(io.StringIO())
>>> tw.write(b'abcd\n')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: write() argument must be str, not bytes
>>> tw.write('abcd\n')
5
>>> tw.read()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: string argument expected, got 'bytes'
>>> tw.read(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: underlying read() should have returned a bytes-like object, not 'str'



Would it be better for TextIOWrapper to fail earlier, at instantiation-time, for this kind of (unrecoverably?) broken type mismatch?

----------
components: Library (Lib)
messages: 351139
nosy: bskinn
priority: normal
severity: normal
status: open
title: Should io.TextIOWrapper raise an error at instantiation if a StringIO is passed as 'buffer'?
type: behavior
versions: Python 3.8, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38029>
_______________________________________


More information about the New-bugs-announce mailing list