New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
repr() on detached stream objects fails #67282
Comments
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 |
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. |
Damn, detaching the intermediate buffered stream is a bit more awkward. The difference between the “io” and “_pyio” implementations boils down to:
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. |
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. |
It looks reasonable to me. |
New changeset f3ff3e424b6f by Benjamin Peterson in branch '3.4': New changeset afa8d8ab0937 by Benjamin Peterson in branch '2.7': New changeset f2cfa8a348dd by Benjamin Peterson in branch 'default': |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: