Commit bc336b7a authored by Boxiang Sun's avatar Boxiang Sun

throw exception in list multiply only if the second op is indexable, otherwise...

throw exception in list multiply only if the second op is indexable, otherwise return NotImplemented
parent 875432d4
......@@ -691,11 +691,15 @@ extern "C" int PyList_Insert(PyObject* op, Py_ssize_t where, PyObject* newitem)
}
Box* listMul(BoxedList* self, Box* rhs) {
static BoxedString* index_str = internStringImmortal("__index__");
Py_ssize_t n;
Py_ssize_t n = PyNumber_AsSsize_t(rhs, PyExc_IndexError);
if (n == -1 && PyErr_Occurred())
throwCAPIException();
if (PyIndex_Check(rhs)) {
n = PyNumber_AsSsize_t(rhs, PyExc_OverflowError);
if (n == -1 && PyErr_Occurred())
throwCAPIException();
} else {
return NotImplemented;
}
int s = self->size;
......@@ -715,11 +719,15 @@ Box* listMul(BoxedList* self, Box* rhs) {
}
Box* listImul(BoxedList* self, Box* rhs) {
static BoxedString* index_str = internStringImmortal("__index__");
Py_ssize_t n;
Py_ssize_t n = PyNumber_AsSsize_t(rhs, PyExc_IndexError);
if (n == -1 && PyErr_Occurred())
throwCAPIException();
if (PyIndex_Check(rhs)) {
n = PyNumber_AsSsize_t(rhs, PyExc_OverflowError);
if (n == -1 && PyErr_Occurred())
throwCAPIException();
} else {
return NotImplemented;
}
int s = self->size;
......
......@@ -219,3 +219,19 @@ try:
RaisingCmp() in [1, 2, 3]
except ZeroDivisionError as e:
print e
class D(object):
def __rmul__(self, other):
return other * 2
d = D()
try:
print([1, 2] * 3.5)
except TypeError as e:
print(type(e))
try:
print([1, 2] * d)
except TypeError as e:
print(type(e))
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment