Skip to content

Commit e67f7db

Browse files
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.
1 parent 5b96370 commit e67f7db

File tree

3 files changed

+26
-58
lines changed

3 files changed

+26
-58
lines changed

Objects/bytearrayobject.c

+6-13
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,15 @@ char _PyByteArray_empty_string[] = "";
2222
static int
2323
_getbytevalue(PyObject* arg, int *value)
2424
{
25-
long face_value;
25+
int overflow;
26+
long face_value = PyLong_AsLongAndOverflow(arg, &overflow);
2627

27-
if (PyLong_Check(arg)) {
28-
face_value = PyLong_AsLong(arg);
29-
} else {
30-
PyObject *index = PyNumber_Index(arg);
31-
if (index == NULL) {
32-
*value = -1;
33-
return 0;
34-
}
35-
face_value = PyLong_AsLong(index);
36-
Py_DECREF(index);
28+
if (face_value == -1 && PyErr_Occurred()) {
29+
*value = -1;
30+
return 0;
3731
}
38-
3932
if (face_value < 0 || face_value >= 256) {
40-
/* this includes the OverflowError in case the long is too large */
33+
/* this includes an overflow in converting to C long */
4134
PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
4235
*value = -1;
4336
return 0;

Objects/bytesobject.c

+9-22
Original file line numberDiff line numberDiff line change
@@ -510,17 +510,14 @@ formatlong(PyObject *v, int flags, int prec, int type)
510510
iobj = _PyNumber_Index(v);
511511
else
512512
iobj = PyNumber_Long(v);
513-
if (iobj == NULL) {
514-
if (!PyErr_ExceptionMatches(PyExc_TypeError))
515-
return NULL;
516-
}
517-
else if (!PyLong_Check(iobj))
518-
Py_CLEAR(iobj);
519513
if (iobj != NULL) {
514+
assert(PyLong_Check(iobj));
520515
result = _PyUnicode_FormatLong(iobj, flags & F_ALT, prec, type);
521516
Py_DECREF(iobj);
522517
return result;
523518
}
519+
if (!PyErr_ExceptionMatches(PyExc_TypeError))
520+
return NULL;
524521
}
525522
PyErr_Format(PyExc_TypeError,
526523
"%%%c format: %s is required, not %.200s", type,
@@ -542,26 +539,16 @@ byte_converter(PyObject *arg, char *p)
542539
return 1;
543540
}
544541
else {
545-
PyObject *iobj;
546-
long ival;
547542
int overflow;
548-
/* make sure number is a type of integer */
549-
if (PyLong_Check(arg)) {
550-
ival = PyLong_AsLongAndOverflow(arg, &overflow);
551-
}
552-
else {
553-
iobj = PyNumber_Index(arg);
554-
if (iobj == NULL) {
555-
if (!PyErr_ExceptionMatches(PyExc_TypeError))
556-
return 0;
543+
long ival = PyLong_AsLongAndOverflow(arg, &overflow);
544+
if (ival == -1 && PyErr_Occurred()) {
545+
if (PyErr_ExceptionMatches(PyExc_TypeError)) {
557546
goto onError;
558547
}
559-
ival = PyLong_AsLongAndOverflow(iobj, &overflow);
560-
Py_DECREF(iobj);
548+
return 0;
561549
}
562-
if (!overflow && ival == -1 && PyErr_Occurred())
563-
goto onError;
564-
if (overflow || !(0 <= ival && ival <= 255)) {
550+
if (!(0 <= ival && ival <= 255)) {
551+
/* this includes an overflow in converting to C long */
565552
PyErr_SetString(PyExc_OverflowError,
566553
"%c arg not in range(256)");
567554
return 0;

Objects/unicodeobject.c

+11-23
Original file line numberDiff line numberDiff line change
@@ -14641,19 +14641,14 @@ mainformatlong(PyObject *v,
1464114641
if (!PyLong_Check(v)) {
1464214642
if (type == 'o' || type == 'x' || type == 'X') {
1464314643
iobj = _PyNumber_Index(v);
14644-
if (iobj == NULL) {
14645-
if (PyErr_ExceptionMatches(PyExc_TypeError))
14646-
goto wrongtype;
14647-
return -1;
14648-
}
1464914644
}
1465014645
else {
1465114646
iobj = PyNumber_Long(v);
14652-
if (iobj == NULL ) {
14653-
if (PyErr_ExceptionMatches(PyExc_TypeError))
14654-
goto wrongtype;
14655-
return -1;
14656-
}
14647+
}
14648+
if (iobj == NULL ) {
14649+
if (PyErr_ExceptionMatches(PyExc_TypeError))
14650+
goto wrongtype;
14651+
return -1;
1465714652
}
1465814653
assert(PyLong_Check(iobj));
1465914654
}
@@ -14736,24 +14731,17 @@ formatchar(PyObject *v)
1473614731
goto onError;
1473714732
}
1473814733
else {
14739-
PyObject *iobj;
14740-
long x;
14741-
/* make sure number is a type of integer */
14742-
if (!PyLong_Check(v)) {
14743-
iobj = PyNumber_Index(v);
14744-
if (iobj == NULL) {
14734+
int overflow;
14735+
long x = PyLong_AsLongAndOverflow(v, &overflow);
14736+
if (x == -1 && PyErr_Occurred()) {
14737+
if (PyErr_ExceptionMatches(PyExc_TypeError)) {
1474514738
goto onError;
1474614739
}
14747-
x = PyLong_AsLong(iobj);
14748-
Py_DECREF(iobj);
14749-
}
14750-
else {
14751-
x = PyLong_AsLong(v);
14740+
return (Py_UCS4) -1;
1475214741
}
14753-
if (x == -1 && PyErr_Occurred())
14754-
goto onError;
1475514742

1475614743
if (x < 0 || x > MAX_UNICODE) {
14744+
/* this includes an overflow in converting to C long */
1475714745
PyErr_SetString(PyExc_OverflowError,
1475814746
"%c arg not in range(0x110000)");
1475914747
return (Py_UCS4) -1;

0 commit comments

Comments
 (0)