mirror of
https://github.com/python/cpython.git
synced 2025-09-18 22:50:26 +00:00
bpo-29730: replace some calls to PyNumber_Check and improve some error messages (#650)
This commit is contained in:
parent
b7c9150b68
commit
004251059b
5 changed files with 39 additions and 25 deletions
|
@ -549,14 +549,15 @@ _PyIO_ConvertSsize_t(PyObject *obj, void *result) {
|
||||||
if (obj == Py_None) {
|
if (obj == Py_None) {
|
||||||
limit = -1;
|
limit = -1;
|
||||||
}
|
}
|
||||||
else if (PyNumber_Check(obj)) {
|
else if (PyIndex_Check(obj)) {
|
||||||
limit = PyNumber_AsSsize_t(obj, PyExc_OverflowError);
|
limit = PyNumber_AsSsize_t(obj, PyExc_OverflowError);
|
||||||
if (limit == -1 && PyErr_Occurred())
|
if (limit == -1 && PyErr_Occurred()) {
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
PyErr_Format(PyExc_TypeError,
|
PyErr_Format(PyExc_TypeError,
|
||||||
"integer argument expected, got '%.200s'",
|
"argument should be integer or None, not '%.200s'",
|
||||||
Py_TYPE(obj)->tp_name);
|
Py_TYPE(obj)->tp_name);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -578,12 +578,18 @@ _io_BytesIO_truncate_impl(bytesio *self, PyObject *arg)
|
||||||
/* Truncate to current position if no argument is passed. */
|
/* Truncate to current position if no argument is passed. */
|
||||||
size = self->pos;
|
size = self->pos;
|
||||||
}
|
}
|
||||||
else {
|
else if (PyIndex_Check(arg)) {
|
||||||
size = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
|
size = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
|
||||||
if (size == -1 && PyErr_Occurred()) {
|
if (size == -1 && PyErr_Occurred()) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
PyErr_Format(PyExc_TypeError,
|
||||||
|
"argument should be integer or None, not '%.200s'",
|
||||||
|
Py_TYPE(arg)->tp_name);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
if (size < 0) {
|
if (size < 0) {
|
||||||
PyErr_Format(PyExc_ValueError,
|
PyErr_Format(PyExc_ValueError,
|
||||||
|
|
|
@ -458,17 +458,19 @@ _io_StringIO_truncate_impl(stringio *self, PyObject *arg)
|
||||||
CHECK_INITIALIZED(self);
|
CHECK_INITIALIZED(self);
|
||||||
CHECK_CLOSED(self);
|
CHECK_CLOSED(self);
|
||||||
|
|
||||||
if (PyNumber_Check(arg)) {
|
if (PyIndex_Check(arg)) {
|
||||||
size = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
|
size = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
|
||||||
if (size == -1 && PyErr_Occurred())
|
if (size == -1 && PyErr_Occurred()) {
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if (arg == Py_None) {
|
else if (arg == Py_None) {
|
||||||
/* Truncate to current position if no argument is passed. */
|
/* Truncate to current position if no argument is passed. */
|
||||||
size = self->pos;
|
size = self->pos;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'",
|
PyErr_Format(PyExc_TypeError,
|
||||||
|
"argument should be integer or None, not '%.200s'",
|
||||||
Py_TYPE(arg)->tp_name);
|
Py_TYPE(arg)->tp_name);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
|
@ -247,14 +247,15 @@ mmap_convert_ssize_t(PyObject *obj, void *result) {
|
||||||
if (obj == Py_None) {
|
if (obj == Py_None) {
|
||||||
limit = -1;
|
limit = -1;
|
||||||
}
|
}
|
||||||
else if (PyNumber_Check(obj)) {
|
else if (PyIndex_Check(obj)) {
|
||||||
limit = PyNumber_AsSsize_t(obj, PyExc_OverflowError);
|
limit = PyNumber_AsSsize_t(obj, PyExc_OverflowError);
|
||||||
if (limit == -1 && PyErr_Occurred())
|
if (limit == -1 && PyErr_Occurred()) {
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
PyErr_Format(PyExc_TypeError,
|
PyErr_Format(PyExc_TypeError,
|
||||||
"integer argument expected, got '%.200s'",
|
"argument should be integer or None, not '%.200s'",
|
||||||
Py_TYPE(obj)->tp_name);
|
Py_TYPE(obj)->tp_name);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -399,12 +399,15 @@ _Py_bytes_maketrans(Py_buffer *frm, Py_buffer *to)
|
||||||
#include "stringlib/find.h"
|
#include "stringlib/find.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Wraps stringlib_parse_args_finds() and additionally checks whether the
|
Wraps stringlib_parse_args_finds() and additionally checks the first
|
||||||
first argument is an integer in range(0, 256).
|
argument type.
|
||||||
|
|
||||||
If this is the case, writes the integer value to the byte parameter
|
In case the first argument is a bytes-like object, sets it to subobj,
|
||||||
and sets subobj to NULL. Otherwise, sets the first argument to subobj
|
and doesn't touch the byte parameter.
|
||||||
and doesn't touch byte. The other parameters are similar to those of
|
In case it is an integer in range(0, 256), writes the integer value
|
||||||
|
to byte, and sets subobj to NULL.
|
||||||
|
|
||||||
|
The other parameters are similar to those of
|
||||||
stringlib_parse_args_finds().
|
stringlib_parse_args_finds().
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -415,27 +418,28 @@ parse_args_finds_byte(const char *function_name, PyObject *args,
|
||||||
{
|
{
|
||||||
PyObject *tmp_subobj;
|
PyObject *tmp_subobj;
|
||||||
Py_ssize_t ival;
|
Py_ssize_t ival;
|
||||||
PyObject *err;
|
|
||||||
|
|
||||||
if(!stringlib_parse_args_finds(function_name, args, &tmp_subobj,
|
if(!stringlib_parse_args_finds(function_name, args, &tmp_subobj,
|
||||||
start, end))
|
start, end))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if (!PyNumber_Check(tmp_subobj)) {
|
if (PyObject_CheckBuffer(tmp_subobj)) {
|
||||||
*subobj = tmp_subobj;
|
*subobj = tmp_subobj;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ival = PyNumber_AsSsize_t(tmp_subobj, PyExc_OverflowError);
|
if (!PyIndex_Check(tmp_subobj)) {
|
||||||
if (ival == -1) {
|
PyErr_Format(PyExc_TypeError,
|
||||||
err = PyErr_Occurred();
|
"argument should be integer or bytes-like object, "
|
||||||
if (err && !PyErr_GivenExceptionMatches(err, PyExc_OverflowError)) {
|
"not '%.200s'",
|
||||||
PyErr_Clear();
|
Py_TYPE(tmp_subobj)->tp_name);
|
||||||
*subobj = tmp_subobj;
|
return 0;
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ival = PyNumber_AsSsize_t(tmp_subobj, NULL);
|
||||||
|
if (ival == -1 && PyErr_Occurred()) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
if (ival < 0 || ival > 255) {
|
if (ival < 0 || ival > 255) {
|
||||||
PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
|
PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue