Commit 005eac26 authored by Boxiang Sun's avatar Boxiang Sun

in tuple operation, if the op type check failed, return NotImplemented instead...

in tuple operation, if the op type check failed, return NotImplemented instead throw exception, let custom type has chance to call their own function
parent bc336b7a
......@@ -169,16 +169,7 @@ Box* tupleAdd(BoxedTuple* self, Box* rhs) {
return rtn;
}
Box* tupleMul(BoxedTuple* self, Box* rhs) {
Py_ssize_t n;
if (PyIndex_Check(rhs)) {
n = PyNumber_AsSsize_t(rhs, PyExc_OverflowError);
if (n == -1 && PyErr_Occurred())
throwCAPIException();
} else {
raiseExcHelper(TypeError, "can't multiply sequence by non-int of type '%s'", getTypeName(rhs));
}
Box* tupleMulInt(BoxedTuple* self, int n) {
int s = self->size();
if (n < 0)
......@@ -197,6 +188,19 @@ Box* tupleMul(BoxedTuple* self, Box* rhs) {
}
}
Box* tupleMul(BoxedTuple* self, Box* rhs) {
Py_ssize_t n;
if (PyIndex_Check(rhs)) {
n = PyNumber_AsSsize_t(rhs, PyExc_OverflowError);
if (n == -1 && PyErr_Occurred())
throwCAPIException();
return tupleMulInt(self, n);
} else {
return NotImplemented;
}
}
Box* tupleLen(BoxedTuple* t) {
assert(PyTuple_Check(t));
return boxInt(t->size());
......@@ -690,8 +694,8 @@ void setupTuple() {
// Return type is UNKNOWN as it could be NotImplemented.
tuple_cls->giveAttr("__add__", new BoxedFunction(FunctionMetadata::create((void*)tupleAdd, UNKNOWN, 2)));
tuple_cls->giveAttr("__mul__", new BoxedFunction(FunctionMetadata::create((void*)tupleMul, BOXED_TUPLE, 2)));
tuple_cls->giveAttr("__rmul__", new BoxedFunction(FunctionMetadata::create((void*)tupleMul, BOXED_TUPLE, 2)));
tuple_cls->giveAttr("__mul__", new BoxedFunction(FunctionMetadata::create((void*)tupleMul, UNKNOWN, 2)));
tuple_cls->giveAttr("__rmul__", new BoxedFunction(FunctionMetadata::create((void*)tupleMul, UNKNOWN, 2)));
tuple_cls->giveAttr("__getnewargs__", new BoxedFunction(FunctionMetadata::create((void*)tuple_getnewargs, UNKNOWN,
1, ParamNames::empty(), CAPI)));
......
......@@ -239,3 +239,24 @@ class C(object):
def __repr__(self):
return repr(self.t)
print repr(C())
try:
(1, 2) + "a"
except TypeError as e:
print(type(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(e.message)
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