bpo-37999: Simplify the conversion code for %c, %d, %x, etc. (GH-20437)

Since PyLong_AsLong() no longer use __int__, explicit call
of PyNumber_Index() before it is no longer needed.
This commit is contained in:
Serhiy Storchaka 2020-06-29 22:36:41 +03:00 committed by GitHub
parent 5b96370030
commit e67f7db3c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 58 deletions

View File

@ -22,22 +22,15 @@ char _PyByteArray_empty_string[] = "";
static int static int
_getbytevalue(PyObject* arg, int *value) _getbytevalue(PyObject* arg, int *value)
{ {
long face_value; int overflow;
long face_value = PyLong_AsLongAndOverflow(arg, &overflow);
if (PyLong_Check(arg)) { if (face_value == -1 && PyErr_Occurred()) {
face_value = PyLong_AsLong(arg);
} else {
PyObject *index = PyNumber_Index(arg);
if (index == NULL) {
*value = -1; *value = -1;
return 0; return 0;
} }
face_value = PyLong_AsLong(index);
Py_DECREF(index);
}
if (face_value < 0 || face_value >= 256) { if (face_value < 0 || face_value >= 256) {
/* this includes the OverflowError in case the long is too large */ /* this includes an overflow in converting to C long */
PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)"); PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
*value = -1; *value = -1;
return 0; return 0;

View File

@ -510,17 +510,14 @@ formatlong(PyObject *v, int flags, int prec, int type)
iobj = _PyNumber_Index(v); iobj = _PyNumber_Index(v);
else else
iobj = PyNumber_Long(v); iobj = PyNumber_Long(v);
if (iobj == NULL) {
if (!PyErr_ExceptionMatches(PyExc_TypeError))
return NULL;
}
else if (!PyLong_Check(iobj))
Py_CLEAR(iobj);
if (iobj != NULL) { if (iobj != NULL) {
assert(PyLong_Check(iobj));
result = _PyUnicode_FormatLong(iobj, flags & F_ALT, prec, type); result = _PyUnicode_FormatLong(iobj, flags & F_ALT, prec, type);
Py_DECREF(iobj); Py_DECREF(iobj);
return result; return result;
} }
if (!PyErr_ExceptionMatches(PyExc_TypeError))
return NULL;
} }
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
"%%%c format: %s is required, not %.200s", type, "%%%c format: %s is required, not %.200s", type,
@ -542,26 +539,16 @@ byte_converter(PyObject *arg, char *p)
return 1; return 1;
} }
else { else {
PyObject *iobj;
long ival;
int overflow; int overflow;
/* make sure number is a type of integer */ long ival = PyLong_AsLongAndOverflow(arg, &overflow);
if (PyLong_Check(arg)) { if (ival == -1 && PyErr_Occurred()) {
ival = PyLong_AsLongAndOverflow(arg, &overflow); if (PyErr_ExceptionMatches(PyExc_TypeError)) {
goto onError;
} }
else {
iobj = PyNumber_Index(arg);
if (iobj == NULL) {
if (!PyErr_ExceptionMatches(PyExc_TypeError))
return 0; return 0;
goto onError;
} }
ival = PyLong_AsLongAndOverflow(iobj, &overflow); if (!(0 <= ival && ival <= 255)) {
Py_DECREF(iobj); /* this includes an overflow in converting to C long */
}
if (!overflow && ival == -1 && PyErr_Occurred())
goto onError;
if (overflow || !(0 <= ival && ival <= 255)) {
PyErr_SetString(PyExc_OverflowError, PyErr_SetString(PyExc_OverflowError,
"%c arg not in range(256)"); "%c arg not in range(256)");
return 0; return 0;

View File

@ -14641,20 +14641,15 @@ mainformatlong(PyObject *v,
if (!PyLong_Check(v)) { if (!PyLong_Check(v)) {
if (type == 'o' || type == 'x' || type == 'X') { if (type == 'o' || type == 'x' || type == 'X') {
iobj = _PyNumber_Index(v); iobj = _PyNumber_Index(v);
if (iobj == NULL) {
if (PyErr_ExceptionMatches(PyExc_TypeError))
goto wrongtype;
return -1;
}
} }
else { else {
iobj = PyNumber_Long(v); iobj = PyNumber_Long(v);
}
if (iobj == NULL ) { if (iobj == NULL ) {
if (PyErr_ExceptionMatches(PyExc_TypeError)) if (PyErr_ExceptionMatches(PyExc_TypeError))
goto wrongtype; goto wrongtype;
return -1; return -1;
} }
}
assert(PyLong_Check(iobj)); assert(PyLong_Check(iobj));
} }
else { else {
@ -14736,24 +14731,17 @@ formatchar(PyObject *v)
goto onError; goto onError;
} }
else { else {
PyObject *iobj; int overflow;
long x; long x = PyLong_AsLongAndOverflow(v, &overflow);
/* make sure number is a type of integer */ if (x == -1 && PyErr_Occurred()) {
if (!PyLong_Check(v)) { if (PyErr_ExceptionMatches(PyExc_TypeError)) {
iobj = PyNumber_Index(v);
if (iobj == NULL) {
goto onError; goto onError;
} }
x = PyLong_AsLong(iobj); return (Py_UCS4) -1;
Py_DECREF(iobj);
} }
else {
x = PyLong_AsLong(v);
}
if (x == -1 && PyErr_Occurred())
goto onError;
if (x < 0 || x > MAX_UNICODE) { if (x < 0 || x > MAX_UNICODE) {
/* this includes an overflow in converting to C long */
PyErr_SetString(PyExc_OverflowError, PyErr_SetString(PyExc_OverflowError,
"%c arg not in range(0x110000)"); "%c arg not in range(0x110000)");
return (Py_UCS4) -1; return (Py_UCS4) -1;