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

using try-catch block for throwing exceptions

and remove raw_ostream for displaying data structure
parent 4c743cb6
......@@ -32,21 +32,31 @@ using pyston::ExceptionStyle::ExceptionStyle;
Box* dictRepr(BoxedDict* self) {
int status = Py_ReprEnter((PyObject*)self);
std::vector<char> chars;
if (status != 0) {
if (status < 0)
return boxString(llvm::StringRef(&chars[0], chars.size()));
try {
int status = Py_ReprEnter((PyObject*)self);
if (status != 0) {
if (status < 0)
throwCAPIException();
chars.push_back('{');
chars.push_back('.');
chars.push_back('.');
chars.push_back('.');
chars.push_back('}');
chars.push_back('{');
chars.push_back('.');
chars.push_back('.');
chars.push_back('.');
chars.push_back('}');
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('{');
bool first = true;
for (const auto& p : self->d) {
if (!first) {
......
......@@ -17,8 +17,6 @@
#include <algorithm>
#include <cstring>
#include "llvm/Support/raw_ostream.h"
#include "capi/types.h"
#include "core/ast.h"
#include "core/common.h"
......@@ -63,36 +61,46 @@ extern "C" PyObject* PyList_AsTuple(PyObject* v) noexcept {
}
extern "C" Box* listRepr(BoxedList* self) {
// TODO highly inefficient with all the string copying
std::string O("");
llvm::raw_string_ostream os(O);
std::vector<char> chars;
// Implementing Recursive Print of list in same way from Cpython
int recursive = Py_ReprEnter((PyObject*)self);
if (recursive != 0) {
if (recursive < 0)
return boxString(os.str());
os << "[...]";
return boxString(os.str());
try {
int status = Py_ReprEnter((PyObject*)self);
if (status != 0) {
if (status < 0)
throwCAPIException();
chars.push_back('[');
chars.push_back('.');
chars.push_back('.');
chars.push_back('.');
chars.push_back(']');
return boxString(llvm::StringRef(&chars[0], chars.size()));
}
} catch (ExcInfo e) {
setCAPIException(e);
Py_ReprLeave((PyObject*)self);
return NULL;
}
os << '[';
chars.push_back('[');
for (int i = 0; i < self->size; i++) {
if (i > 0)
os << ", ";
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);
os << s->s();
chars.insert(chars.end(), s->s().begin(), s->s().end());
}
os << ']';
chars.push_back(']');
Py_ReprLeave((PyObject*)self);
return boxString(os.str());
return boxString(llvm::StringRef(&chars[0], chars.size()));
}
extern "C" Box* listNonzero(BoxedList* self) {
......
......@@ -14,8 +14,6 @@
#include "runtime/set.h"
#include <llvm/Support/raw_ostream.h>
#include "gc/collector.h"
#include "runtime/objmodel.h"
......@@ -96,29 +94,54 @@ Box* setNew(Box* _cls, Box* container) {
}
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());
std::vector<char> chars;
try {
int status = Py_ReprEnter((PyObject*)self);
if (status != 0) {
if (status < 0)
throwCAPIException();
std::string ty = std::string(type_name);
chars.insert(chars.end(), ty.begin(), ty.end());
os << type_name << "(...)";
return boxString(os.str());
chars.push_back('(');
chars.push_back('.');
chars.push_back('.');
chars.push_back('.');
chars.push_back(')');
return boxString(llvm::StringRef(&chars[0], chars.size()));
}
} catch (ExcInfo e) {
setCAPIException(e);
Py_ReprLeave((PyObject*)self);
return NULL;
}
os << type_name << "([";
std::string ty = std::string(type_name);
chars.insert(chars.end(), ty.begin(), ty.end());
chars.push_back('(');
chars.push_back('[');
bool first = true;
for (Box* elt : self->s) {
if (!first) {
os << ", ";
chars.push_back(',');
chars.push_back(' ');
}
os << static_cast<BoxedString*>(repr(elt))->s();
BoxedString* str = static_cast<BoxedString*>(repr(elt));
chars.insert(chars.end(), str->s().begin(), str->s().end());
first = false;
}
os << "])";
chars.push_back(']');
chars.push_back(')');
Py_ReprLeave((PyObject*)self);
return boxString(os.str());
return boxString(llvm::StringRef(&chars[0], chars.size()));
}
Box* setRepr(BoxedSet* self) {
......
......@@ -16,8 +16,6 @@
#include <algorithm>
#include "llvm/Support/raw_ostream.h"
#include "capi/typeobject.h"
#include "core/ast.h"
#include "core/common.h"
......@@ -204,39 +202,56 @@ Box* tupleRepr(BoxedTuple* t) {
assert(isSubclass(t->cls, tuple_cls));
int n;
std::string O("");
llvm::raw_string_ostream os(O);
std::vector<char> chars;
n = t->size();
if (n == 0) {
os << "()";
return boxString(os.str());
chars.push_back('(');
chars.push_back(')');
return boxString(llvm::StringRef(&chars[0], chars.size()));
}
int status = Py_ReprEnter((PyObject*)t);
if (status != 0) {
if (status < 0)
return boxString(os.str());
try {
int status = Py_ReprEnter((PyObject*)t);
if (status != 0) {
if (status < 0)
throwCAPIException();
chars.push_back('(');
chars.push_back('.');
chars.push_back('.');
chars.push_back('.');
chars.push_back(')');
return boxString(llvm::StringRef(&chars[0], chars.size()));
}
} catch (ExcInfo e) {
setCAPIException(e);
Py_ReprLeave((PyObject*)t);
os << "(...)";
return boxString(os.str());
return NULL;
}
os << "(";
chars.push_back('(');
for (int i = 0; i < n; i++) {
if (i)
os << ", ";
if (i) {
chars.push_back(',');
chars.push_back(' ');
}
BoxedString* elt_repr = static_cast<BoxedString*>(repr(t->elts[i]));
os << elt_repr->s();
chars.insert(chars.end(), elt_repr->s().begin(), elt_repr->s().end());
}
if (n == 1)
os << ",";
os << ")";
chars.push_back(',');
chars.push_back(')');
Py_ReprLeave((PyObject*)t);
return boxString(os.str());
return boxString(llvm::StringRef(&chars[0], chars.size()));
}
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