# Buggy code
def add_defaults_to_specs(r, linked, specs, update=False):
    # TODO: This should use the pinning mechanism. But don't change the API:
    # cas uses it.
    if r.explicit(specs):
        return
    log.debug("H0 specs=%r" % specs)
    linked = [d if d.endswith(".tar.bz2") else d + ".tar.bz2" for d in linked]
    names_linked = {name_dist(dist): dist for dist in linked}
    mspecs = list(map(MatchSpec, specs))

    for name, def_ver in [
        ("python", default_python),
        # Default version required, but only used for Python
        ("lua", None),
    ]:
        if any(s.name == name and not s.is_simple() for s in mspecs):
            # if any of the specifications mention the Python/Numpy version,
            # we don't need to add the default spec
            log.debug("H1 %s" % name)
            continue

        depends_on = {s for s in mspecs if r.depends_on(s, name)}
        any_depends_on = bool(depends_on)
        log.debug("H2 %s %s" % (name, any_depends_on))

        if not any_depends_on:
            # if nothing depends on Python/Numpy AND the Python/Numpy is not
            # specified, we don't need to add the default spec
            log.debug("H2A %s" % name)
            continue

        if any(s.is_exact() for s in depends_on):
            # If something depends on Python/Numpy, but the spec is very
            # explicit, we also don't need to add the default spec
            log.debug("H2B %s" % name)
            continue

        if name in names_linked:
            # if Python/Numpy is already linked, we add that instead of the
            # default
            log.debug("H3 %s" % name)
            fkey = names_linked[name]
            info = r.index[fkey]
            ver = ".".join(info["version"].split(".", 2)[:2])
            spec = "%s %s* (target=%s)" % (info["name"], ver, fkey)
            specs.append(spec)
            continue

        if name == "python" and def_ver.startswith("3."):
            # Don't include Python 3 in the specs if this is the Python 3
            # version of conda.
            continue

        if def_ver is not None:
            specs.append("%s %s*" % (name, def_ver))
    log.debug("HF specs=%r" % specs)


# Traceback (KeyError)
$ CIO_TEST=3 conda update pycrypto
...
Traceback (most recent call last):
File "/Users/ilan/python/bin/conda", line 6, in <module>
sys.exit(conda.cli.main.main())
File "/Users/ilan/conda/conda/cli/main.py", line 120, in main
args_func(args, p)
File "/Users/ilan/conda/conda/cli/main.py", line 127, in args_func
args.func(args, p)
File "/Users/ilan/conda/conda/cli/main_update.py", line 64, in execute
install.install(args, parser, 'update')
File "/Users/ilan/conda/conda/cli/install.py", line 232, in install
plan.add_defaults_to_specs(r, linked, specs, update=isupdate)
File "/Users/ilan/conda/conda/plan.py", line 344, in add_defaults_to_specs
names_linked = {r.index[fn]['name']: fn for fn in linked}
File "/Users/ilan/conda/conda/plan.py", line 344, in <dictcomp>
names_linked = {r.index[fn]['name']: fn for fn in linked}
KeyError: 'xar.tar.bz2'

# Fixed code
def add_defaults_to_specs(r, linked, specs, update=False):
    # TODO: This should use the pinning mechanism. But don't change the API:
    # cas uses it.
    if r.explicit(specs):
        return
    log.debug("H0 specs=%r" % specs)
    linked = [d if d.endswith(".tar.bz2") else d + ".tar.bz2" for d in linked]
    names_linked = {r.index[fn]["name"]: fn for fn in linked if fn in r.index}
    mspecs = list(map(MatchSpec, specs))

    for name, def_ver in [
        ("python", default_python),
        # Default version required, but only used for Python
        ("lua", None),
    ]:
        if any(s.name == name and not s.is_simple() for s in mspecs):
            # if any of the specifications mention the Python/Numpy version,
            # we don't need to add the default spec
            log.debug("H1 %s" % name)
            continue

        depends_on = {s for s in mspecs if r.depends_on(s, name)}
        any_depends_on = bool(depends_on)
        log.debug("H2 %s %s" % (name, any_depends_on))

        if not any_depends_on:
            # if nothing depends on Python/Numpy AND the Python/Numpy is not
            # specified, we don't need to add the default spec
            log.debug("H2A %s" % name)
            continue

        if any(s.is_exact() for s in depends_on):
            # If something depends on Python/Numpy, but the spec is very
            # explicit, we also don't need to add the default spec
            log.debug("H2B %s" % name)
            continue

        if name in names_linked:
            # if Python/Numpy is already linked, we add that instead of the
            # default
            log.debug("H3 %s" % name)
            fkey = names_linked[name]
            info = r.index[fkey]
            ver = ".".join(info["version"].split(".", 2)[:2])
            spec = "%s %s* (target=%s)" % (info["name"], ver, fkey)
            specs.append(spec)
            continue

        if name == "python" and def_ver.startswith("3."):
            # Don't include Python 3 in the specs if this is the Python 3
            # version of conda.
            continue

        if def_ver is not None:
            specs.append("%s %s*" % (name, def_ver))
    log.debug("HF specs=%r" % specs)