Skip to content

bpo-28261: fix err msgs where PyArg_ParseTuple is used to parse normal tuples #3119

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

Merged
merged 7 commits into from
Aug 20, 2017

Conversation

orenmn
Copy link
Contributor

@orenmn orenmn commented Aug 17, 2017

according to http://bugs.python.org/issue28261, fix some error messages.

without this patch, each of the following calls would have produced a wrong error message (at least on Windows 10):

  • ctypes.CFUNCTYPE(None)(())
  • _io.IncrementalNewlineDecoder(42, 42).setstate(())
  • audioop.ratecv(b'a', 1, 1, 42, 42, (42, ((),)))
  • audioop.lin2adpcm(b'a', 1, ())
  • audioop.adpcm2lin(b'a', 1, ())
  • _overlapped.Overlapped().ConnectEx(42, ()) (also, _overlapped.Overlapped().ConnectEx(42, 43) would have caused a SystemError.)
  • socket.getnameinfo((), 42)
  • time.strftime('aoeu', ())
  • time.asctime(())
  • time.mktime(())

https://bugs.python.org/issue28261

@@ -562,8 +562,11 @@ _io_IncrementalNewlineDecoder_setstate(nldecoder_object *self,
PyObject *buffer;
unsigned long long flag;

if (!PyArg_ParseTuple(state, "OK", &buffer, &flag))
if (!PyArg_ParseTuple(state,
"OK;IncrementalNewlineDecoder.setstate(): illegal "
Copy link
Member

Choose a reason for hiding this comment

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

If state is not a tuple, PyArg_ParseTuple() raises SystemError which means an internal error in the interpreter or extension. Add an explicit check and raise TypeError if state is not a tuple. See lin2adpcm() for example. This comment is applicable to other changes below.

And this error message looks too long to me. The method name (or at least the class name) could be omitted. The context can be derived from a traceback.

@@ -3262,7 +3262,8 @@ PyCFuncPtr_FromDll(PyTypeObject *type, PyObject *args, PyObject *kwds)
/* Here ftuple is a borrowed reference */
return NULL;

if (!PyArg_ParseTuple(ftuple, "O&O", _get_name, &name, &dll)) {
if (!PyArg_ParseTuple(ftuple, "O&O;illegal func_spec argument",
_get_name, &name, &dll)) {
Copy link
Member

Choose a reason for hiding this comment

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

In case of multiline condition the opening { should be moved to the separate line for better readability. This is a new requirement of PEP 7.

This comment is applicable to other changes below.

@bedevere-bot
Copy link

A Python core developer, serhiy-storchaka, has requested some changes be
made to your pull request before we can consider merging it. If you
could please address their requests along with any other requests in
other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment
on this pull request containing the phrase I didn't expect the Spanish Inquisition!
I will then notify serhiy-storchaka along with any other core developers
who have left a review that you're ready for them to take another look
at this pull request.

channel = PyTuple_GetItem(samps, chan);
if (!PyTuple_Check(channel)) {
PyErr_SetString(PyExc_TypeError,
"ratecv(): channel must be 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 am not sure about this error message, as I failed to find the documentation of the state argument.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe just "illegal state argument"? state is an opaque value. It is returned by ratecv() and passed to other invocation of ratecv(). "channel" is nowhere documented.

@orenmn
Copy link
Contributor Author

orenmn commented Aug 18, 2017

note that I also added very basic tests to test_audioop.py and to test_io.py, to verify SystemError is not raised anymore.

I didn't expect the Spanish Inquisition!

@bedevere-bot
Copy link

Nobody expects the Spanish Inquisition!

@serhiy-storchaka: please review the changes made to this pull request.

channel = PyTuple_GetItem(samps, chan);
if (!PyTuple_Check(channel)) {
PyErr_SetString(PyExc_TypeError,
"ratecv(): channel must be 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.

Maybe just "illegal state argument"? state is an opaque value. It is returned by ratecv() and passed to other invocation of ratecv(). "channel" is nowhere documented.

@@ -1024,8 +1026,11 @@ Overlapped_ConnectEx(OverlappedObject *self, PyObject *args)
BOOL ret;
DWORD err;

if (!PyArg_ParseTuple(args, F_HANDLE "O", &ConnectSocket, &AddressObj))
if (!PyArg_ParseTuple(args, F_HANDLE "O!:ConnectEx", &ConnectSocket,
Copy link
Member

Choose a reason for hiding this comment

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

Would not it look better if break a line before &ConnectSocket?

@@ -586,8 +586,11 @@ time_strftime(PyObject *self, PyObject *args)
if (_PyTime_localtime(tt, &buf) != 0)
return NULL;
}
else if (!gettmarg(tup, &buf) || !checktm(&buf))
else if (!gettmarg(tup, &buf, "iiiiiiiii;strftime(): illegal time tuple "
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't it look better if break a line before the format argument and before !checktm(&buf)?

@serhiy-storchaka
Copy link
Member

I think a NEWS.d entry for this PR is not required. But you can add it if you prefer.

@@ -780,8 +781,9 @@ time_asctime(PyObject *self, PyObject *args)
if (_PyTime_localtime(tt, &buf) != 0)
return NULL;

} else if (!gettmarg(tup, &buf, "iiiiiiiii;asctime(): illegal time tuple "
"argument") || !checktm(&buf))
} else if (!gettmarg(tup, &buf,
Copy link
Member

Choose a reason for hiding this comment

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

Yet one nitpick: break a line between "}" and "else" while we are here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks for the review, Serhiy!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants