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