summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorINADA Naoki <songofacandy@gmail.com>2017-01-06 08:32:01 (GMT)
committerINADA Naoki <songofacandy@gmail.com>2017-01-06 08:32:01 (GMT)
commita634e23209e90fd516fae1b4a303d57fdb1b9917 (patch)
tree33c88b1473930f36920e7d94f78fe39872b52f93 /Objects
parenta251fb02f4400a2ea32677110e430742fd1444bc (diff)
downloadcpython-a634e23209e90fd516fae1b4a303d57fdb1b9917.zip
cpython-a634e23209e90fd516fae1b4a303d57fdb1b9917.tar.gz
cpython-a634e23209e90fd516fae1b4a303d57fdb1b9917.tar.bz2
Issue #29159: Fix regression in bytes(x) when x.__index__() raises Exception.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/bytearrayobject.c22
-rw-r--r--Objects/bytesobject.c20
2 files changed, 25 insertions, 17 deletions
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c
index c6d0707..a8d6980 100644
--- a/Objects/bytearrayobject.c
+++ b/Objects/bytearrayobject.c
@@ -798,18 +798,22 @@ bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
if (PyIndex_Check(arg)) {
count = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
if (count == -1 && PyErr_Occurred()) {
- return -1;
- }
- if (count < 0) {
- PyErr_SetString(PyExc_ValueError, "negative count");
- return -1;
+ if (PyErr_ExceptionMatches(PyExc_OverflowError))
+ return -1;
+ PyErr_Clear(); /* fall through */
}
- if (count > 0) {
- if (PyByteArray_Resize((PyObject *)self, count))
+ else {
+ if (count < 0) {
+ PyErr_SetString(PyExc_ValueError, "negative count");
return -1;
- memset(PyByteArray_AS_STRING(self), 0, count);
+ }
+ if (count > 0) {
+ if (PyByteArray_Resize((PyObject *)self, count))
+ return -1;
+ memset(PyByteArray_AS_STRING(self), 0, count);
+ }
+ return 0;
}
- return 0;
}
/* Use the buffer API */
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index b22e57e..5d48440 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -2593,16 +2593,20 @@ bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
if (PyIndex_Check(x)) {
size = PyNumber_AsSsize_t(x, PyExc_OverflowError);
if (size == -1 && PyErr_Occurred()) {
- return NULL;
+ if (PyErr_ExceptionMatches(PyExc_OverflowError))
+ return NULL;
+ PyErr_Clear(); /* fall through */
}
- if (size < 0) {
- PyErr_SetString(PyExc_ValueError, "negative count");
- return NULL;
+ else {
+ if (size < 0) {
+ PyErr_SetString(PyExc_ValueError, "negative count");
+ return NULL;
+ }
+ new = _PyBytes_FromSize(size, 1);
+ if (new == NULL)
+ return NULL;
+ return new;
}
- new = _PyBytes_FromSize(size, 1);
- if (new == NULL)
- return NULL;
- return new;
}
return PyBytes_FromObject(x);