# Buggy code
def __setitem__(self, key, value):
    # Enforce unicode compatibility.
    if PY2 and isinstance(value, native_str):
        # Allow Python 2's implicit string decoding, but fail now instead of when entry fields are used.
        # If encoding is anything but ascii, it should be decoded it to text before setting an entry field
        try:
            value = value.decode("ascii")
        except UnicodeDecodeError:
            raise EntryUnicodeError(key, value)
    elif isinstance(value, bytes):
        raise EntryUnicodeError(key, value)
    # Coerce any enriched strings (such as those returned by BeautifulSoup) to plain strings to avoid serialization
    # troubles.
    elif isinstance(value, text_type) and type(value) != text_type:  # pylint: disable=unidiomatic-typecheck
        value = text_type(value)

    # url and original_url handling
    if key == "url":
        if not isinstance(value, (str, LazyLookup)):
            raise PluginError("Tried to set %r url to %r" % (self.get("title"), value))
        self.setdefault("original_url", value)

    # title handling
    if key == "title":
        if not isinstance(value, (str, LazyLookup)):
            raise PluginError("Tried to set title to %r" % value)

    try:
        log.trace("ENTRY SET: %s = %r" % (key, value))
    except Exception as e:
        log.debug("trying to debug key `%s` value threw exception: %s" % (key, e))

    super(Entry, self).__setitem__(key, value)


# Traceback (KeyError)
2016-02-02 23:41 CRITICAL task_queue                    BUG: Unhandled exception during task queue run loop.
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/flexget/task_queue.py", line 47, in run
self.current_task.execute()
File "/usr/local/lib/python2.7/dist-packages/flexget/task.py", line 69, in wrapper
return func(self, *args, **kw)
File "/usr/local/lib/python2.7/dist-packages/flexget/task.py", line 578, in execute
self._execute()
File "/usr/local/lib/python2.7/dist-packages/flexget/task.py", line 557, in _execute
entry.complete()
File "/usr/local/lib/python2.7/dist-packages/flexget/entry.py", line 163, in complete
self.run_hooks('complete', **kwargs)
File "/usr/local/lib/python2.7/dist-packages/flexget/entry.py", line 78, in run_hooks
func(self, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/flexget/plugins/input/discover.py", line 178, in entry_complete
search_results.remove(entry)
KeyError: <Entry(title=Sarah.&amp;.Duck.S01E03.Lach.mal.Esel.GERMAN.HDTV.x264-pbw,state=rejected)>

# Fixed code
def __setitem__(self, key, value):
    # Enforce unicode compatibility.
    if PY2 and isinstance(value, native_str):
        # Allow Python 2's implicit string decoding, but fail now instead of when entry fields are used.
        # If encoding is anything but ascii, it should be decoded it to text before setting an entry field
        try:
            value = value.decode("ascii")
        except UnicodeDecodeError:
            raise EntryUnicodeError(key, value)
    elif isinstance(value, bytes):
        raise EntryUnicodeError(key, value)
    # Coerce any enriched strings (such as those returned by BeautifulSoup) to plain strings to avoid serialization
    # troubles.
    elif isinstance(value, text_type) and type(value) != text_type:  # pylint: disable=unidiomatic-typecheck
        value = text_type(value)

    # url and original_url handling
    if key == "url":
        if not isinstance(value, (str, LazyLookup)):
            raise PluginError("Tried to set %r url to %r" % (self.get("title"), value))
        self.setdefault("original_url", value)

    # title handling
    if key == "title":
        if not isinstance(value, (str, LazyLookup)):
            raise PluginError("Tried to set title to %r" % value)
        self.setdefault("original_title", value)

    try:
        log.trace("ENTRY SET: %s = %r" % (key, value))
    except Exception as e:
        log.debug("trying to debug key `%s` value threw exception: %s" % (key, e))

    super(Entry, self).__setitem__(key, value)