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) ...@@ -691,11 +691,15 @@ extern "C" int PyList_Insert(PyObject* op, Py_ssize_t where, PyObject* newitem)
} }
Box* listMul(BoxedList* self, Box* rhs) { 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)) {
if (n == -1 && PyErr_Occurred()) n = PyNumber_AsSsize_t(rhs, PyExc_OverflowError);
throwCAPIException(); if (n == -1 && PyErr_Occurred())
throwCAPIException();
} else {
return NotImplemented;
}
int s = self->size; int s = self->size;
...@@ -715,11 +719,15 @@ Box* listMul(BoxedList* self, Box* rhs) { ...@@ -715,11 +719,15 @@ Box* listMul(BoxedList* self, Box* rhs) {
} }
Box* listImul(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)) {
if (n == -1 && PyErr_Occurred()) n = PyNumber_AsSsize_t(rhs, PyExc_OverflowError);
throwCAPIException(); if (n == -1 && PyErr_Occurred())
throwCAPIException();
} else {
return NotImplemented;
}
int s = self->size; int s = self->size;
......
...@@ -169,16 +169,7 @@ Box* tupleAdd(BoxedTuple* self, Box* rhs) { ...@@ -169,16 +169,7 @@ Box* tupleAdd(BoxedTuple* self, Box* rhs) {
return rtn; return rtn;
} }
Box* tupleMul(BoxedTuple* self, Box* rhs) { Box* tupleMulInt(BoxedTuple* self, int n) {
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));
}
int s = self->size(); int s = self->size();
if (n < 0) if (n < 0)
...@@ -197,6 +188,19 @@ Box* tupleMul(BoxedTuple* self, Box* rhs) { ...@@ -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) { Box* tupleLen(BoxedTuple* t) {
assert(PyTuple_Check(t)); assert(PyTuple_Check(t));
return boxInt(t->size()); return boxInt(t->size());
...@@ -690,8 +694,8 @@ void setupTuple() { ...@@ -690,8 +694,8 @@ void setupTuple() {
// Return type is UNKNOWN as it could be NotImplemented. // Return type is UNKNOWN as it could be NotImplemented.
tuple_cls->giveAttr("__add__", new BoxedFunction(FunctionMetadata::create((void*)tupleAdd, UNKNOWN, 2))); 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("__mul__", new BoxedFunction(FunctionMetadata::create((void*)tupleMul, UNKNOWN, 2)));
tuple_cls->giveAttr("__rmul__", new BoxedFunction(FunctionMetadata::create((void*)tupleMul, BOXED_TUPLE, 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, tuple_cls->giveAttr("__getnewargs__", new BoxedFunction(FunctionMetadata::create((void*)tuple_getnewargs, UNKNOWN,
1, ParamNames::empty(), CAPI))); 1, ParamNames::empty(), CAPI)));
......
...@@ -219,3 +219,19 @@ try: ...@@ -219,3 +219,19 @@ try:
RaisingCmp() in [1, 2, 3] RaisingCmp() in [1, 2, 3]
except ZeroDivisionError as e: except ZeroDivisionError as e:
print 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): ...@@ -239,3 +239,24 @@ class C(object):
def __repr__(self): def __repr__(self):
return repr(self.t) return repr(self.t)
print repr(C()) 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