sys._getframe.f_back.f_locals

Here are the examples of the python api sys._getframe.f_back.f_locals taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

3 Examples 7

Example 1

Project: codetools
Source File: with_mask.py
View license
    def __enter__(self):
        """ Enter method.
        """
        locals_val = sys._getframe().f_back.f_locals
        if locals_val.has_key('context') and isinstance(locals_val['context'],
                                                        dict):
            locals_val = locals_val['context']

        # Make a copy of this context.
        locals_val = adapt(locals_val, ICheckpointable).checkpoint()

        adapters = [WithMaskAdapter(mask=self.mask)]
        self.context = AdaptedDataContext(subcontext=locals_val,
                                          _adapters=adapters)

        return

Example 2

Project: ayrton
Source File: remote.py
View license
    def __enter__ (self):
        # get the globals from the runtime

        # for solving the import problem:
        # _pickle.PicklingError: Can't pickle <class 'module'>: attribute lookup builtins.module failed
        # there are two solutions. either we setup a complex system that intercepts
        # the imports and hold them in another ayrton.Environment attribute
        # or we just weed them out here. so far this is the simpler option
        # but forces the user to reimport what's going to be used in the remote
        g= clean_environment (ayrton.runner.globals)

        # get the locals from the runtime
        # this is not so easy: for some reason, ayrton.runner.locals is not up to
        # date in the middle of the execution (remember *this* code is executed
        # via exec() in Ayrton.run_code())
        # another option is to go through the frames
        inception_locals= sys._getframe().f_back.f_locals
        l= clean_environment (inception_locals)

        # special treatment for argv
        g['argv']= ayrton.runner.globals['argv']

        logger.debug3 ('globals passed to remote: %s', ayrton.utils.dump_dict (g))
        global_env= pickle.dumps (g)
        logger.debug3 ('locals passed to remote: %s', ayrton.utils.dump_dict (l))
        local_env= pickle.dumps (l)

        backchannel_port= 4227

        command= self.remote_command (backchannel_port, global_env, local_env)
        logger.debug ('code to execute remote: %s', command)

        try:
            i, o, e= self.prepare_connections (backchannel_port, command)

            logger.debug ('sending ast, globals, locals')
            # TODO: compress?
            self.result_channel.sendall (self.ast)
            self.result_channel.sendall (global_env)
            self.result_channel.sendall (local_env)

            # TODO: handle _in, _out, _err
            self.remote= RemoteStub (( (os.dup (0), i), (o, os.dup (1)), (e, os.dup (2)) ))
        except (paramiko.ssh_exception.SSHException, ConnectionError, OSError) as e:
            # NOTE: this is the only time we do this
            # please make sure the list of fileobjs is correct
            for fileobj in (self.result_channel, self.result_listen, self.client):
                try:
                    close (fileobj)
                except Exception as inner:
                    logger.debug (traceback.format_exc (inner))

            raise e

Example 3

Project: codetools
Source File: with_mask.py
View license
    def __exit__(self, exc_type, exc_val, exc_tb):
        """ Exit method.
        """
        locals_val = sys._getframe().f_back.f_locals
        if locals_val.has_key('context') and isinstance(locals_val['context'],
                                                        dict):
            locals_val = locals_val['context']

        for k in locals_val.keys():
            if k in self.context.keys() and isinstance(self.context[k],
                                                       ndarray):
                equal_values = self.context[k] == locals_val[k]
                if isinstance(equal_values, ndarray):
                    equal_values = equal_values.all()

                if not equal_values:
                    self.context[k] = locals_val[k]
                    locals_val[k] = self.context[k]

        self.context.pop_adapter()
        return