-
-
Notifications
You must be signed in to change notification settings - Fork 32.9k
bpo-31243: fix PyArg_ParseTuple failure checks #3171
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
bpo-31243: fix PyArg_ParseTuple failure checks #3171
Conversation
@@ -1498,15 +1498,26 @@ textiowrapper_read_chunk(textio *self, Py_ssize_t size_hint) | |||
/* Given this, we know there was a valid snapshot point |
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.
ISTM that this comment is wrong, but I didn't manage to find its origin (benjaminp created _textio.c with that comment in it (https://github.com/python/cpython/tree/4fa88fa0ba35e25ad9be66ebbdaba9aca553dc8b), but I don't know where it came from. BTW, the same comment can also be found in Lib/_pyio.py).
In addition, I am not familiar with the io
module, so I am reluctant to remove this comment from here.
what do you think?
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.
What do you think @benjaminp?
Lib/test/test_io.py
Outdated
class BadDecoder: | ||
def getstate(self): | ||
return getstate_ret_val | ||
class BadIncrementalDecoder: |
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.
Can this be just a function?
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.
definitely. sorry for that.
Lib/test/test_io.py
Outdated
def __call__(self, dummy): | ||
return BadDecoder() | ||
quopri = codecs.lookup("quopri") | ||
quopri_decoder = quopri.incrementaldecoder |
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.
You can use test.support.swap_attr()
.
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.
that's awesome.
BTW, except for its docstring, could it be that swap_attr() is not documented?
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.
The test.support
module is for internal use, and most of its content is not documented. If you want to add a documentation, open a separate issue.
Modules/_io/textio.c
Outdated
Py_DECREF(state); | ||
return -1; | ||
} | ||
|
||
if (!PyBytes_Check(dec_buffer)) { | ||
PyErr_Format(PyExc_TypeError, | ||
"decoder getstate() should have returned a bytes " | ||
"object, not '%.200s'", | ||
"decoder getstate() should have returned a tuple " |
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.
Isn't there too much attention for rare errors? Wouldn't be enough just "illegal decoder state" for all three checks?
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 thought it would be more helpful for users, but if you think this is an overkill, I would change it.
BTW, there is another "decoder getstate() should have returned ..."
error message, in DECODER_GETSTATE()
. should I change it also to "illegal decoder state" for consistency?
(the PyArg_ParseTuple before that one is part of bpo-28261. I am working on a patch for it, and I guessed we shouldn't fix it 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.
Good point. Similar changes should be made in DECODER_GETSTATE()
.
I think it is worth to keep the wrong type name in this error, since returning a bytearray or a memoryview is more common error. But it would be nice to keep it short and make all three error messages similar.
For example, just "illegal decoder state" for first two cases and "illegal decoder state: the first item should be a bytes object, not '%.200s'" in the third case.
@@ -2395,8 +2392,8 @@ _io_TextIOWrapper_tell_impl(textio *self) | |||
} \ |
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.
Add also an explicit check for a tuple.
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 thought that this should be fixed as part of bpo-28261, as bpo31243 is about PyArg_ParseTuple returning 0 or 1.
ISTM that if we add that check for a tuple, we might as well add ";illegal decoder state" to the argument of PyArg_ParseTuple.
Don't you think that leaving it to bpo-28261 is preferable? (I would open a PR for leftovers of bpo-28261 anyway today.)
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.
Okay, this can be done in a separate PR. But in any case I think it is worth to add a NEWS.d entry.
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.
of course, I would add it soon
Please add also a NEWS.d entry. This change fixes bugs and the information about this can be important for end users. |
please ignore this NEWS.d. it is not accurate. I would fix it soon. |
(cherry picked from commit ba7d736)
GH-3233 is a backport of this pull request to the 3.6 branch. |
GH-3235 is a backport of this pull request to the 2.7 branch. |
according to http://bugs.python.org/issue31243, replace checks whether PyArg_ParseTuple returned a negative value with checks whether PyArg_ParseTuple returned 0.
while we are here, make sure the call to PyArg_ParseTuple in textiowrapper_read_chunk() won't cause producing a wrong error message, as explained in http://bugs.python.org/issue28261, or a SystemError.
https://bugs.python.org/issue31243