Commit d99f473e authored by Dong-hee,Na's avatar Dong-hee,Na

modify exeception handling during repr

parent 86096848
......@@ -32,10 +32,11 @@ using pyston::ExceptionStyle::ExceptionStyle;
Box* dictRepr(BoxedDict* self) {
std::vector<char> chars;
try {
std::vector<char> chars;
int status = Py_ReprEnter((PyObject*)self);
if (status != 0) {
if (status < 0)
throwCAPIException();
......@@ -49,32 +50,38 @@ Box* dictRepr(BoxedDict* self) {
return boxString(llvm::StringRef(&chars[0], chars.size()));
}
} catch (ExcInfo e) {
setCAPIException(e);
Py_ReprLeave((PyObject*)self);
return NULL;
}
chars.push_back('{');
chars.push_back('{');
bool first = true;
for (const auto& p : self->d) {
if (!first) {
chars.push_back(',');
bool first = true;
for (const auto& p : self->d) {
if (!first) {
chars.push_back(',');
chars.push_back(' ');
}
first = false;
BoxedString* k = static_cast<BoxedString*>(repr(p.first));
BoxedString* v = static_cast<BoxedString*>(repr(p.second));
chars.insert(chars.end(), k->s().begin(), k->s().end());
chars.push_back(':');
chars.push_back(' ');
chars.insert(chars.end(), v->s().begin(), v->s().end());
}
first = false;
BoxedString* k = static_cast<BoxedString*>(repr(p.first));
BoxedString* v = static_cast<BoxedString*>(repr(p.second));
chars.insert(chars.end(), k->s().begin(), k->s().end());
chars.push_back(':');
chars.push_back(' ');
chars.insert(chars.end(), v->s().begin(), v->s().end());
}
chars.push_back('}');
Py_ReprLeave((PyObject*)self);
return boxString(llvm::StringRef(&chars[0], chars.size()));
chars.push_back('}');
Py_ReprLeave((PyObject*)self);
return boxString(llvm::StringRef(&chars[0], chars.size()));
} catch (ExcInfo e) {
Py_ReprLeave((PyObject*)self);
throw e;
}
}
Box* dictClear(BoxedDict* self) {
......
......@@ -62,12 +62,13 @@ extern "C" PyObject* PyList_AsTuple(PyObject* v) noexcept {
extern "C" Box* listRepr(BoxedList* self) {
std::vector<char> chars;
// Implementing Recursive Print of list in same way from Cpython
try {
std::vector<char> chars;
int status = Py_ReprEnter((PyObject*)self);
if (status != 0) {
if (status < 0)
throwCAPIException();
......@@ -79,28 +80,35 @@ extern "C" Box* listRepr(BoxedList* self) {
return boxString(llvm::StringRef(&chars[0], chars.size()));
}
} catch (ExcInfo e) {
setCAPIException(e);
Py_ReprLeave((PyObject*)self);
return NULL;
}
chars.push_back('[');
for (int i = 0; i < self->size; i++) {
if (i > 0) {
chars.push_back(',');
chars.push_back(' ');
chars.push_back('[');
for (int i = 0; i < self->size; i++) {
if (i > 0) {
chars.push_back(',');
chars.push_back(' ');
}
Box* r = self->elts->elts[i]->reprICAsString();
assert(r->cls == str_cls);
BoxedString* s = static_cast<BoxedString*>(r);
chars.insert(chars.end(), s->s().begin(), s->s().end());
}
Box* r = self->elts->elts[i]->reprICAsString();
chars.push_back(']');
assert(r->cls == str_cls);
BoxedString* s = static_cast<BoxedString*>(r);
chars.insert(chars.end(), s->s().begin(), s->s().end());
}
chars.push_back(']');
Py_ReprLeave((PyObject*)self);
Py_ReprLeave((PyObject*)self);
return boxString(llvm::StringRef(&chars[0], chars.size()));
return boxString(llvm::StringRef(&chars[0], chars.size()));
} catch (ExcInfo e) {
Py_ReprLeave((PyObject*)self);
throw e;
}
}
extern "C" Box* listNonzero(BoxedList* self) {
......
......@@ -95,11 +95,13 @@ Box* setNew(Box* _cls, Box* container) {
static Box* _setRepr(BoxedSet* self, const char* type_name) {
std::vector<char> chars;
try {
std::vector<char> chars;
int status = Py_ReprEnter((PyObject*)self);
if (status != 0) {
if (status < 0)
throwCAPIException();
......@@ -114,34 +116,34 @@ static Box* _setRepr(BoxedSet* self, const char* type_name) {
return boxString(llvm::StringRef(&chars[0], chars.size()));
}
} catch (ExcInfo e) {
setCAPIException(e);
Py_ReprLeave((PyObject*)self);
return NULL;
}
std::string ty = std::string(type_name);
chars.insert(chars.end(), ty.begin(), ty.end());
std::string ty = std::string(type_name);
chars.insert(chars.end(), ty.begin(), ty.end());
chars.push_back('(');
chars.push_back('[');
chars.push_back('(');
chars.push_back('[');
bool first = true;
for (Box* elt : self->s) {
bool first = true;
for (Box* elt : self->s) {
if (!first) {
chars.push_back(',');
chars.push_back(' ');
}
if (!first) {
chars.push_back(',');
chars.push_back(' ');
BoxedString* str = static_cast<BoxedString*>(repr(elt));
chars.insert(chars.end(), str->s().begin(), str->s().end());
first = false;
}
chars.push_back(']');
chars.push_back(')');
Py_ReprLeave((PyObject*)self);
return boxString(llvm::StringRef(&chars[0], chars.size()));
BoxedString* str = static_cast<BoxedString*>(repr(elt));
chars.insert(chars.end(), str->s().begin(), str->s().end());
first = false;
} catch (ExcInfo e) {
Py_ReprLeave((PyObject*)self);
throw e;
}
chars.push_back(']');
chars.push_back(')');
Py_ReprLeave((PyObject*)self);
return boxString(llvm::StringRef(&chars[0], chars.size()));
}
Box* setRepr(BoxedSet* self) {
......
......@@ -201,18 +201,19 @@ extern "C" Py_ssize_t PyTuple_Size(PyObject* op) noexcept {
Box* tupleRepr(BoxedTuple* t) {
assert(isSubclass(t->cls, tuple_cls));
int n;
std::vector<char> chars;
n = t->size();
if (n == 0) {
chars.push_back('(');
chars.push_back(')');
return boxString(llvm::StringRef(&chars[0], chars.size()));
}
try {
int n;
std::vector<char> chars;
int status = Py_ReprEnter((PyObject*)t);
n = t->size();
if (n == 0) {
chars.push_back('(');
chars.push_back(')');
return boxString(llvm::StringRef(&chars[0], chars.size()));
}
if (status != 0) {
if (status < 0)
throwCAPIException();
......@@ -226,32 +227,30 @@ Box* tupleRepr(BoxedTuple* t) {
return boxString(llvm::StringRef(&chars[0], chars.size()));
}
} catch (ExcInfo e) {
setCAPIException(e);
Py_ReprLeave((PyObject*)t);
return NULL;
}
chars.push_back('(');
chars.push_back('(');
for (int i = 0; i < n; i++) {
if (i) {
chars.push_back(',');
chars.push_back(' ');
}
BoxedString* elt_repr = static_cast<BoxedString*>(repr(t->elts[i]));
chars.insert(chars.end(), elt_repr->s().begin(), elt_repr->s().end());
}
for (int i = 0; i < n; i++) {
if (i) {
if (n == 1)
chars.push_back(',');
chars.push_back(' ');
}
BoxedString* elt_repr = static_cast<BoxedString*>(repr(t->elts[i]));
chars.insert(chars.end(), elt_repr->s().begin(), elt_repr->s().end());
}
if (n == 1)
chars.push_back(',');
chars.push_back(')');
Py_ReprLeave((PyObject*)t);
chars.push_back(')');
Py_ReprLeave((PyObject*)t);
return boxString(llvm::StringRef(&chars[0], chars.size()));
return boxString(llvm::StringRef(&chars[0], chars.size()));
} catch (ExcInfo e) {
Py_ReprLeave((PyObject*)t);
throw e;
}
}
Box* tupleNonzero(BoxedTuple* self) {
......
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