Skip to content

Conversation

orenmn
Copy link
Contributor

@orenmn orenmn commented Aug 21, 2017

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

@orenmn orenmn changed the title bpo-31243 fix PyArg_ParseTuple failure checks bpo-31243: fix PyArg_ParseTuple failure checks Aug 21, 2017
@@ -1498,15 +1498,26 @@ textiowrapper_read_chunk(textio *self, Py_ssize_t size_hint)
/* Given this, we know there was a valid snapshot point
Copy link
Contributor Author

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?

Copy link
Member

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?

class BadDecoder:
def getstate(self):
return getstate_ret_val
class BadIncrementalDecoder:
Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

definitely. sorry for that.

def __call__(self, dummy):
return BadDecoder()
quopri = codecs.lookup("quopri")
quopri_decoder = quopri.incrementaldecoder
Copy link
Member

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().

Copy link
Contributor Author

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?

Copy link
Member

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.

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 "
Copy link
Member

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?

Copy link
Contributor Author

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.)

Copy link
Member

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)
} \
Copy link
Member

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.

Copy link
Contributor Author

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.)

Copy link
Member

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.

Copy link
Contributor Author

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

@serhiy-storchaka
Copy link
Member

Please add also a NEWS.d entry. This change fixes bugs and the information about this can be important for end users.

@orenmn
Copy link
Contributor Author

orenmn commented Aug 24, 2017

please ignore this NEWS.d. it is not accurate. I would fix it soon.

@serhiy-storchaka serhiy-storchaka merged commit ba7d736 into python:master Aug 29, 2017
@serhiy-storchaka serhiy-storchaka added needs backport to 2.7 type-bug An unexpected behavior, bug, or error labels Aug 29, 2017
orenmn added a commit to orenmn/cpython that referenced this pull request Aug 29, 2017
@bedevere-bot
Copy link

GH-3233 is a backport of this pull request to the 3.6 branch.

@bedevere-bot
Copy link

GH-3235 is a backport of this pull request to the 2.7 branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants