Skip to content

Commit

Permalink
Remove itertools.tee
Browse files Browse the repository at this point in the history
The problem with usign tee is that only the first callback to use the iterator can write to it. In `ready_states`, the `save_state` after the `yield` statement is ignored for all others.
  • Loading branch information
Eric Hennenfent committed Aug 5, 2020
1 parent 36c6fbd commit 11404eb
Showing 1 changed file with 2 additions and 11 deletions.
13 changes: 2 additions & 11 deletions manticore/utils/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
import functools
from typing import Dict, Set
from itertools import takewhile, tee
from itertools import takewhile
from weakref import WeakKeyDictionary, ref
from inspect import isgenerator

Expand Down Expand Up @@ -155,23 +155,14 @@ def _publish(self, _name, *args, can_raise=True, **kwargs):
# shouldn't check the event.
def _publish_impl(self, _name, *args, **kwargs):
bucket_items = self._get_signal_bucket(_name).items()
n = sum(len(methods) for _r, methods in bucket_items)
clones = {}
for i, item in enumerate(args):
if isgenerator(item):
clones[i] = tee(item, n)

i = 0
for robj, methods in bucket_items:
for callback in methods:
# Need to clone any iterable args, otherwise the first usage will drain it
# WARNING: THIS IS NOT THREAD SAFE https://docs.python.org/3.8/library/itertools.html#itertools.tee
new_args = (
(arg if not isgenerator(arg) else clones[arg_idx][i])
for arg_idx, arg in enumerate(args)
(arg if not isgenerator(arg) else getattr(self, arg.__name__)) for arg in args
)
callback(robj(), *new_args, **kwargs)
i += 1

# The include_source flag indicates to prepend the source of the event in
# the callback signature. This is set on forward_events_from/to
Expand Down

0 comments on commit 11404eb

Please sign in to comment.