summaryrefslogtreecommitdiff
path: root/Objects/bytearrayobject.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2020-06-29 22:36:41 +0300
committerGitHub <noreply@github.com>2020-06-29 22:36:41 +0300
commite67f7db3c34f70536f36c56bb82e33c3512a53a3 (patch)
tree51530f17f218fde20aca7866d663b46bac614305 /Objects/bytearrayobject.c
parent5b96370030707b68e8a5b787e933654297ddbc98 (diff)
downloadcpython-git-e67f7db3c34f70536f36c56bb82e33c3512a53a3.tar.gz
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.
Diffstat (limited to 'Objects/bytearrayobject.c')
-rw-r--r--Objects/bytearrayobject.c19
1 files changed, 6 insertions, 13 deletions
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c
index 5a803be627..83c79b200a 100644
--- a/Objects/bytearrayobject.c
+++ b/Objects/bytearrayobject.c
@@ -22,22 +22,15 @@ char _PyByteArray_empty_string[] = "";
static int
_getbytevalue(PyObject* arg, int *value)
{
- long face_value;
+ int overflow;
+ long face_value = PyLong_AsLongAndOverflow(arg, &overflow);
- if (PyLong_Check(arg)) {
- face_value = PyLong_AsLong(arg);
- } else {
- PyObject *index = PyNumber_Index(arg);
- if (index == NULL) {
- *value = -1;
- return 0;
- }
- face_value = PyLong_AsLong(index);
- Py_DECREF(index);
+ if (face_value == -1 && PyErr_Occurred()) {
+ *value = -1;
+ return 0;
}
-
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)");
*value = -1;
return 0;