Commit d034a63e authored by Chris Toshok's avatar Chris Toshok

make BoxedTraceback hold a single line, instead of a vector. rename...

make BoxedTraceback hold a single line, instead of a vector.  rename BoxedTraceback::Here to BoxedTraceback::here
parent e7bd664b
...@@ -518,7 +518,7 @@ public: ...@@ -518,7 +518,7 @@ public:
exc_info.reraise = false; exc_info.reraise = false;
return; return;
} }
BoxedTraceback::Here(line_info, reinterpret_cast<BoxedTraceback**>(&exc_info.traceback)); BoxedTraceback::here(line_info, &exc_info.traceback);
} }
void logException() { void logException() {
...@@ -614,7 +614,7 @@ void exceptionCaughtInInterpreter(LineInfo line_info, ExcInfo* exc_info) { ...@@ -614,7 +614,7 @@ void exceptionCaughtInInterpreter(LineInfo line_info, ExcInfo* exc_info) {
exc_info->reraise = false; exc_info->reraise = false;
return; return;
} }
BoxedTraceback::Here(line_info, reinterpret_cast<BoxedTraceback**>(&exc_info->traceback)); BoxedTraceback::here(line_info, &exc_info->traceback);
} }
void unwindingThroughFrame(PythonUnwindSession* unwind_session, unw_cursor_t* cursor) { void unwindingThroughFrame(PythonUnwindSession* unwind_session, unw_cursor_t* cursor) {
...@@ -754,18 +754,16 @@ BoxedTraceback* getTraceback() { ...@@ -754,18 +754,16 @@ BoxedTraceback* getTraceback() {
Timer _t("getTraceback", 1000); Timer _t("getTraceback", 1000);
BoxedTraceback::LinesVector entries; Box* tb = new BoxedTraceback();
unwindPythonStack([&](PythonFrameIteratorImpl* frame_iter) { unwindPythonStack([&](PythonFrameIteratorImpl* frame_iter) {
entries.push_back(lineInfoForFrame(frame_iter)); BoxedTraceback::here(lineInfoForFrame(frame_iter), &tb);
return false; return false;
}); });
std::reverse(entries.begin(), entries.end());
long us = _t.end(); long us = _t.end();
us_gettraceback.log(us); us_gettraceback.log(us);
return new BoxedTraceback(std::move(entries)); return static_cast<BoxedTraceback*>(tb);
} }
ExcInfo* getFrameExcInfo() { ExcInfo* getFrameExcInfo() {
......
...@@ -55,8 +55,7 @@ void raiseExc(Box* exc_obj) { ...@@ -55,8 +55,7 @@ void raiseExc(Box* exc_obj) {
void raiseSyntaxError(const char* msg, int lineno, int col_offset, llvm::StringRef file, llvm::StringRef func) { void raiseSyntaxError(const char* msg, int lineno, int col_offset, llvm::StringRef file, llvm::StringRef func) {
Box* exc = runtimeCall(SyntaxError, ArgPassSpec(1), boxString(msg), NULL, NULL, NULL, NULL); Box* exc = runtimeCall(SyntaxError, ArgPassSpec(1), boxString(msg), NULL, NULL, NULL, NULL);
auto tb = new BoxedTraceback(); auto tb = new BoxedTraceback(LineInfo(lineno, col_offset, file, func), None);
tb->addLine(LineInfo(lineno, col_offset, file, func));
throw ExcInfo(exc->cls, exc, tb); throw ExcInfo(exc->cls, exc, tb);
} }
......
...@@ -57,48 +57,43 @@ void printTraceback(Box* b) { ...@@ -57,48 +57,43 @@ void printTraceback(Box* b) {
fprintf(stderr, "Traceback (most recent call last):\n"); fprintf(stderr, "Traceback (most recent call last):\n");
for (; tb && tb != None; tb = static_cast<BoxedTraceback*>(tb->tb_next)) { for (; tb && tb != None; tb = static_cast<BoxedTraceback*>(tb->tb_next)) {
for (auto line : tb->lines) { auto& line = tb->line;
fprintf(stderr, " File \"%s\", line %d, in %s:\n", line.file.c_str(), line.line, line.func.c_str()); fprintf(stderr, " File \"%s\", line %d, in %s:\n", line.file.c_str(), line.line, line.func.c_str());
if (line.line < 0) if (line.line < 0)
continue; continue;
FILE* f = fopen(line.file.c_str(), "r"); FILE* f = fopen(line.file.c_str(), "r");
if (f) { if (f) {
assert(line.line < 10000000 && "Refusing to try to seek that many lines forward"); assert(line.line < 10000000 && "Refusing to try to seek that many lines forward");
for (int i = 1; i < line.line; i++) { for (int i = 1; i < line.line; i++) {
char* buf = NULL;
size_t size;
size_t r = getline(&buf, &size, f);
if (r != -1)
free(buf);
}
char* buf = NULL; char* buf = NULL;
size_t size; size_t size;
size_t r = getline(&buf, &size, f); size_t r = getline(&buf, &size, f);
if (r != -1) { if (r != -1)
while (buf[r - 1] == '\n' or buf[r - 1] == '\r')
r--;
char* ptr = buf;
while (*ptr == ' ' || *ptr == '\t') {
ptr++;
r--;
}
fprintf(stderr, " %.*s\n", (int)r, ptr);
free(buf); free(buf);
}
char* buf = NULL;
size_t size;
size_t r = getline(&buf, &size, f);
if (r != -1) {
while (buf[r - 1] == '\n' or buf[r - 1] == '\r')
r--;
char* ptr = buf;
while (*ptr == ' ' || *ptr == '\t') {
ptr++;
r--;
} }
fclose(f);
fprintf(stderr, " %.*s\n", (int)r, ptr);
free(buf);
} }
fclose(f);
} }
} }
} }
void BoxedTraceback::addLine(const LineInfo line) {
lines.insert(lines.begin(), line);
}
Box* BoxedTraceback::getLines(Box* b) { Box* BoxedTraceback::getLines(Box* b) {
assert(b->cls == traceback_cls); assert(b->cls == traceback_cls);
...@@ -107,8 +102,8 @@ Box* BoxedTraceback::getLines(Box* b) { ...@@ -107,8 +102,8 @@ Box* BoxedTraceback::getLines(Box* b) {
if (!tb->py_lines) { if (!tb->py_lines) {
BoxedList* lines = new BoxedList(); BoxedList* lines = new BoxedList();
for (BoxedTraceback* wtb = tb; wtb && wtb != None; wtb = static_cast<BoxedTraceback*>(wtb->tb_next)) { for (BoxedTraceback* wtb = tb; wtb && wtb != None; wtb = static_cast<BoxedTraceback*>(wtb->tb_next)) {
lines->ensure(wtb->lines.size()); if (wtb->has_line) {
for (auto& line : wtb->lines) { auto& line = wtb->line;
auto l = BoxedTuple::create({ boxString(line.file), boxString(line.func), boxInt(line.line) }); auto l = BoxedTuple::create({ boxString(line.file), boxString(line.func), boxInt(line.line) });
listAppendInternal(lines, l); listAppendInternal(lines, l);
} }
...@@ -119,9 +114,8 @@ Box* BoxedTraceback::getLines(Box* b) { ...@@ -119,9 +114,8 @@ Box* BoxedTraceback::getLines(Box* b) {
return tb->py_lines; return tb->py_lines;
} }
void BoxedTraceback::Here(LineInfo lineInfo, BoxedTraceback** tb) { void BoxedTraceback::here(LineInfo lineInfo, Box** tb) {
*tb = new BoxedTraceback(*tb); *tb = new BoxedTraceback(lineInfo, *tb);
(*tb)->addLine(lineInfo);
} }
void setupTraceback() { void setupTraceback() {
......
...@@ -27,25 +27,22 @@ class GCVisitor; ...@@ -27,25 +27,22 @@ class GCVisitor;
extern "C" BoxedClass* traceback_cls; extern "C" BoxedClass* traceback_cls;
class BoxedTraceback : public Box { class BoxedTraceback : public Box {
public: public:
typedef llvm::SmallVector<LineInfo, 1> LinesVector;
Box* tb_next; Box* tb_next;
LinesVector lines; bool has_line;
LineInfo line;
Box* py_lines; Box* py_lines;
BoxedTraceback(LinesVector&& lines) : tb_next(None), lines(std::move(lines)), py_lines(NULL) {} BoxedTraceback(LineInfo line, Box* tb_next) : tb_next(tb_next), has_line(true), line(line), py_lines(NULL) {}
BoxedTraceback(BoxedTraceback* tb_next) : tb_next(tb_next), py_lines(NULL) {} BoxedTraceback() : tb_next(None), has_line(false), line(-1, -1, "", ""), py_lines(NULL) {}
BoxedTraceback() : tb_next(None), py_lines(NULL) {}
DEFAULT_CLASS(traceback_cls); DEFAULT_CLASS(traceback_cls);
void addLine(const LineInfo line);
static Box* getLines(Box* b); static Box* getLines(Box* b);
static void gcHandler(gc::GCVisitor* v, Box* b); static void gcHandler(gc::GCVisitor* v, Box* b);
// somewhat equivalent to PyTraceBack_Here // somewhat equivalent to PyTraceBack_Here
static void Here(LineInfo lineInfo, BoxedTraceback** tb); static void here(LineInfo lineInfo, Box** tb);
}; };
void printTraceback(Box* b); void printTraceback(Box* b);
......
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