<commit_before>;(function($B){

eval($B.InjectBuiltins())

$B.$raise = function(arg){
    // Used for "raise" without specifying an exception.
    // If there is an exception in the stack, use it, else throw a simple
    // Exception
    if(arg === undefined){
        var es = $B.current_exception
        if(es !== undefined){throw es}
        throw _b_.RuntimeError.$factory("No active exception to reraise")
    }else if(isinstance(arg, BaseException)){
        throw arg
    }else if(arg.__class__ === $B.$factory && issubclass(arg, BaseException)){
        throw arg()
    }else if(arg.$is_class && issubclass(arg, BaseException)){
        throw arg.$factory()
    }else{
        throw _b_.TypeError.$factory("exceptions must derive from BaseException")
    }
}

$B.$syntax_err_line = function(exc, module, pos, line_num){
    // map position to line number
    var pos2line = {},
        lnum = 1,
        src = $B.$py_src[module],
        module = module.charAt(0) == "$" ? "<string>" : module
    if(src === undefined){
        console.log("no src for", module)
        exc.$line_info = line_num + ',' + module
        exc.args = _b_.tuple.$factory([$B.$getitem(exc.args, 0), module,
            line_num, 0, 0])
    }else{
        var line_pos = {1:0}
        for(var i = 0, len = src.length; i < len; i++){
            pos2line[i] = lnum
            if(src.charAt(i) == "\n"){line_pos[++lnum] = i}
        }
        while(line_num === undefined){
            line_num = pos2line[pos]
            pos--
        }
        exc.$line_info = line_num + "," + module

        var lines = src.split("\n"),
            line = lines[line_num - 1],
            lpos = pos - line_pos[line_num],
            len = line.length
        line = line.replace(/^\s*/,'')
        lpos -= len - line.length
        exc.args = _b_.tuple.$factory([$B.$getitem(exc.args, 0), module,
            line_num, lpos, line])
    }
}

$B.$SyntaxError = function(module, msg, pos, line_num, root) {
    if(root !== undefined && root.line_info !== undefined){
        // this may happen for syntax errors inside a lambda
        line_num = root.line_info
    }
    var exc = _b_.SyntaxError.$factory(msg)
    $B.$syntax_err_line(exc, module, pos, line_num)
    throw exc
}

$B.$IndentationError = function(module, msg, pos) {
    var exc = _b_.IndentationError.$factory(msg)
    $B.$syntax_err_line(exc, module, pos)
    throw exc
}


// class of traceback objects
var traceback = $B.make_class("traceback",
    function(exc){
        return {
            __class__ : traceback,
            exc: exc
        }
    }
)

traceback.__getattribute__ = function(self, attr){
    if(self.exc.$stack.length == 0){alert("no stack", attr)}
    var last_frame = $B.last(self.exc.$stack)
    if(last_frame === undefined){
        alert("last frame undef ")
        console.log(self.exc.$stack, Object.keys(self.exc.$stack))
    }
    var line_info = self.exc.$line_info || last_frame[1].$line_info

    switch(attr){
        case "tb_frame":
            return frame.$factory(self.exc.$stack)
        case "tb_lineno":
            if(line_info === undefined){return -1}
            else{return parseInt(line_info.split(",")[0])}
        case "tb_lasti":
            if(line_info === undefined){return "<unknown>"}
            else{
                var info = line_info.split(",")
                var src = $B.$py_src[info[1]]
                if(src !== undefined){
                    return src.split("\n")[parseInt(info[0] - 1)].trim()
                }else{return "<unknown>"}
            }
        case "tb_next":
            if(self.exc.$stack.length == 1){return None}
            else{
                return traceback.$factory(
                    self.exc.$stack.slice(0, self.exc.$stack.length - 1))
            }
        default:
            return _b_.object.__getattribute__(traceback, attr)
    }
}

$B.set_func_names(traceback, "builtins")

// class of frame objects
var frame = $B.make_class("frame",
    function(stack, pos){
        var fs = stack
        var res = {
            __class__: frame,
            f_builtins : {}, // XXX fix me
            $stack: stack,
        }
        if(pos === undefined){pos = fs.length - 1}
        res.$pos = pos
        if(fs.length){
            var _frame = fs[pos]
            var locals_id = _frame[0]
            try{
                res.f_locals = $B.obj_dict(_frame[1])
            }catch(err){
                console.log("err " + err)
                throw err
            }<commit_msg>fix traceback.tb_next<commit_after>;(function($B){

eval($B.InjectBuiltins())

$B.$raise = function(arg){
    // Used for "raise" without specifying an exception.
    // If there is an exception in the stack, use it, else throw a simple
    // Exception
    if(arg === undefined){
        var es = $B.current_exception
        if(es !== undefined){throw es}
        throw _b_.RuntimeError.$factory("No active exception to reraise")
    }else if(isinstance(arg, BaseException)){
        throw arg
    }else if(arg.__class__ === $B.$factory && issubclass(arg, BaseException)){
        throw arg()
    }else if(arg.$is_class && issubclass(arg, BaseException)){
        throw arg.$factory()
    }else{
        throw _b_.TypeError.$factory("exceptions must derive from BaseException")
    }
}

$B.$syntax_err_line = function(exc, module, pos, line_num){
    // map position to line number
    var pos2line = {},
        lnum = 1,
        src = $B.$py_src[module],
        module = module.charAt(0) == "$" ? "<string>" : module
    if(src === undefined){
        console.log("no src for", module)
        exc.$line_info = line_num + ',' + module
        exc.args = _b_.tuple.$factory([$B.$getitem(exc.args, 0), module,
            line_num, 0, 0])
    }else{
        var line_pos = {1:0}
        for(var i = 0, len = src.length; i < len; i++){
            pos2line[i] = lnum
            if(src.charAt(i) == "\n"){line_pos[++lnum] = i}
        }
        while(line_num === undefined){
            line_num = pos2line[pos]
            pos--
        }
        exc.$line_info = line_num + "," + module

        var lines = src.split("\n"),
            line = lines[line_num - 1],
            lpos = pos - line_pos[line_num],
            len = line.length
        line = line.replace(/^\s*/,'')
        lpos -= len - line.length
        exc.args = _b_.tuple.$factory([$B.$getitem(exc.args, 0), module,
            line_num, lpos, line])
    }
}

$B.$SyntaxError = function(module, msg, pos, line_num, root) {
    if(root !== undefined && root.line_info !== undefined){
        // this may happen for syntax errors inside a lambda
        line_num = root.line_info
    }
    var exc = _b_.SyntaxError.$factory(msg)
    $B.$syntax_err_line(exc, module, pos, line_num)
    throw exc
}

$B.$IndentationError = function(module, msg, pos) {
    var exc = _b_.IndentationError.$factory(msg)
    $B.$syntax_err_line(exc, module, pos)
    throw exc
}


// class of traceback objects
var traceback = $B.make_class("traceback",
    function(exc, stack){
        if(stack===undefined)
            stack=exc.$stack
        return {
            __class__ : traceback,
            $stack: stack,
            exc: exc
        }
    }
)

traceback.__getattribute__ = function(self, attr){
    if(self.$stack.length == 0){alert("no stack", attr)}
    var last_frame = $B.last(self.$stack)
    if(last_frame === undefined){
        alert("last frame undef ")
        console.log(self.$stack, Object.keys(self.$stack))
    }
    var line_info = last_frame[1].$line_info

    switch(attr){
        case "tb_frame":
            return frame.$factory(self.$stack)
        case "tb_lineno":
            if(line_info === undefined){return -1}
            else{return parseInt(line_info.split(",")[0])}
        case "tb_lasti":
            if(line_info === undefined){return "<unknown>"}
            else{
                var info = line_info.split(",")
                var src = $B.$py_src[info[1]]
                if(src !== undefined){
                    return src.split("\n")[parseInt(info[0] - 1)].trim()
                }else{return "<unknown>"}
            }
        case "tb_next":
            if(self.$stack.length <= 1){return None}
            else{
                return traceback.$factory(self.exc,
                    self.exc.$stack.slice(0, self.exc.$stack.length - 2))
            }
        default:
            return _b_.object.__getattribute__(traceback, attr)
    }
}

$B.set_func_names(traceback, "builtins")

// class of frame objects
var frame = $B.make_class("frame",
    function(stack, pos){
        var fs = stack
        var res = {
            __class__: frame,
            f_builtins : {}, // XXX fix me
            $stack: stack,
        }
        if(pos === undefined){pos = fs.length - 1}
        res.$pos = pos
        if(fs.length){
            var _frame = fs[pos]
            var locals_id = _frame[0]
            try{
                res.f_locals = $B.obj_dict(_frame[1])
            }catch(err){
                console.log("err " + err)
                throw err
            }
{'fraction_non_alphanumeric': 0.1465450950236186, 'fraction_numerical': 0.00571240250466879, 'mean_word_length': 1.9481865284974094, 'pattern_counts': {'":': 8, '<': 12, '<?xml version=': 0, '>': 9, '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': '3795332', 'n_tokens_mistral': 2869, 'n_tokens_neox': 2809, 'n_words': 637}