[New-bugs-announce] [issue28261] wrong error messages when using PyArg_ParseTuple to parse normal tuples

Oren Milman report at bugs.python.org
Fri Sep 23 16:16:55 EDT 2016


New submission from Oren Milman:

------------ current state ------------
In few places in the codebase, PyArg_ParseTuple is called in order to parse a normal tuple (instead of the usual case of parsing the arguments tuple of a function).
In some of these places, failure of PyArg_ParseTuple is handled simply by cleanup (if needed) and returning an error code, and so an exception with the generic error message is raised.
The generic error message is appropriate in case the parsed tuple is the arguments tuple of a function, but might be really wrong in case it is a normal tuple.

For example, such case in Modules/socketmodule.c in socket_getnameinfo leads to the following:
    >>> import socket
    >>> socket.getnameinfo(tuple(), 1)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: function takes at least 2 arguments (0 given)
    >>>


------------ proposed changes ------------
In each of these places, add to the format string (which is passed to PyArg_ParseTuple) a ';', followed by an appropriate error message, as already done in Modules/audioop.c in audioop_ratecv_impl:
        if (!PyArg_ParseTuple(state,
                        "iO!;audioop.ratecv: illegal state argument",
                        &d, &PyTuple_Type, &samps))
            goto exit;


------------ diff ------------
The proposed patches diff file is attached.


------------ tests ------------
I wrote an ugly script to verify the wrong error messages on CPython without my patches, and to test the patches on CPython with my patches. The script is attached (but it might fail on a non-Windows machine).

In addition, I ran 'python_d.exe -m test -j3' (on my 64-bit Windows 10) with and without the patches, and got quite the same output.
The outputs of both runs are attached.

----------
components: IO
files: testBugsOrPatches.py
messages: 277296
nosy: Oren Milman
priority: normal
severity: normal
status: open
title: wrong error messages when using PyArg_ParseTuple to parse normal tuples
type: behavior
versions: Python 3.7
Added file: http://bugs.python.org/file44795/testBugsOrPatches.py

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue28261>
_______________________________________


More information about the New-bugs-announce mailing list