<commit_before>from nolang.objects.root import W_Root


class Frame(W_Root):
    def __init__(self, bytecode):
        self.bytecode = bytecode
        if bytecode.module is not None:  # for tests
            self.globals_w = bytecode.module.functions
        self.locals_w = [None] * len(bytecode.varnames)
        self.stack_w = [None] * bytecode.stack_depth
        self.resume_stack = [0] * bytecode.resume_stack_depth
        self.resume_stack_depth = 0
        self.pos = 0

    def populate_args(self, args_w):
        for i in range(len(args_w)):
            self.locals_w[i] = args_w[i]

    def push(self, w_val):
        self.stack_w[self.pos] = w_val
        self.pos += 1

    def pop(self):
        new_pos = self.pos - 1
        w_res = self.stack_w[new_pos]
        assert new_pos >= 0
        self.pos = new_pos
        return w_res

    def peek(self):
        return self.stack_w[self.pos - 1]

    def store_var(self, index):
        self.locals_w[index] = self.pop()


def find_line(bytecode, target_pc):
    src = bytecode.source
    pos = 0
    lineno = 0
    target_position = bytecode.lnotab[target_pc]
    prev_pos = 0
    while pos < target_position:
        prev_pos = pos
        pos = src.find("\n", pos + 1)
        lineno += 1
        if pos < 0:
            return src[prev_pos + 1:], lineno
    return src[prev_pos + 1:pos], lineno


def format_traceback(space, apperr):
    lines = []
    w_exception = apperr.w_exception
    tb_list = []
    tb = apperr.traceback
    while tb:
        tb_list.append(tb)
        tb = tb.next

    for i in range(len(tb_list) - 1, -1, -1):
        tb = tb_list[i]
        line, lineno = find_line(tb.bytecode, tb.position)
        lines.append("file %s, line %d" % (tb.bytecode.filename, lineno))
        lines.append("  " + line)
    lines.append("%s: %s" % (space.type(w_exception).name, w_exception.message))
    lines.append("")
    return "\n".join(lines)<commit_msg>Improved rendering of traceback<commit_after>from nolang.objects.root import W_Root


class Frame(W_Root):
    def __init__(self, bytecode):
        self.bytecode = bytecode
        if bytecode.module is not None:  # for tests
            self.globals_w = bytecode.module.functions
        self.locals_w = [None] * len(bytecode.varnames)
        self.stack_w = [None] * bytecode.stack_depth
        self.resume_stack = [0] * bytecode.resume_stack_depth
        self.resume_stack_depth = 0
        self.pos = 0

    def populate_args(self, args_w):
        for i in range(len(args_w)):
            self.locals_w[i] = args_w[i]

    def push(self, w_val):
        self.stack_w[self.pos] = w_val
        self.pos += 1

    def pop(self):
        new_pos = self.pos - 1
        w_res = self.stack_w[new_pos]
        assert new_pos >= 0
        self.pos = new_pos
        return w_res

    def peek(self):
        return self.stack_w[self.pos - 1]

    def store_var(self, index):
        self.locals_w[index] = self.pop()


def find_line(bytecode, target_pc):
    src = bytecode.source
    pos = 0
    lineno = 0
    target_position = bytecode.lnotab[target_pc]
    prev_pos = 0
    while pos < target_position:
        prev_pos = pos
        pos = src.find("\n", pos + 1)
        lineno += 1
        if pos < 0:
            return src[prev_pos + 1:], lineno
    return src[prev_pos + 1:pos], lineno


def format_traceback(space, apperr):
    lines = []
    w_exception = apperr.w_exception
    tb_list = []
    tb = apperr.traceback
    while tb:
        tb_list.append(tb)
        tb = tb.next

    for i in range(len(tb_list) - 1, -1, -1):
        tb = tb_list[i]
        line, lineno = find_line(tb.bytecode, tb.position)
        lines.append("file \"%s\", line %d" % (tb.bytecode.filename, lineno))
        lines.append("  " + line.strip())
    lines.append("%s: %s" % (space.type(w_exception).name, w_exception.message))
    lines.append("")
    return "\n".join(lines)
{'fraction_non_alphanumeric': 0.12039877300613497, 'fraction_numerical': 0.009202453987730062, 'mean_word_length': 2.225886232481451, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2091948', 'n_tokens_mistral': 1424, 'n_tokens_neox': 1357, 'n_words': 302}