# Buggy code
def add_unknown(index, priorities):
    priorities = {p[0]: p[1] for p in itervalues(priorities)}
    maxp = max(itervalues(priorities)) + 1 if priorities else 1
    for dist, info in iteritems(package_cache()):
        schannel, dname = dist2pair(dist)
        fname = dname + ".tar.bz2"
        fkey = dist + ".tar.bz2"
        if fkey in index or not info["dirs"]:
            continue
        try:
            with open(join(info["dirs"][0], "info", "index.json")) as fi:
                meta = json.load(fi)
        except IOError:
            continue
        if info["urls"]:
            url = info["urls"][0]
        elif "url" in meta:
            url = meta["url"]
        elif "channel" in meta:
            url = meta["channel"].rstrip("/") + "/" + fname
        else:
            url = "<unknown>/" + fname
        if url.rsplit("/", 1)[-1] != fname:
            continue
        channel, schannel2 = url_channel(url)
        if schannel2 != schannel:
            continue
        priority = priorities.get(schannel, maxp)
        if "link" in meta:
            del meta["link"]
        meta.update(
            {
                "fn": fname,
                "url": url,
                "channel": channel,
                "schannel": schannel,
                "priority": priority,
            }
        )
        meta.setdefault("depends", [])
        log.debug("adding cached pkg to index: %s" % fkey)
        index[fkey] = meta


# Traceback (TypeError)
% conda create -p ~/anaclone --clone root
Source:      /Users/ijstokes/anaconda
Destination: /Users/ijstokes/anaclone
The following packages cannot be cloned out of the root environment:
- conda-4.1.1-py27_0
- conda-build-1.20.3-py27_0
Packages: 332
Files: 16134
An unexpected error has occurred, please consider sending the
following traceback to the conda GitHub issue tracker at:

https://github.com/conda/conda/issues

clude the output of the command 'conda info' in your report.


Traceback (most recent call last):
File "/Users/ijstokes/anaconda/envs/ana40py35/bin/conda", line 6, in <module>
sys.exit(main())
File "/Users/ijstokes/anaconda/lib/python2.7/site-packages/conda/cli/main.py", line 120, in main
args_func(args, p)
File "/Users/ijstokes/anaconda/lib/python2.7/site-packages/conda/cli/main.py", line 127, in args_func
args.func(args, p)
File "/Users/ijstokes/anaconda/lib/python2.7/site-packages/conda/cli/main_create.py", line 57, in execute
install(args, parser, 'create')
File "/Users/ijstokes/anaconda/lib/python2.7/site-packages/conda/cli/install.py", line 218, in install
'unknown': args.unknown})
File "/Users/ijstokes/anaconda/lib/python2.7/site-packages/conda/cli/install.py", line 87, in clone
fetch_args=fetch_args)
File "/Users/ijstokes/anaconda/lib/python2.7/site-packages/conda/misc.py", line 353, in clone_env
force_extract=False, fetch_args=fetch_args)
File "/Users/ijstokes/anaconda/lib/python2.7/site-packages/conda/misc.py", line 56, in explicit
m = url_pat.match(spec)
TypeError: expected string or buffer

# Fixed code
def add_unknown(index, priorities):
    priorities = {p[0]: p[1] for p in itervalues(priorities)}
    maxp = max(itervalues(priorities)) + 1 if priorities else 1
    for dist, info in iteritems(package_cache()):
        schannel, dname = dist2pair(dist)
        fname = dname + ".tar.bz2"
        fkey = dist + ".tar.bz2"
        if fkey in index or not info["dirs"]:
            continue
        try:
            with open(join(info["dirs"][0], "info", "index.json")) as fi:
                meta = json.load(fi)
        except IOError:
            continue
        if info["urls"]:
            url = info["urls"][0]
        elif meta.get("url"):
            url = meta["url"]
        elif meta.get("channel"):
            url = meta["channel"].rstrip("/") + "/" + fname
        else:
            url = "<unknown>/" + fname
        if url.rsplit("/", 1)[-1] != fname:
            continue
        channel, schannel2 = url_channel(url)
        if schannel2 != schannel:
            continue
        priority = priorities.get(schannel, maxp)
        if "link" in meta:
            del meta["link"]
        meta.update(
            {
                "fn": fname,
                "url": url,
                "channel": channel,
                "schannel": schannel,
                "priority": priority,
            }
        )
        meta.setdefault("depends", [])
        log.debug("adding cached pkg to index: %s" % fkey)
        index[fkey] = meta