<commit_before>"""Safe execution of untrusted Python code."""

import json

def straw(v):
    return json.loads(json.dumps(jsonable_dict(v)))

def jsonable_dict(d):
    jd = {}
    for k,v in d.iteritems():
        try:
            json.dumps(v)
        except TypeError:
            continue
        else:
            jd[k] = v
    return jd

def safe_exec(code, globals_dict, locals_dict=None, future_division=False, assumed_imports=None):
    if future_division:
        code = "from __future__ import division\n" + code

    g_dict = straw(globals_dict)

    if locals_dict is None:
        l_dict = g_dict
    else:
        l_dict = straw(locals_dict)

    exec code in g_dict, l_dict

    globals_dict.update(straw(g_dict))
    if locals_dict is not None:
        locals_dict.update(straw(l_dict))<commit_msg>safe_exec can load modules for you.<commit_after>"""Safe execution of untrusted Python code."""

import json

from .lazymod import LazyModule

def straw(v):
    return json.loads(json.dumps(jsonable_dict(v)))

def jsonable_dict(d):
    jd = {}
    for k,v in d.iteritems():
        try:
            json.dumps(v)
        except TypeError:
            continue
        else:
            jd[k] = v
    return jd

def safe_exec(code, globals_dict, locals_dict=None, future_division=False, assumed_imports=None):
    if future_division:
        code = "from __future__ import division\n" + code

    g_dict = straw(globals_dict)

    if locals_dict is None:
        l_dict = g_dict
    else:
        l_dict = straw(locals_dict)

    for modname in assumed_imports or ():
        if isinstance(modname, tuple):
            name, modname = modname
        else:
            name = modname
        g_dict[name] = LazyModule(modname)

    exec code in g_dict, l_dict

    globals_dict.update(straw(g_dict))
    if locals_dict is not None:
        locals_dict.update(straw(l_dict))
{'fraction_non_alphanumeric': 0.09220985691573927, 'fraction_numerical': 0.0, 'mean_word_length': 2.5223880597014925, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5034318', 'n_tokens_mistral': 637, 'n_tokens_neox': 623, 'n_words': 156}