Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3e17c78

Browse files
Issue #26198: Fixed error messages for some argument parsing errors.
2 parents 503f908 + c4b813d commit 3e17c78

2 files changed

Lines changed: 21 additions & 13 deletions

File tree

Lib/test/test_capi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ def test_skipitem(self):
489489
format.encode("ascii"), keywords)
490490
when_not_skipped = False
491491
except TypeError as e:
492-
s = "argument 1 must be impossible<bad format char>, not int"
492+
s = "argument 1 (impossible<bad format char>)"
493493
when_not_skipped = (str(e) == s)
494494
except RuntimeError as e:
495495
when_not_skipped = False

Python/getargs.c

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ vgetargs1(PyObject *args, const char *format, va_list *p_va, int flags)
342342
flags, levels, msgbuf,
343343
sizeof(msgbuf), &freelist);
344344
if (msg) {
345-
seterror(i+1, msg, levels, fname, msg);
345+
seterror(i+1, msg, levels, fname, message);
346346
return cleanreturn(0, &freelist);
347347
}
348348
}
@@ -535,9 +535,15 @@ converterr(const char *expected, PyObject *arg, char *msgbuf, size_t bufsize)
535535
{
536536
assert(expected != NULL);
537537
assert(arg != NULL);
538-
PyOS_snprintf(msgbuf, bufsize,
539-
"must be %.50s, not %.50s", expected,
540-
arg == Py_None ? "None" : arg->ob_type->tp_name);
538+
if (expected[0] == '(') {
539+
PyOS_snprintf(msgbuf, bufsize,
540+
"%.100s", expected);
541+
}
542+
else {
543+
PyOS_snprintf(msgbuf, bufsize,
544+
"must be %.50s, not %.50s", expected,
545+
arg == Py_None ? "None" : arg->ob_type->tp_name);
546+
}
541547
return msgbuf;
542548
}
543549

@@ -741,7 +747,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
741747
if (PyLong_Check(arg))
742748
ival = PyLong_AsUnsignedLongMask(arg);
743749
else
744-
return converterr("integer<k>", arg, msgbuf, bufsize);
750+
return converterr("int", arg, msgbuf, bufsize);
745751
*p = ival;
746752
break;
747753
}
@@ -766,7 +772,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
766772
if (PyLong_Check(arg))
767773
ival = PyLong_AsUnsignedLongLongMask(arg);
768774
else
769-
return converterr("integer<K>", arg, msgbuf, bufsize);
775+
return converterr("int", arg, msgbuf, bufsize);
770776
*p = ival;
771777
break;
772778
}
@@ -1123,9 +1129,11 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
11231129
} else {
11241130
if (size + 1 > BUFFER_LEN) {
11251131
Py_DECREF(s);
1126-
return converterr(
1127-
"(buffer overflow)",
1128-
arg, msgbuf, bufsize);
1132+
PyErr_Format(PyExc_TypeError,
1133+
"encoded string too long "
1134+
"(%zd, maximum length %zd)",
1135+
(Py_ssize_t)size, (Py_ssize_t)(BUFFER_LEN-1));
1136+
RETURN_ERR_OCCURRED;
11291137
}
11301138
}
11311139
memcpy(*buffer, ptr, size+1);
@@ -1147,7 +1155,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
11471155
if ((Py_ssize_t)strlen(ptr) != size) {
11481156
Py_DECREF(s);
11491157
return converterr(
1150-
"encoded string without NULL bytes",
1158+
"encoded string without null bytes",
11511159
arg, msgbuf, bufsize);
11521160
}
11531161
*buffer = PyMem_NEW(char, size + 1);
@@ -1237,7 +1245,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
12371245

12381246
if (*format != '*')
12391247
return converterr(
1240-
"invalid use of 'w' format character",
1248+
"(invalid use of 'w' format character)",
12411249
arg, msgbuf, bufsize);
12421250
format++;
12431251

@@ -1261,7 +1269,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
12611269
}
12621270

12631271
default:
1264-
return converterr("impossible<bad format char>", arg, msgbuf, bufsize);
1272+
return converterr("(impossible<bad format char>)", arg, msgbuf, bufsize);
12651273

12661274
}
12671275

0 commit comments

Comments
 (0)