Commit ded39f2a authored by Kevin Modzelewski's avatar Kevin Modzelewski

Merge pull request #766 from corona10/recursive

implement recursive printing of set and tuples.
parents 75e203a2 9f45f86f
......@@ -99,6 +99,14 @@ static Box* _setRepr(BoxedSet* self, const char* type_name) {
std::string O("");
llvm::raw_string_ostream os(O);
int status = Py_ReprEnter((PyObject*)self);
if (status != 0) {
if (status < 0)
return boxString(os.str());
os << type_name << "(...)";
return boxString(os.str());
}
os << type_name << "([";
bool first = true;
for (Box* elt : self->s) {
......@@ -109,6 +117,7 @@ static Box* _setRepr(BoxedSet* self, const char* type_name) {
first = false;
}
os << "])";
Py_ReprLeave((PyObject*)self);
return boxString(os.str());
}
......@@ -548,6 +557,7 @@ void setupSet() {
frozenset_cls->giveAttr("__nonzero__", set_cls->getattr(internStringMortal("__nonzero__")));
frozenset_cls->giveAttr("__hash__", new BoxedFunction(boxRTFunction((void*)setHash, BOXED_INT, 1)));
set_cls->giveAttr("__hash__", None);
set_cls->giveAttr("add", new BoxedFunction(boxRTFunction((void*)setAdd, NONE, 2)));
set_cls->giveAttr("remove", new BoxedFunction(boxRTFunction((void*)setRemove, NONE, 2)));
......
......@@ -203,11 +203,27 @@ extern "C" Py_ssize_t PyTuple_Size(PyObject* op) noexcept {
Box* tupleRepr(BoxedTuple* t) {
assert(isSubclass(t->cls, tuple_cls));
int n;
std::string O("");
llvm::raw_string_ostream os(O);
n = t->size();
if (n == 0) {
os << "()";
return boxString(os.str());
}
int status = Py_ReprEnter((PyObject*)t);
if (status != 0) {
if (status < 0)
return boxString(os.str());
os << "(...)";
return boxString(os.str());
}
os << "(";
int n = t->size();
for (int i = 0; i < n; i++) {
if (i)
os << ", ";
......@@ -219,6 +235,7 @@ Box* tupleRepr(BoxedTuple* t) {
os << ",";
os << ")";
Py_ReprLeave((PyObject*)t);
return boxString(os.str());
}
......
# should_error
set1 = set([1,2,3,4])
set2 = set([5,6,7])
set1.add(set2)
class C(object):
def __init__(self):
self.t = set([self])
def __repr__(self):
return repr(self.t)
print repr(C())
......@@ -224,3 +224,11 @@ except ValueError as e:
n = float('nan')
print n in (n, n)
#recursive printing test
class C(object):
def __init__(self):
self.t = (self,)
def __repr__(self):
return repr(self.t)
print repr(C())
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