Beispiel #1
0
 def run_code(self, code, f_globals=None, f_locals=None):
     if f_globals is None: f_globals = builtins.globals()
     if f_locals is None: f_locals = f_globals
     if '__builtins__' not in f_globals:
         f_globals['__builtins__'] = builtins.__dict__
     frame = Frame(code, f_globals, f_locals, None, None)
     return self.run(frame)
Beispiel #2
0
def import_all_but_wx(
    name,
    globals=builtins.globals(),
    locals=builtins.locals(),
    fromlist=[],
    level=0,
    default_import=builtins.__import__,
):
    if name == "wx" or name.startswith("wx."):
        raise ImportError("Not allowed to import wx!")
    return default_import(name, globals, locals, fromlist, level)
Beispiel #3
0
def exec(source, globals=None, locals=None, forbidden=False, builtins=False,
         exec_function=None):
    """
    Similar to the built-in function exec() for Pytuguês code.

    The additional exec_function() argument allows to define a function to
    replace Python's builtin compile().

    Args:
        source (str or code):
            Code to be executed.
        globals, locals:
            A globals/locals dictionary
        builtins (bool):
            If given, update builtins with functions in tugalib.
        forbidden (bool):
            If true, initialize the forbidden lib functionality to enable i18n
            for Python builtins in C-level.
        exec_function (callable):
            A possible replacement for Python's built-in exec().
    """

    exec_function = exec_function or _exec

    if globals is None:
        globals = _builtins.globals()

    if builtins:
        update_builtins(forbidden)
    else:
        globals.update(tugalib_namespace(forbidden))

    if isinstance(source, str):
        code = transpile(source)
    else:
        code = source

    if locals is None:
        return exec_function(code, globals)
    else:
        return exec_function(code, globals, locals)
Beispiel #4
0
 def reset_context(self):
     # create new context for execution
     self._globals_ns = copy.copy(builtins.globals())
Beispiel #5
0
    lambda *args, **kwargs: wrap(builtins.format)(*args, **kwargs),
    builtins.format)
frozenset = functools.update_wrapper(
    lambda *args, **kwargs: builtins.frozenset(*args, **kwargs),
    builtins.frozenset)
frozenset._ = functools.update_wrapper(
    lambda *args, **kwargs: wrap(builtins.frozenset)(*args, **kwargs),
    builtins.frozenset)
getattr = functools.update_wrapper(
    lambda *args, **kwargs: builtins.getattr(*args, **kwargs),
    builtins.getattr)
getattr._ = functools.update_wrapper(
    lambda *args, **kwargs: wrap(builtins.getattr)(*args, **kwargs),
    builtins.getattr)
globals = functools.update_wrapper(
    lambda *args, **kwargs: builtins.globals(*args, **kwargs),
    builtins.globals)
globals._ = functools.update_wrapper(
    lambda *args, **kwargs: wrap(builtins.globals)(*args, **kwargs),
    builtins.globals)
hasattr = functools.update_wrapper(
    lambda *args, **kwargs: builtins.hasattr(*args, **kwargs),
    builtins.hasattr)
hasattr._ = functools.update_wrapper(
    lambda *args, **kwargs: wrap(builtins.hasattr)(*args, **kwargs),
    builtins.hasattr)
hash = functools.update_wrapper(
    lambda *args, **kwargs: builtins.hash(*args, **kwargs), builtins.hash)
hash._ = functools.update_wrapper(
    lambda *args, **kwargs: wrap(builtins.hash)(*args, **kwargs),
    builtins.hash)
Beispiel #6
0
import builtins

builtins.globals()

ranking = {'A': 100, 'B': 85, 'C': 95}

#ranking.get('A')
print(sorted(ranking, key=ranking.get, reverse=True))
Beispiel #7
0
def run(code, f_globals, f_locals):
    if f_globals is None: f_globals = builtins.globals()
    if f_locals is None:  f_locals = f_globals
    if '__builtins__' not in f_globals:
        f_globals['__builtins__'] = builtins.__dict__
    return run_frame(code, None, f_globals, f_locals)