-
-
Notifications
You must be signed in to change notification settings - Fork 33.3k
gh-140650: fix SystemError in io.BufferedWriter.close when closed errors #140653
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does not look right. If IS_CLOSED() returns a positive value and sets an error, there is a bug in IS_CLOSED().
|
@serhiy-storchaka : Overall problem is code assumes Another option here is "Exception" can be turned into a True result for "IS_CLOSED" + clear the exception but that seems like hiding an implemention issue rather than helping resolve it to me. |
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also update CHECK_CLOSED to handle IS_CLOSED returning non-zero + setting an exception. In particular, if there's an exception CHECK_CLOSED should always return.
|
Should I still be using |
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
| @@ -0,0 +1,2 @@ | |||
| Fix an issue where closing :class:`io.BufferedWriter` could crash | |||
| if the closed attribute raised an exception. | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not accurate. Bad things happen when the value of the closed attribute cannot be converted to boolean.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In addition to the "cannot be converted to a boolean" case, this patch also fixes cases where closed is defined as a managed attribute equivalent to
@property
def closed(self):
raise Exception()Should the news entry just focus on the first type error case, or mention fixing this case as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should add tests on this behavior too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the news to reflect both cases, and added a test for the other behavior. I wonder if we need a line mentioning that exceptions from .closed are no longer swallowed by the corresponding methods (like .write(), .flush())
Restores the 3.14 behavior of emitting a TypeError. Preservation of the sign helps in the error checking here:
cpython/Modules/_io/bufferedio.c
Lines 556 to 563 in 3dab11f
db68bfc changed from
buffered_closedtoIS_CLOSED.