-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
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
gh-127182: Fix io.StringIO.__setstate__
crash when None
is the first value
#127219
Conversation
…the first value
self->string_size = bufsize; | ||
} | ||
else { | ||
assert(item == Py_None); |
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.
It may be worth it to check item value at runtime, and fail with an assertion if it's not None.
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.
I don't think that it is, because __init__
call above checks that. I don't think that it would be possible to trigger this error here.
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.
Oh right, it makes sense. I didn't see the _io_StringIO___init__()
call.
Co-authored-by: Victor Stinner <vstinner@python.org>
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
self->string_size = bufsize; | ||
} | ||
else { | ||
assert(item == Py_None); |
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.
Oh right, it makes sense. I didn't see the _io_StringIO___init__()
call.
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
self->string_size = bufsize; | ||
} | ||
else { | ||
assert(item == Py_None); |
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.
As a side note, should we encourage using Py_Is
instead of ==
?
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 this case, Py_IsNone() can be used ;-)
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.
I would prefer to keep it as == Py_None
, because other related parts do the same thing: if (value && value != Py_None && !PyUnicode_Check(value))
in __init__
, for example.
Thanks @sobolevn for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12, 3.13. |
…the first value (pythonGH-127219) (cherry picked from commit a2ee899) Co-authored-by: sobolevn <mail@sobolevn.me> Co-authored-by: Victor Stinner <vstinner@python.org>
…the first value (pythonGH-127219) (cherry picked from commit a2ee899) Co-authored-by: sobolevn <mail@sobolevn.me> Co-authored-by: Victor Stinner <vstinner@python.org>
GH-127262 is a backport of this pull request to the 3.13 branch. |
GH-127263 is a backport of this pull request to the 3.12 branch. |
Change:
None
value in__setstate__
, it cannot be recieved normally, because__getstate__
never returnsNone
, but we should not let a simple direct call to crash, when the fix is rather simple__init__
method:cpython/Modules/_io/stringio.c
Line 898 in f7bb658
None
is an allowed input value, seecpython/Modules/_io/stringio.c
Line 693 in f7bb658
None
in__setstate__
NULL
cannot be passed to__setstate__
, because it requires atuple
inputStringIO.__setstate__
#127182