classification
Title: repr() on detached stream objects fails
Type: behavior Stage: resolved
Components: IO Versions: Python 3.5, Python 3.4, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, hynek, martin.panter, pitrou, python-dev, serhiy.storchaka, stutzbach
Priority: normal Keywords: patch

Created on 2014-12-20 14:19 by martin.panter, last changed 2014-12-22 03:01 by python-dev. This issue is now closed.

Files
File name Uploaded Description Edit
detach-indep.patch martin.panter, 2014-12-20 14:19 review
detach-indep.v2.patch martin.panter, 2014-12-21 11:17 review
Messages (6)
msg232971 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2014-12-20 14:19
Patch to fix the underlying issue I mentioned in msg230955. After calling detach() on one of the BufferedIOBase wrappers or a TextIOWrapper, most operations will raise an exception. My patch ensures the following operations are still usable, because they are documented and it doesn’t make sense to disable them:

repr(stream)
stream.encoding
stream.errors
stream.line_buffering
msg232973 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-12-20 16:42
The issue is still here.

>>> f = open('/dev/null')
>>> f
<_io.TextIOWrapper name='/dev/null' mode='r' encoding='UTF-8'>
>>> f.buffer.detach()
<_io.FileIO name='/dev/null' mode='rb' closefd=True>
>>> f
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: raw stream has been detached

Python implementation works.

>>> import _pyio
>>> f = _pyio.open('/dev/null')
>>> f
<_pyio.TextIOWrapper name='/dev/null' mode='r' encoding='UTF-8'>
>>> f.buffer.detach()
<_io.FileIO name='/dev/null' mode='rb' closefd=True>
>>> f
<_pyio.TextIOWrapper mode='r' encoding='UTF-8'>
>>> f = _pyio.open('/dev/null')
>>> f.detach()
<_pyio.BufferedReader name='/dev/null'>
>>> f
<_pyio.TextIOWrapper mode='r' encoding='UTF-8'>
>>> f = _pyio.open('/dev/null', 'rb')
>>> f
<_pyio.BufferedReader name='/dev/null'>
>>> f.detach()
<_io.FileIO name='/dev/null' mode='rb' closefd=True>
>>> f
<_pyio.BufferedReader>

I would be good to make Python and C implementation match.
msg232983 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2014-12-20 22:01
Damn, detaching the intermediate buffered stream is a bit more awkward. The difference between the “io” and “_pyio” implementations boils down to:

* io.BufferedReader/Writer/RWPair.name properties raise a ValueError if the stream is detached
* _pyio._BufferedIOMixin.name property returns “self.raw.name”. When detached, “self.raw” is None, so this causes an AttributeError.

This is significant because io.TextIOWrapper.__repr__() only handles AttributeError when accessing “self.buffer.name”. The best option that I can think of to fix this is to make all the repr() implementations handle this ValueError exception.
msg232997 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2014-12-21 11:17
Here is patch v2, which ignores any exception derived from the Exception base class when reading the self.name etc properties. I’m interested what people think of this approach.
msg232998 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-12-21 14:17
It looks reasonable to me.
msg233006 - (view) Author: Roundup Robot (python-dev) Date: 2014-12-22 03:01
New changeset f3ff3e424b6f by Benjamin Peterson in branch '3.4':
allow more operations to work on detached streams (closes #23093)
https://hg.python.org/cpython/rev/f3ff3e424b6f

New changeset afa8d8ab0937 by Benjamin Peterson in branch '2.7':
allow more operations to work on detached streams (closes #23093)
https://hg.python.org/cpython/rev/afa8d8ab0937

New changeset f2cfa8a348dd by Benjamin Peterson in branch 'default':
merge 3.4 (#23093)
https://hg.python.org/cpython/rev/f2cfa8a348dd
History
Date User Action Args
2014-12-22 03:01:25python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg233006

resolution: fixed
stage: patch review -> resolved
2014-12-21 14:17:16serhiy.storchakasetmessages: + msg232998
2014-12-21 11:17:11martin.pantersetfiles: + detach-indep.v2.patch

messages: + msg232997
2014-12-20 22:01:00martin.pantersetmessages: + msg232983
2014-12-20 16:42:27serhiy.storchakasetversions: + Python 2.7, Python 3.5
nosy: + hynek, stutzbach, pitrou, serhiy.storchaka, benjamin.peterson

messages: + msg232973

stage: patch review
2014-12-20 14:19:49martin.pantercreate