Ejemplo n.º 1
0
    def remote_exec(self, source, **kwargs):
        """ return channel object and connect it to a remote
            execution thread where the given ``source`` executes.

            * ``source`` is a string: execute source string remotely
              with a ``channel`` put into the global namespace.
            * ``source`` is a pure function: serialize source and
              call function with ``**kwargs``, adding a
              ``channel`` object to the keyword arguments.
            * ``source`` is a pure module: execute source of module
              with a ``channel`` in its global namespace

            In all cases the binding ``__name__='__channelexec__'``
            will be available in the global namespace of the remotely
            executing code.
        """
        call_name = None
        if isinstance(source, types.ModuleType):
            linecache.updatecache(inspect.getsourcefile(source))
            source = inspect.getsource(source)
        elif isinstance(source, types.FunctionType):
            call_name = source.__name__
            source = _source_of_function(source)
        else:
            source = textwrap.dedent(str(source))

        if call_name is None and kwargs:
            raise TypeError("can't pass kwargs to non-function remote_exec")

        channel = self.newchannel()
        self._send(Message.CHANNEL_EXEC,
                   channel.id,
                   gateway_base.dumps_internal((source, call_name, kwargs)))
        return channel
Ejemplo n.º 2
0
    def remote_exec(self, source, **kwargs):
        """ return channel object and connect it to a remote
            execution thread where the given ``source`` executes.

            * ``source`` is a string: execute source string remotely
              with a ``channel`` put into the global namespace.
            * ``source`` is a pure function: serialize source and
              call function with ``**kwargs``, adding a
              ``channel`` object to the keyword arguments.
            * ``source`` is a pure module: execute source of module
              with a ``channel`` in its global namespace

            In all cases the binding ``__name__='__channelexec__'``
            will be available in the global namespace of the remotely
            executing code.
        """
        call_name = None
        if isinstance(source, types.ModuleType):
            linecache.updatecache(inspect.getsourcefile(source))
            source = inspect.getsource(source)
        elif isinstance(source, types.FunctionType):
            call_name = source.__name__
            source = _source_of_function(source)
        else:
            source = textwrap.dedent(str(source))

        if call_name is None and kwargs:
            raise TypeError("can't pass kwargs to non-function remote_exec")

        channel = self.newchannel()
        self._send(Message.CHANNEL_EXEC, channel.id,
                   gateway_base.dumps_internal((source, call_name, kwargs)))
        return channel
Ejemplo n.º 3
0
 def reconfigure(self, py2str_as_py3str=True, py3str_as_py2str=False):
     """
     set the string coercion for this gateway
     the default is to try to convert py2 str as py3 str,
     but not to try and convert py3 str to py2 str
     """
     self._strconfig = (py2str_as_py3str, py3str_as_py2str)
     data = gateway_base.dumps_internal(self._strconfig)
     self._send(Message.RECONFIGURE, data=data)
Ejemplo n.º 4
0
 def reconfigure(self, py2str_as_py3str=True, py3str_as_py2str=False):
     """
     set the string coercion for this gateway
     the default is to try to convert py2 str as py3 str,
     but not to try and convert py3 str to py2 str
     """
     self._strconfig = (py2str_as_py3str, py3str_as_py2str)
     data = gateway_base.dumps_internal(self._strconfig)
     self._send(Message.RECONFIGURE, data=data)