(original) (raw)
changeset: 100178:9f998e24d8d8 branch: 3.5 parent: 100172:f25d8cbd074a user: Serhiy Storchaka storchaka@gmail.com date: Mon Feb 08 01:06:11 2016 +0200 files: Doc/c-api/arg.rst Lib/test/test_capi.py Python/getargs.c description: Issue #26198: Fixed error messages for some argument parsing errors. Fixed the documented about buffer overflow error for "es#" and "et#" format units. diff -r f25d8cbd074a -r 9f998e24d8d8 Doc/c-api/arg.rst --- a/Doc/c-api/arg.rst Mon Feb 08 01:34:09 2016 +0000 +++ b/Doc/c-api/arg.rst Mon Feb 08 01:06:11 2016 +0200 @@ -206,7 +206,8 @@ :c:func:`PyArg_ParseTuple` will use this location as the buffer and interpret the initial value of *\*buffer_length* as the buffer size. It will then copy the encoded data into the buffer and NUL-terminate it. If the buffer is not large - enough, a :exc:`ValueError` will be set. + enough, a :exc:`TypeError` will be set. + Note: starting from Python 3.6 a :exc:`ValueError` will be set. In both cases, *\*buffer_length* is set to the length of the encoded data without the trailing NUL byte. diff -r f25d8cbd074a -r 9f998e24d8d8 Lib/test/test_capi.py --- a/Lib/test/test_capi.py Mon Feb 08 01:34:09 2016 +0000 +++ b/Lib/test/test_capi.py Mon Feb 08 01:06:11 2016 +0200 @@ -489,7 +489,7 @@ format.encode("ascii"), keywords) when_not_skipped = False except TypeError as e: - s = "argument 1 must be impossible, not int" + s = "argument 1 (impossible)" when_not_skipped = (str(e) == s) except RuntimeError as e: when_not_skipped = False diff -r f25d8cbd074a -r 9f998e24d8d8 Python/getargs.c --- a/Python/getargs.c Mon Feb 08 01:34:09 2016 +0000 +++ b/Python/getargs.c Mon Feb 08 01:06:11 2016 +0200 @@ -342,7 +342,7 @@ flags, levels, msgbuf, sizeof(msgbuf), &freelist); if (msg) { - seterror(i+1, msg, levels, fname, msg); + seterror(i+1, msg, levels, fname, message); return cleanreturn(0, &freelist); } } @@ -535,9 +535,15 @@ { assert(expected != NULL); assert(arg != NULL); - PyOS_snprintf(msgbuf, bufsize, - "must be %.50s, not %.50s", expected, - arg == Py_None ? "None" : arg->ob_type->tp_name); + if (expected[0] == '(') { + PyOS_snprintf(msgbuf, bufsize, + "%.100s", expected); + } + else { + PyOS_snprintf(msgbuf, bufsize, + "must be %.50s, not %.50s", expected, + arg == Py_None ? "None" : arg->ob_type->tp_name); + } return msgbuf; } @@ -741,7 +747,7 @@ if (PyLong_Check(arg)) ival = PyLong_AsUnsignedLongMask(arg); else - return converterr("integer", arg, msgbuf, bufsize); + return converterr("int", arg, msgbuf, bufsize); *p = ival; break; } @@ -766,7 +772,7 @@ if (PyLong_Check(arg)) ival = PyLong_AsUnsignedLongLongMask(arg); else - return converterr("integer", arg, msgbuf, bufsize); + return converterr("int", arg, msgbuf, bufsize); *p = ival; break; } @@ -1123,9 +1129,11 @@ } else { if (size + 1 > BUFFER_LEN) { Py_DECREF(s); - return converterr( - "(buffer overflow)", - arg, msgbuf, bufsize); + PyErr_Format(PyExc_TypeError, + "encoded string too long " + "(%zd, maximum length %zd)", + (Py_ssize_t)size, (Py_ssize_t)(BUFFER_LEN-1)); + RETURN_ERR_OCCURRED; } } memcpy(*buffer, ptr, size+1); @@ -1147,7 +1155,7 @@ if ((Py_ssize_t)strlen(ptr) != size) { Py_DECREF(s); return converterr( - "encoded string without NULL bytes", + "encoded string without null bytes", arg, msgbuf, bufsize); } *buffer = PyMem_NEW(char, size + 1); @@ -1237,7 +1245,7 @@ if (*format != '*') return converterr( - "invalid use of 'w' format character", + "(invalid use of 'w' format character)", arg, msgbuf, bufsize); format++; @@ -1261,7 +1269,7 @@ } default: - return converterr("impossible", arg, msgbuf, bufsize); + return converterr("(impossible)", arg, msgbuf, bufsize); }/storchaka@gmail.com