<commit_before>"""Bridge

The Bridge Component is used for inter-process communications between
processes. Bridge is used internally when a Component is started in
"process mode" via :meth:`circuits.core.manager.start`. Typically a
Pipe is used as the socket transport between two sides of a Bridge
(*there must be a :class:`~Bridge` instnace on both sides*).
"""


import traceback

try:
    from cPickle import dumps, loads
except ImportError:
    from pickle import dumps, loads  # NOQA


from ..six import b
from .values import Value
from .handlers import handler
from .events import Event, exception
from .components import BaseComponent


_sentinel = b('~~~')


class ipc(Event):
    """ipc Event

    Send an event to a child/parent process
    """

    def __init__(self, event, channel=None):
        """
        :param event:   Event to execute remotely.
        :type event:    :class:`circuits.core.events.Event`

        :param channel: IPC Channel (channel to use on child/parent).
        :type channel:  str
        """

        super(ipc, self).__init__(event, channel=channel)


class Bridge(BaseComponent):

    channel = "bridge"

    def init(self, socket, channel=channel):
        self._buffer = ""
        self._socket = socket
        self._values = dict()

        if self._socket is not None:
            self._socket.register(self)

    def _process_packet(self, eid, obj):
        if isinstance(obj, Event):
            obj.remote = True
            obj.notify = "value_changed"
            obj.waitingHandlers = 0
            value = self.fire(obj)
            self._values[value] = eid
        elif isinstance(obj, Value):
            if obj.result:
                if isinstance(obj.value, list):
                    for item in obj.value:
                        self._values[eid].value = item
                else:
                    self._values[eid].value = obj.value
            event = Event.create(Bridge.__waiting_event(eid))
            event.remote = True
            self.fire(event, self.channel)

    @handler("value_changed", channel="*")<commit_msg>Fixed broken Python 3 compatibility and broken test with new bridge improvement(s)<commit_after>"""Bridge

The Bridge Component is used for inter-process communications between
processes. Bridge is used internally when a Component is started in
"process mode" via :meth:`circuits.core.manager.start`. Typically a
Pipe is used as the socket transport between two sides of a Bridge
(*there must be a :class:`~Bridge` instnace on both sides*).
"""


import traceback

try:
    from cPickle import dumps, loads
except ImportError:
    from pickle import dumps, loads  # NOQA


from ..six import b
from .values import Value
from .handlers import handler
from .events import Event, exception
from .components import BaseComponent


_sentinel = b('~~~')


class ipc(Event):
    """ipc Event

    Send an event to a child/parent process
    """

    def __init__(self, event, channel=None):
        """
        :param event:   Event to execute remotely.
        :type event:    :class:`circuits.core.events.Event`

        :param channel: IPC Channel (channel to use on child/parent).
        :type channel:  str
        """

        super(ipc, self).__init__(event, channel=channel)


class Bridge(BaseComponent):

    channel = "bridge"

    def init(self, socket, channel=channel):
        self._buffer = b("")
        self._socket = socket
        self._values = dict()

        if self._socket is not None:
            self._socket.register(self)

    def _process_packet(self, eid, obj):
        if isinstance(obj, Event):
            obj.remote = True
            obj.notify = "value_changed"
            obj.waitingHandlers = 0
            value = self.fire(obj)
            self._values[value] = eid
        elif isinstance(obj, Value):
            if obj.result:
                if isinstance(obj.value, list):
                    for item in obj.value:
                        self._values[eid].value = item
                else:
                    self._values[eid].value = obj.value
            event = Event.create(Bridge.__waiting_event(eid))
            event.remote = True
            self.fire(event, self.channel)

    @handler("value_changed", channel="*")
{'fraction_non_alphanumeric': 0.09610876699484294, 'fraction_numerical': 0.0007032348804500703, 'mean_word_length': 2.3026315789473686, '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': '2723014', 'n_tokens_mistral': 1202, 'n_tokens_neox': 1133, 'n_words': 387}