-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
I/O operation on closed file #1664
Comments
Black fails with "I/O operation on closed file", see the reported issue on black: psf/black#1664
As another data point from another code base, I observed this issue when 17 of 188 files would be changed by black. Once those files were changed to satisfy black, the issue went away. When only 5 of 188 files do not satisfy black, the problem occurs sporadically for me. So it seems like the problem manifests when there are a sufficient number of diffs being generated. |
The problematic behavior only happens when the optional When wrapping the output stream with colorama, it is assumed that colorama returns a wrapped stream without its own I'll post a PR for this shortly. |
The wrap_stream_for_windows() function calls colorama.initialise.wrap_stream() function to apply colorama's magic to wrapper to the output stream. Except this wrapper is only applied on Windows platforms that need it, otherwise the original stream is returned as-is. The colorama wrapped stream lacks a detach() method, so a no-op lambda was being assigned to the wrapped stream. The problem is that the no-op lambda was being assigned unconditionally whether or not colorama actually returns a wrapped stream, thus replacing the original TextIOWrapper's detach() method. Replacing the detach() method with a no-op lambda is the root cause of the problem observed in psf#1664. The solution is to only assign the no-op detach() method if the stream lacks its own detach() method. Repairs psf#1664
The wrap_stream_for_windows() function calls colorama.initialise.wrap_stream() function to apply colorama's magic to wrapper to the output stream. Except this wrapper is only applied on Windows platforms that need it, otherwise the original stream is returned as-is. The colorama wrapped stream lacks a detach() method, so a no-op lambda was being assigned to the wrapped stream. The problem is that the no-op lambda was being assigned unconditionally whether or not colorama actually returns a wrapped stream, thus replacing the original TextIOWrapper's detach() method. Replacing the detach() method with a no-op lambda is the root cause of the problem observed in psf#1664. The solution is to only assign the no-op detach() method if the stream lacks its own detach() method. Repairs psf#1664
The wrap_stream_for_windows() function calls colorama.initialise.wrap_stream() function to apply colorama's magic to wrapper to the output stream. Except this wrapper is only applied on Windows platforms that need it, otherwise the original stream is returned as-is. The colorama wrapped stream lacks a detach() method, so a no-op lambda was being assigned to the wrapped stream. The problem is that the no-op lambda was being assigned unconditionally whether or not colorama actually returns a wrapped stream, thus replacing the original TextIOWrapper's detach() method. Replacing the detach() method with a no-op lambda is the root cause of the problem observed in psf#1664. The solution is to only assign the no-op detach() method if the stream lacks its own detach() method. Repairs psf#1664
* Repair colorama wrapping on non-Windows platforms The wrap_stream_for_windows() function calls colorama.initialise.wrap_stream() function to apply colorama's magic to wrapper to the output stream. Except this wrapper is only applied on Windows platforms that need it, otherwise the original stream is returned as-is. The colorama wrapped stream lacks a detach() method, so a no-op lambda was being assigned to the wrapped stream. The problem is that the no-op lambda was being assigned unconditionally whether or not colorama actually returns a wrapped stream, thus replacing the original TextIOWrapper's detach() method. Replacing the detach() method with a no-op lambda is the root cause of the problem observed in #1664. The solution is to only assign the no-op detach() method if the stream lacks its own detach() method. Repairs #1664
Running Before 4d71d74: 2 files would be reformatted, 56 files would be left unchanged, 86 files would fail to reformat. After 4d71d74: 88 files would be reformatted, 56 files would be left unchanged. My environment
I'll have to check that the patch also works on Windows but that will take a while - hopefully I don't have mess around with Windows Update :-) |
I procrastinated for way too long on getting back if this patch works on Windows or not but the answer is "yes it does" :D |
This was fixed quite a while ago, although it was only released in 21.4b0. |
* Repair colorama wrapping on non-Windows platforms The wrap_stream_for_windows() function calls colorama.initialise.wrap_stream() function to apply colorama's magic to wrapper to the output stream. Except this wrapper is only applied on Windows platforms that need it, otherwise the original stream is returned as-is. The colorama wrapped stream lacks a detach() method, so a no-op lambda was being assigned to the wrapped stream. The problem is that the no-op lambda was being assigned unconditionally whether or not colorama actually returns a wrapped stream, thus replacing the original TextIOWrapper's detach() method. Replacing the detach() method with a no-op lambda is the root cause of the problem observed in psf#1664. The solution is to only assign the no-op detach() method if the stream lacks its own detach() method. Repairs psf#1664
Running Black 20.8b1 with the
--diff
option on a tree (> 100 files) produces errors likeRunning
black --diff
with a single file does not produce this error.Linux/Python 3.7.8+
The text was updated successfully, but these errors were encountered: