Commit 12100fee authored by Kevin Modzelewski's avatar Kevin Modzelewski

Fix some list behavior and add list.__ne__

parent 3f33a0eb
......@@ -508,6 +508,13 @@ Box* _listCmp(BoxedList* lhs, BoxedList* rhs, AST_TYPE::AST_TYPE op_type) {
bool is_order
= (op_type == AST_TYPE::Lt || op_type == AST_TYPE::LtE || op_type == AST_TYPE::Gt || op_type == AST_TYPE::GtE);
if (lsz != rsz) {
if (op_type == AST_TYPE::Eq)
return False;
if (op_type == AST_TYPE::NotEq)
return True;
}
int n = std::min(lsz, rsz);
for (int i = 0; i < n; i++) {
Box* is_eq = compareInternal(lhs->elts->elts[i], rhs->elts->elts[i], AST_TYPE::Eq, NULL);
......@@ -552,6 +559,16 @@ Box* listEq(BoxedList* self, Box* rhs) {
return _listCmp(self, static_cast<BoxedList*>(rhs), AST_TYPE::Eq);
}
Box* listNe(BoxedList* self, Box* rhs) {
if (rhs->cls != list_cls) {
return NotImplemented;
}
LOCK_REGION(self->lock.asRead());
return _listCmp(self, static_cast<BoxedList*>(rhs), AST_TYPE::NotEq);
}
void setupList() {
list_iterator_cls = new BoxedHeapClass(type_cls, object_cls, &listIteratorGCHandler, 0, sizeof(BoxedList), false);
......@@ -569,6 +586,7 @@ void setupList() {
new BoxedFunction(boxRTFunction((void*)listIter, typeFromClass(list_iterator_cls), 1)));
list_cls->giveAttr("__eq__", new BoxedFunction(boxRTFunction((void*)listEq, UNKNOWN, 2)));
list_cls->giveAttr("__ne__", new BoxedFunction(boxRTFunction((void*)listNe, UNKNOWN, 2)));
list_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)listRepr, STR, 1)));
list_cls->giveAttr("__str__", list_cls->getattr("__repr__"));
......
......@@ -134,6 +134,9 @@ static void _printTraceback(const std::vector<const LineInfo*>& tb) {
for (auto line : tb) {
fprintf(stderr, " File \"%s\", line %d, in %s:\n", line->file.c_str(), line->line, line->func.c_str());
if (line->line < 0)
continue;
FILE* f = fopen(line->file.c_str(), "r");
if (f) {
assert(line->line < 10000000 && "Refusing to try to seek that many lines forward");
......
......@@ -88,3 +88,11 @@ class Reverse(object):
return self.n <= rhs.n
print Reverse(4) > Reverse(3), Reverse(4) > Reverse(4)
class EqOnly(object):
def __eq__(self, rhs):
print "eq"
return False
print EqOnly() == 1
print EqOnly() != 1
......@@ -76,3 +76,14 @@ print l
x = [0, 1, 2]
print 2 * x
print x * 2
print range(5) == range(5)
print range(5) == range(4)
class C(object):
def __eq__(self, rhs):
print "C.eq"
return False
# Should not call C().__eq__
print [C()] == [1, 2]
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