Commit 68b2ed7a authored by Kevin Modzelewski's avatar Kevin Modzelewski

Merge pull request #1054 from Daetalus/sequence_fixing

Tuple and list calling order fixing.
parents c1f25c37 005eac26
......@@ -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 (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 (PyIndex_Check(rhs)) {
n = PyNumber_AsSsize_t(rhs, PyExc_OverflowError);
if (n == -1 && PyErr_Occurred())
throwCAPIException();
} else {
return NotImplemented;
}
int s = self->size;
......
......@@ -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)));
......
......@@ -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))
......@@ -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