You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to write some code that fixes a misconfigured sys.stdin on a case by case bases but unfortunately I cannot use TextIOWrapper for this because it always closes the underlying file:
Ah. Misread. This is about detaching the underlying stream from TextIOWrapper. I assume this could be done in the __del__ so that would work. I'm checking this now.
Now this fails when context manager is trying to close the text_buffer with an error that it is already detached. If I do not detach it, then it closes buffer as well.
mhvk commented on Jun 3, 2021
mhvkmannequin
Mannequin
More actions
In astropy we are now working around the auto-closing of the underlying stream in TextIOWrapper by subclassing and overriding __del__ to detach [1]. It would seem more elegant if TestIOWrapper (and really, BufferedReader) could gain an closefd argument, just like open has, which is True by default.
p.s. Do let me know if it is better to open a new issue.
Activity
mitsuhiko commentedon Apr 27, 2014
I'm trying to write some code that fixes a misconfigured sys.stdin on a case by case bases but unfortunately I cannot use TextIOWrapper for this because it always closes the underlying file:
Python
Ideally there would be a way to disable this behavior.
eryksun commentedon Apr 27, 2014
It works if you detach the buffer beforehand:
>>> import io, sys >>> stdin = sys.stdin >>> stdin.flush() >>> correct_stdin = io.TextIOWrapper(stdin.buffer, 'utf-8') >>> correct_stdin.readline() foobar 'foobar\n' >>> correct_stdin.detach() <_io.BufferedReader name='<stdin>'> >>> del correct_stdin >>> stdin.readline() foobar 'foobar\n'bitdancer commentedon May 14, 2014
I think detach is the correct way to to this, so I'm closing the issue.
mitsuhiko commentedon May 14, 2014
Detach "destroys" the stream, so it's not a solution. I can't just randomly destroy global state just because it's convenient.
This is what I am doing now which seems borderline insane: https://github.com/mitsuhiko/click/blob/master/click/_compat.py#L31
mitsuhiko commentedon May 14, 2014
Ah. Misread. This is about detaching the underlying stream from TextIOWrapper. I assume this could be done in the __del__ so that would work. I'm checking this now.
mitsuhiko commentedon May 14, 2014
I can confirm that calling detach() in __del__ within an except block solves the issue.
mitar commentedon Oct 20, 2018
I have a similar problem that text wrapper is closing the handle, and if I want to make a workaround, it also fails:
Now this fails when context manager is trying to close the text_buffer with an error that it is already detached. If I do not detach it, then it closes buffer as well.
mhvk commentedon Jun 3, 2021
In astropy we are now working around the auto-closing of the underlying stream in TextIOWrapper by subclassing and overriding
__del__to detach [1]. It would seem more elegant ifTestIOWrapper(and really,BufferedReader) could gain anclosefdargument, just likeopenhas, which isTrueby default.p.s. Do let me know if it is better to open a new issue.
[1] astropy/astropy#11809