(self, func, args=(), kwargs={}, path='',
                      becomes_current_path=True)
Execute function with the given arguments in the sandbox.
Execute function with the given arguments in the sandbox.
Execute function with the given arguments in the sandbox .
def exec_function(self, func, args=(), kwargs={}, path='',
                      becomes_current_path=True):
        """Execute function with the given arguments in the sandbox.
        """
        if path and becomes_current_path:
            self._context.push_source(path)

        old_sandbox = self._context._sandbox
        self._context._sandbox = weakref.ref(self)

        # We don't have to worry about bytecode generation here because we are
        # too low-level for that. However, we could add bytecode generation via
        # the marshall module if parsing performance were ever an issue.

        old_source = self._current_source
        self._current_source = None
        try:
            func(*args, **kwargs)
        except SandboxError as e:
            raise e
        except NameError as e:
            # A NameError is raised when a variable could not be found.
            # The original KeyError has been dropped by the interpreter.
            # However, we should have it cached in our instance!

            # Unless a script is doing something wonky like catching NameError
            # itself (that would be silly), if there is an exception on the
            # global namespace, that's our error.
            actual = e

            if self._last_name_error is not None:
                actual = self._last_name_error
            source_stack = self._context.source_stack
            if not becomes_current_path:
                # Add current file to the stack because it wasn't added before
                # sandbox execution.
                source_stack.append(path)
            raise SandboxExecutionError(source_stack, type(actual), actual,
                                        sys.exc_info()[2])

        except Exception as e:
            # Need to copy the stack otherwise we get a reference and that is
            # mutated during the finally.
            exc = sys.exc_info()
            source_stack = self._context.source_stack
            if not becomes_current_path:
                # Add current file to the stack because it wasn't added before
                # sandbox execution.
                source_stack.append(path)
            raise SandboxExecutionError(source_stack, exc[0], exc[1], exc[2])
        finally:
            self._current_source = old_source
            self._context._sandbox = old_sandbox
            if path and becomes_current_path:
                self._context.pop_source()
def exec_function ( self , func , args = ( ) , kwargs = { } , path = '' , becomes_current_path = True ) : if path and becomes_current_path : self . _context . push_source ( path ) old_sandbox = self . _context . _sandbox self . _context . _sandbox = weakref . ref ( self ) # We don't have to worry about bytecode generation here because we are # too low-level for that. However, we could add bytecode generation via # the marshall module if parsing performance were ever an issue. old_source = self . _current_source self . _current_source = None try : func ( * args , * * kwargs ) except SandboxError as e : raise e except NameError as e : # A NameError is raised when a variable could not be found. # The original KeyError has been dropped by the interpreter. # However, we should have it cached in our instance! # Unless a script is doing something wonky like catching NameError # itself (that would be silly), if there is an exception on the # global namespace, that's our error. actual = e if self . _last_name_error is not None : actual = self . _last_name_error source_stack = self . _context . source_stack if not becomes_current_path : # Add current file to the stack because it wasn't added before # sandbox execution. source_stack . append ( path ) raise SandboxExecutionError ( source_stack , type ( actual ) , actual , sys . exc_info ( ) [ 2 ] ) except Exception as e : # Need to copy the stack otherwise we get a reference and that is # mutated during the finally. exc = sys . exc_info ( ) source_stack = self . _context . source_stack if not becomes_current_path : # Add current file to the stack because it wasn't added before # sandbox execution. source_stack . append ( path ) raise SandboxExecutionError ( source_stack , exc [ 0 ] , exc [ 1 ] , exc [ 2 ] ) finally : self . _current_source = old_source self . _context . _sandbox = old_sandbox if path and becomes_current_path : self . _context . pop_source ( )